Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

ASCII LED control
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Re: ASCII LED control

by utoto on Tue Aug 24, 2010 2:10 pm

Is there an efficient way to use a master char to control both red and blue and yellow and green and both stops?
utoto
 
Posts: 28
Joined: Mon Aug 23, 2010 3:08 pm

Re: ASCII LED control

by adafruit_support_bill on Tue Aug 24, 2010 2:15 pm

Code: Select all | TOGGLE FULL SIZE
       digitalWrite(led1, ledState1);
       digitalWrite(led2, ledState2);
    }


You left this in the middle when you copied the code. That resulted in unbalenced brackets '{}'.

adafruit_support_bill
 
Posts: 78733
Joined: Sat Feb 07, 2009 10:11 am

Re: ASCII LED control

by adafruit_support_bill on Tue Aug 24, 2010 2:21 pm

Sure. Just add another 'else if' test for the 'master character' and duplicate the code for all the operations you want to include.

There may be more efficient implementations, but it's best to save the optimizations until your design has stabilized.

adafruit_support_bill
 
Posts: 78733
Joined: Sat Feb 07, 2009 10:11 am

Re: ASCII LED control

by utoto on Tue Aug 24, 2010 2:27 pm

I think this works for master commands.
:D
How does it look?

Code: Select all | TOGGLE FULL SIZE
/*
* Turn 4 leds split into 2 sets on and off with special characters
* sent over the serial connection with only 1 led allowed on at a
* time. If  the 2nd led is commanded on, the first will be turned
* off prior.
*/

int led1 = 13;      // led is hoooked up to this pin
int led2 = 12;      // led is hoooked up to this pin
int led3 = 11;      // led is hoooked up to this pin
int led4 = 10;      // led is hoooked up to this pin

char red = 'r';  // ascii LED 1 on
char off = 'o';  // ascii leds off
char yellow = 'y';  // ascii LED 2 on
char blue = 'b';  // ascii LED 3 on
char off2 = 'f';  // ascii leds2 off
char green = 'g';  // ascii LED 4 on
char M1 = '1';  // ascii LED 1&3 on
char Moff = 'a';  // ascii ledsAll off
char M2 = '2';  // ascii LED 2&4 on

int ledState1;
int ledState2;
int ledState3;
int ledState4;
char nextChar;

void setup()
{
   Serial.begin(9600);
   
   pinMode(led1, OUTPUT);
   digitalWrite(led1, LOW);
   ledState1 = LOW;
   
   pinMode(led2, OUTPUT);
   digitalWrite(led2, LOW);
   ledState2 = LOW;
   
   pinMode(led3, OUTPUT);
   digitalWrite(led3, LOW);
   ledState3 = LOW;
   
   pinMode(led4, OUTPUT);
   digitalWrite(led4, LOW);
   ledState4 = LOW;
}

void loop()
{
  if (Serial.available())
    {   
       nextChar = Serial.read();  // read just one character
       if (nextChar == red)  // check for 'r'
       {
         ledState1 = HIGH;
         ledState2 = LOW;
         Serial.println("led 1 On-red");
       }
       else if (nextChar == yellow)  // check for 'y'
       {
         ledState1 = LOW;
         ledState2 = HIGH;
         Serial.println("led 2 On-yellow");
       }
       else if (nextChar == off)  // check for 'o'
       {                 
         ledState1 = LOW;
         ledState2 = LOW;
         Serial.println("leds Off");
       }   
       else if (nextChar == blue)  // check for 'b'
       {
         ledState3 = HIGH;
         ledState4 = LOW;
         Serial.println("led 3 On-blue");
       }
       else if (nextChar == green)  // check for 'g'
       {
         ledState3 = LOW;
         ledState4 = HIGH;
         Serial.println("led 4 On-green");
       }
       else if (nextChar == off2)  // check for 'f'
       {                 
         ledState3 = LOW;
         ledState4 = LOW;
         Serial.println("leds2 Off");
       }
       else if (nextChar == M1)  // check for '1'
       {
         ledState1 = HIGH;
         ledState2 = LOW;
         ledState3 = HIGH;
         ledState4 = LOW;
         Serial.println("led 1 & 3 On-red & blue");
       }
       else if (nextChar == M2)  // check for '2'
       {
         ledState1 = LOW;
         ledState2 = HIGH;
         ledState3 = LOW;
         ledState4 = HIGH;
         Serial.println("led 2 & 4 On-yellow & green");
       }
       else if (nextChar == Moff)  // check for 'a'
       {
         ledState1 = LOW;
         ledState2 = LOW;
         ledState3 = LOW;
         ledState4 = LOW;
         Serial.println("All leds Off");
       }
     
       digitalWrite(led1, ledState1);
       digitalWrite(led2, ledState2);
       digitalWrite(led3, ledState3);
       digitalWrite(led4, ledState4);
    }

}
utoto
 
Posts: 28
Joined: Mon Aug 23, 2010 3:08 pm

Re: ASCII LED control

by utoto on Tue Aug 24, 2010 2:39 pm

Here I have tried to enter a single char. pass-code or address for this controller.
It should keep anything from happening unless the "A" is placed before any other char. in a string.
But, it does not. I think it is in the execution of the pass-code, but, I am not sure how to do this.

Code: Select all | TOGGLE FULL SIZE
/*
* Turn 4 leds split into 2 sets on and off with special characters
* sent over the serial connection with only 1 led allowed on at a
* time. If  the 2nd led is commanded on, the first will be turned
* off prior.
*/

int led1 = 13;      // led is hoooked up to this pin
int led2 = 12;      // led is hoooked up to this pin
int led3 = 11;      // led is hoooked up to this pin
int led4 = 10;      // led is hoooked up to this pin

char red = 'r';  // ascii LED 1 on
char off = 'o';  // ascii leds off
char yellow = 'y';  // ascii LED 2 on
char blue = 'b';  // ascii LED 3 on
char off2 = 'f';  // ascii leds2 off
char green = 'g';  // ascii LED 4 on
char M1 = '1';  // ascii LED 1&3 on
char Moff = 'a';  // ascii ledsAll off
char M2 = '2';  // ascii LED 2&4 on

int passChar = 'A';   // the first character of the password
int charCount;

int ledState1;
int ledState2;
int ledState3;
int ledState4;
char nextChar;

void setup()
{
   Serial.begin(9600);
   
   pinMode(led1, OUTPUT);
   digitalWrite(led1, LOW);
   ledState1 = LOW;
   
   pinMode(led2, OUTPUT);
   digitalWrite(led2, LOW);
   ledState2 = LOW;
   
   pinMode(led3, OUTPUT);
   digitalWrite(led3, LOW);
   ledState3 = LOW;
   
   pinMode(led4, OUTPUT);
   digitalWrite(led4, LOW);
   ledState4 = LOW;
   
   charCount = 0;
}

void loop()
{
  if (Serial.available())
   
    Serial.println(nextChar);
       
    if (charCount == 0) // check for the first char of the pwd...
    {     
      if (nextChar == passChar)
      {
        charCount = 1;  // got it, so update the character counter
      }
      else
      {
        charCount = 0;  // wrong character, so reset count to zero
      }
    }
 
    {   
       nextChar = Serial.read();  // read just one character
       if (nextChar == red)  // check for 'r'
       {
         ledState1 = HIGH;
         ledState2 = LOW;
         Serial.println("led 1 On-red");
       }
       else if (nextChar == yellow)  // check for 'y'
       {
         ledState1 = LOW;
         ledState2 = HIGH;
         Serial.println("led 2 On-yellow");
       }
       else if (nextChar == off)  // check for 'o'
       {                 
         ledState1 = LOW;
         ledState2 = LOW;
         Serial.println("leds Off");
       }   
       else if (nextChar == blue)  // check for 'b'
       {
         ledState3 = HIGH;
         ledState4 = LOW;
         Serial.println("led 3 On-blue");
       }
       else if (nextChar == green)  // check for 'g'
       {
         ledState3 = LOW;
         ledState4 = HIGH;
         Serial.println("led 4 On-green");
       }
       else if (nextChar == off2)  // check for 'f'
       {                 
         ledState3 = LOW;
         ledState4 = LOW;
         Serial.println("leds2 Off");
       }
       else if (nextChar == M1)  // check for '1'
       {
         ledState1 = HIGH;
         ledState2 = LOW;
         ledState3 = HIGH;
         ledState4 = LOW;
         Serial.println("led 1 & 3 On-red & blue");
       }
       else if (nextChar == M2)  // check for '2'
       {
         ledState1 = LOW;
         ledState2 = HIGH;
         ledState3 = LOW;
         ledState4 = HIGH;
         Serial.println("led 2 & 4 On-yellow & green");
       }
       else if (nextChar == Moff)  // check for 'a'
       {
         ledState1 = LOW;
         ledState2 = LOW;
         ledState3 = LOW;
         ledState4 = LOW;
         Serial.println("All leds Off");
       }
     
       digitalWrite(led1, ledState1);
       digitalWrite(led2, ledState2);
       digitalWrite(led3, ledState3);
       digitalWrite(led4, ledState4);
    }

}
utoto
 
Posts: 28
Joined: Mon Aug 23, 2010 3:08 pm

Re: ASCII LED control

by adafruit_support_bill on Tue Aug 24, 2010 3:11 pm

Code: Select all | TOGGLE FULL SIZE
{
  if (Serial.available())
   
    Serial.println(nextChar);
       


Don't you want to read the character here?

adafruit_support_bill
 
Posts: 78733
Joined: Sat Feb 07, 2009 10:11 am

Re: ASCII LED control

by utoto on Tue Aug 24, 2010 3:46 pm

I think so. It seems to me that it should be at the top so that if its condition(s) are not met, the rest will not happen. And then if its conditions are met, it will allow them to be executed.
utoto
 
Posts: 28
Joined: Mon Aug 23, 2010 3:08 pm

Re: ASCII LED control

by adafruit_support_bill on Tue Aug 24, 2010 4:14 pm

So replace:
Serial.println(nextChar);

with:
nextChar = Serial.read();

Also I think that the character counting logic is unnecessary. The outer loop will just read characters until it sees a 'passChar', then drop into the inner if/else-if logic.

adafruit_support_bill
 
Posts: 78733
Joined: Sat Feb 07, 2009 10:11 am

Re: ASCII LED control

by utoto on Tue Aug 24, 2010 4:29 pm

I am trying to take out the counter and have replaced as you said, but, keep getting errors.

Code: Select all | TOGGLE FULL SIZE
void loop()
{
  if (Serial.available())   
         
     nextChar = Serial.read();  // read just one character
       
           if (nextChar == red)  // check for 'r'
           {
             ledState1 = HIGH;
             ledState2 = LOW;
             Serial.println("led 1 On-red");
           }
           else if (nextChar == yellow)  // check for 'y'
utoto
 
Posts: 28
Joined: Mon Aug 23, 2010 3:08 pm

Re: ASCII LED control

by adafruit_support_bill on Tue Aug 24, 2010 4:49 pm

What happened to your outer loop check for 'passChar'?

Also, if you post the whole code, I can compile it here to check for syntax errors.

adafruit_support_bill
 
Posts: 78733
Joined: Sat Feb 07, 2009 10:11 am

Re: ASCII LED control

by utoto on Tue Aug 24, 2010 4:53 pm

Sorry. I thought I was doing as you instructed.

Here is what I had before.

Code: Select all | TOGGLE FULL SIZE
/*
* Turn 4 leds split into 2 sets on and off with special characters
* sent over the serial connection with only 1 led allowed on at a
* time. If  the 2nd led is commanded on, the first will be turned
* off prior.
* Now with 1st Place char addressing.
*/

int led1 = 13;      // led is hoooked up to this pin
int led2 = 12;      // led is hoooked up to this pin
int led3 = 11;      // led is hoooked up to this pin
int led4 = 10;      // led is hoooked up to this pin

char red = 'r';     // ascii LED 1 on
char off = 'o';     // ascii leds off
char yellow = 'y';  // ascii LED 2 on
char blue = 'b';    // ascii LED 3 on
char off2 = 'f';    // ascii leds2 off
char green = 'g';   // ascii LED 4 on
char M1 = '1';      // ascii LED 1&3 on
char Moff = 'a';    // ascii ledsAll off
char M2 = '2';      // ascii LED 2&4 on

int passChar = 'A';   // the first character of the password

int ledState1;
int ledState2;
int ledState3;
int ledState4;
char nextChar;

void setup()
{
   Serial.begin(9600);
   
   pinMode(led1, OUTPUT);
   digitalWrite(led1, LOW);
   ledState1 = LOW;
   
   pinMode(led2, OUTPUT);
   digitalWrite(led2, LOW);
   ledState2 = LOW;
   
   pinMode(led3, OUTPUT);
   digitalWrite(led3, LOW);
   ledState3 = LOW;
   
   pinMode(led4, OUTPUT);
   digitalWrite(led4, LOW);
   ledState4 = LOW;
}

void loop()
{
  if (Serial.available())   
         
     nextChar = Serial.read();  // read just one character
       
           if (nextChar == red)  // check for 'r'
           {
             ledState1 = HIGH;
             ledState2 = LOW;
             Serial.println("led 1 On-red");
           }
           else if (nextChar == yellow)  // check for 'y'
           {
             ledState1 = LOW;
             ledState2 = HIGH;
             Serial.println("led 2 On-yellow");
           }
           else if (nextChar == off)  // check for 'o'
           {                 
             ledState1 = LOW;
             ledState2 = LOW;
             Serial.println("leds Off");
           }   
           else if (nextChar == blue)  // check for 'b'
           {
             ledState3 = HIGH;
             ledState4 = LOW;
             Serial.println("led 3 On-blue");
           }
           else if (nextChar == green)  // check for 'g'
           {
             ledState3 = LOW;
             ledState4 = HIGH;
             Serial.println("led 4 On-green");
           }
           else if (nextChar == off2)  // check for 'f'
           {                 
             ledState3 = LOW;
             ledState4 = LOW;
             Serial.println("leds2 Off");
           }
           else if (nextChar == M1)  // check for '1'
           {
             ledState1 = HIGH;
             ledState2 = LOW;
             ledState3 = HIGH;
             ledState4 = LOW;
             Serial.println("led 1 & 3 On-red & blue");
           }
           else if (nextChar == M2)  // check for '2'
           {
             ledState1 = LOW;
             ledState2 = HIGH;
             ledState3 = LOW;
             ledState4 = HIGH;
             Serial.println("led 2 & 4 On-yellow & green");
           }
           else if (nextChar == Moff)  // check for 'a'
           {
             ledState1 = LOW;
             ledState2 = LOW;
             ledState3 = LOW;
             ledState4 = LOW;
             Serial.println("All leds Off");
           } 
       }
       digitalWrite(led1, ledState1);
       digitalWrite(led2, ledState2);
       digitalWrite(led3, ledState3);
       digitalWrite(led4, ledState4);
    }

}
utoto
 
Posts: 28
Joined: Mon Aug 23, 2010 3:08 pm

Re: ASCII LED control

by adafruit_support_bill on Tue Aug 24, 2010 4:59 pm

OK. One step at a time.

Code: Select all | TOGGLE FULL SIZE
void loop()
{
  if (Serial.available())   
         
     nextChar = Serial.read();  // read just one character


Your 'if' needs curly braces '{}' to define its scope. You missed the opening brace '{' before 'nextChar ='. So the closing brace before the 'digitalWrite' is unbalanced.

Code: Select all | TOGGLE FULL SIZE
       }
       digitalWrite(led1, ledState1);
       digitalWrite(led2, ledState2);
       digitalWrite(led3, ledState3);
       digitalWrite(led4, ledState4);

adafruit_support_bill
 
Posts: 78733
Joined: Sat Feb 07, 2009 10:11 am

Re: ASCII LED control

by utoto on Tue Aug 24, 2010 5:26 pm

How should this layout?
If I was giving these instructions to to somebody to execute when certain characters were displayed to them, I would preface the instructions with "you will know when a command is meant for you because the first character in the message will be an "addressing character". If you see anything but "this" or "that" in the first position of the message then ignore the command because it is not meant for you.

This ultimately is what I am trying to do.

I hope this helps.
utoto
 
Posts: 28
Joined: Mon Aug 23, 2010 3:08 pm

Re: ASCII LED control

by adafruit_support_bill on Tue Aug 24, 2010 5:32 pm

I understand that part. But you need to understand the syntax of an 'if' statement to make it compile.

Get the last code you posted to compile properly, and we can add in the logic for the "addressing character".

adafruit_support_bill
 
Posts: 78733
Joined: Sat Feb 07, 2009 10:11 am

Re: ASCII LED control

by utoto on Tue Aug 24, 2010 5:44 pm

OK. Here it is.

It has the counter junk back in it now as well since I am not sure of what to keep and what not.

BTW, thank you very much for your help. I am learning more than it may seem. I do appreciate it.

Code: Select all | TOGGLE FULL SIZE
/*
* Turn 4 leds split into 2 sets on and off with special characters
* sent over the serial connection with only 1 led allowed on at a
* time. If  the 2nd led is commanded on, the first will be turned
* off prior.
* Now with 1st Place char address.
*/

int led1 = 13;      // led is hoooked up to this pin
int led2 = 12;      // led is hoooked up to this pin
int led3 = 11;      // led is hoooked up to this pin
int led4 = 10;      // led is hoooked up to this pin

char red = 'r';     // ascii LED 1 on
char off = 'o';  // ascii leds off
char yellow = 'y';  // ascii LED 2 on
char blue = 'b';  // ascii LED 3 on
char off2 = 'f';  // ascii leds2 off
char green = 'g';  // ascii LED 4 on
char M1 = '1';  // ascii LED 1&3 on
char Moff = 'a';  // ascii ledsAll off
char M2 = '2';  // ascii LED 2&4 on

int passChar = 'A';   // the first character of the password
int charCount;

int ledState1;
int ledState2;
int ledState3;
int ledState4;
char nextChar;

void setup()
{
   Serial.begin(9600);
   
   pinMode(led1, OUTPUT);
   digitalWrite(led1, LOW);
   ledState1 = LOW;
   
   pinMode(led2, OUTPUT);
   digitalWrite(led2, LOW);
   ledState2 = LOW;
   
   pinMode(led3, OUTPUT);
   digitalWrite(led3, LOW);
   ledState3 = LOW;
   
   pinMode(led4, OUTPUT);
   digitalWrite(led4, LOW);
   ledState4 = LOW;
   
   charCount = 0;
}

void loop()
{
  if (Serial.available())
   
    Serial.println(nextChar);
    {   
    if (charCount == 0) // check for the first char of the pwd...
    {     
       if (nextChar == passChar)
       {
         charCount = 1;  // got it, so update the character counter
       }
       else
       {
         charCount = 0;  // wrong character, so reset count to zero
         
         {
           nextChar = Serial.read();  // read just one character
         }
           if (nextChar == red)  // check for 'r'
           {
             ledState1 = HIGH;
             ledState2 = LOW;
             Serial.println("led 1 On-red");
           }
           else if (nextChar == yellow)  // check for 'y'
           {
             ledState1 = LOW;
             ledState2 = HIGH;
             Serial.println("led 2 On-yellow");
           }
           else if (nextChar == off)  // check for 'o'
           {                 
             ledState1 = LOW;
             ledState2 = LOW;
             Serial.println("leds Off");
           }   
           else if (nextChar == blue)  // check for 'b'
           {
             ledState3 = HIGH;
             ledState4 = LOW;
             Serial.println("led 3 On-blue");
           }
           else if (nextChar == green)  // check for 'g'
           {
             ledState3 = LOW;
             ledState4 = HIGH;
             Serial.println("led 4 On-green");
           }
           else if (nextChar == off2)  // check for 'f'
           {                 
             ledState3 = LOW;
             ledState4 = LOW;
             Serial.println("leds2 Off");
           }
           else if (nextChar == M1)  // check for '1'
           {
             ledState1 = HIGH;
             ledState2 = LOW;
             ledState3 = HIGH;
             ledState4 = LOW;
             Serial.println("led 1 & 3 On-red & blue");
           }
           else if (nextChar == M2)  // check for '2'
           {
             ledState1 = LOW;
             ledState2 = HIGH;
             ledState3 = LOW;
             ledState4 = HIGH;
             Serial.println("led 2 & 4 On-yellow & green");
           }
           else if (nextChar == Moff)  // check for 'a'
           {
             ledState1 = LOW;
             ledState2 = LOW;
             ledState3 = LOW;
             ledState4 = LOW;
             Serial.println("All leds Off");
           } 
       }
     
       digitalWrite(led1, ledState1);
       digitalWrite(led2, ledState2);
       digitalWrite(led3, ledState3);
       digitalWrite(led4, ledState4);
    }

}
utoto
 
Posts: 28
Joined: Mon Aug 23, 2010 3:08 pm

Please be positive and constructive with your questions and comments.