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.

ASCII LED control

by utoto on Mon Aug 23, 2010 5:10 pm

Hi,
1st post here.
I am in need of help with my LED code controlled with ASCII via serial @ 9600.
I attached my project(some parts taken from other kind contributors).
I am trying to get a character to enable and another to disable the LED.
Then I want to expand to expand to use 2-3 characters for each.

Also, I have been trying to copy my Arduino IDE code and paste it here for review. But, this is not working so well. You may have already noticed that I have no code displayed.

Please, help?
utoto
 
Posts: 28
Joined: Mon Aug 23, 2010 3:08 pm

Re: ASCII LED control

by adafruit_support_bill on Mon Aug 23, 2010 5:14 pm

From the IDE, you should be able to click on the "Edit" menu and choose "Select All". Then click on "Edit" again and choose "Copy".

You should then be able to paste it into the forum post.

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

Re: ASCII LED control

by utoto on Mon Aug 23, 2010 7:44 pm

Agreed. Yet, I cannot. Perhaps a special window is needed so that the code is not misunderstood by the computer???
utoto
 
Posts: 28
Joined: Mon Aug 23, 2010 3:08 pm

Re: ASCII LED control

by utoto on Mon Aug 23, 2010 8:45 pm

OK! Got it! Ubuntu 10.04 and Java! Who knew?



/*
* Turn an led on and off with special characters
* sent over the serial connection.
*/

int ledPin = 10; // led is hoooked up to this pin
int on = 'y'; // ascii LED on
int off = 'n'; // ascii led off

int ledState;
int nextChar;

void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
ledState = LOW;
}

void loop(){
if (Serial.available()) {
nextChar = Serial.read();
Serial.println(nextChar);
if (nextChar == on) {
if (ledState == LOW) {
ledState = HIGH;
Serial.println("led On");
} else{
Serial.println(nextChar);
if (nextChar == off) {
if (ledState == HIGH) {
ledState = LOW;
Serial.println("led Off");
}
}
}
digitalWrite(ledPin, ledState);

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

Re: ASCII LED control

by franklin97355 on Mon Aug 23, 2010 11:13 pm

OK, as you can see, if you don't wrap your code with "code" tags it takes out all the indentation making the code hard to read (in bigger code snippets)

franklin97355
 
Posts: 21582
Joined: Mon Apr 21, 2008 2:33 pm
Location: Lacomb, OR.

Re: ASCII LED control

by westfw on Tue Aug 24, 2010 12:17 am

You don't have your braces in quite the right places. See // **** below
Code: Select all | TOGGLE FULL SIZE
  if (Serial.available()) {
    nextChar = Serial.read();
    Serial.println(nextChar, BYTE);
    if (nextChar == on) {
      if (ledState == LOW) {
        ledState = HIGH;
        Serial.println("led On");
      }
      else{         // *****   This else matches "if (ledState == LOW)",
                    // ******   not "if (nextChar == on)" *****
        Serial.println(nextChar, BYTE);
        if (nextChar == off) {
          if (ledState == HIGH) {
            ledState = LOW;
            Serial.println("led Off");
          }
        }
      }
      digitalWrite(ledPin, ledState);
    }
  }

westfw
 
Posts: 1721
Joined: Fri Apr 27, 2007 1:01 pm
Location: SF Bay area

Re: ASCII LED control

by utoto on Tue Aug 24, 2010 10:19 am

Yep. That was it! My thanks westfw. And, yes, franklin97355, I will have to learn how to do this. Thanks for letting me know.

Code: Select all | TOGGLE FULL SIZE
   /*
* Turn 2 leds 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

char red = 'r';  // ascii LED 1 on
char off = 'o';  // ascii led off
char yellow = 'y';  // ascii LED 2 on

int ledState1;
int ledState2;
char nextChar;

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

void loop()
{
  if (Serial.available())
    {   
       nextChar = Serial.read();
       if (nextChar == red)
       {
           if (ledState1 == LOW)
           if (ledState2 == LOW)
           {
               ledState1 = HIGH;
               Serial.println("led 1 On-red");
           }
       }
       else
       { 
           nextChar = Serial.read();
           if (nextChar == yellow)
          {
               if (ledState2 == LOW)
               if (ledState1 == LOW)
               {
                   ledState2 = HIGH;
                   Serial.println("led 2 On-yellow");
               }
           }
       }
       else
       {                 
           nextChar = Serial.read();
           if (nextChar == off)
           {
               {
                   ledState1 = LOW;
                   ledState2 = LOW;
                   Serial.println("leds Off");
               }
           }
       }
       digitalWrite(led1, ledState1);
       digitalWrite(led2, ledState2);
    }
   
}


Ha! It took me a couple of times, but, I finally found out how! Nice!

Why am I getting errors for bracing again? I have 10 opens and 10 closes. If I move the last "close" up in front of the 2nd "else", it does fine, but, the program doesn't work right.
utoto
 
Posts: 28
Joined: Mon Aug 23, 2010 3:08 pm

Re: ASCII LED control

by adafruit_support_bill on Tue Aug 24, 2010 11:01 am

There is no 'if' for your second 'else'.

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

Re: ASCII LED control

by utoto on Tue Aug 24, 2010 11:43 am

But, I have no "if" at my 2nd "else". They all are above or below.?.
utoto
 
Posts: 28
Joined: Mon Aug 23, 2010 3:08 pm

Re: ASCII LED control

by adafruit_support_bill on Tue Aug 24, 2010 12:10 pm

But, I have no "if" at my 2nd "else". They all are above or below.?.


I'm not sure what you mean by that. You can't have an 'else' without a matching 'if'. If there are more than two cases, you can use an 'else if' in the middle.

Nevertheless, based on the comments at the beginning, your loop is more complicated than it needs to be. I think this is what you are trying to implement:

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'
       { 
         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");
       }
       digitalWrite(led1, ledState1);
       digitalWrite(led2, ledState2);
    }
   
}

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

Re: ASCII LED control

by utoto on Tue Aug 24, 2010 1:08 pm

Perhaps it is that I am ignorant in my answer as to of the meaning of your question. I am quite green and trying to learn. I have been learning from "Making Things Talk" and Arduino tutorials. I am having difficulty (obviously) with proper syntax. (is that the right word?)
But, here is the error message I got:

Code: Select all | TOGGLE FULL SIZE
 In function ‘void loop()’:
error: expected `}' before ‘else'


I did try as you said. I do not think, however, that I did it properly. I am not familiar yet with the lingo.

Code: Select all | TOGGLE FULL SIZE
   }
       else if
       {                 
           nextChar = Serial.read();
           if (nextChar == off)
           {
               {
                   ledState1 = LOW;
                   ledState2 = LOW;
                   Serial.println("leds Off");
               }
           }
       }
       digitalWrite(led1, ledState1);
       digitalWrite(led2, ledState2);
    }
   
}
utoto
 
Posts: 28
Joined: Mon Aug 23, 2010 3:08 pm

Re: ASCII LED control

by adafruit_support_bill on Tue Aug 24, 2010 1:31 pm

The 'else if' should go in the middle. The syntax is:

Code: Select all | TOGGLE FULL SIZE
if(<condition 1>)
  {
  // process condition 1
  }
else if(<condition 2>)
  {
  // process condition 2
  }
else if(<condition 3>)// you can have as many 'else if' clauses as you need.
  {
  // process condition 3
  }
else  // none of the above cases
  {
  // process everything else (optional)
  }


There are a few other problems with the logic as you have it. I don't think it will work as you intended based on your header comment. I added some comments to your loop. Compare this with the loop code in my previous post.

Code: Select all | TOGGLE FULL SIZE
void loop()
{
  if (Serial.available())
    {   
       nextChar = Serial.read();
       if (nextChar == red)
       {
           if (ledState1 == LOW)  // this statement does nothing.
           if (ledState2 == LOW)  // this will prevent you from turning on red if yellow is already on
           {
               ledState1 = HIGH;
               Serial.println("led 1 On-red");
           }
       }
       else
       { 
           nextChar = Serial.read();  // this will consume another character from the input stream.
                                      // that means that you will have to type 2 characters for the 'y'
                                      // to be recognized.
           if (nextChar == yellow)
          {
               if (ledState2 == LOW)  // this statement does nothing
               if (ledState1 == LOW)  // this prevents yellow from turning on if red is already on
               {
                   ledState2 = HIGH;
                   Serial.println("led 2 On-yellow");
               }
           }
       }
       else  // there is no matching 'if' for this else
       {                 
           nextChar = Serial.read();  // this consumes another character
           if (nextChar == off)
           {
               {
                   ledState1 = LOW;
                   ledState2 = LOW;
                   Serial.println("leds Off");
               }
           }
       }
       digitalWrite(led1, ledState1);
       digitalWrite(led2, ledState2);
    }
   
}

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

Re: ASCII LED control

by utoto on Tue Aug 24, 2010 1:48 pm

Ah yes. I think I had my "else" alone and needed to be "else if" and then separate from my instructions.
I do not get all of it. But, I am getting a better feel for how it should look. I finally tried your code. Nice! Thank you. I did not want to copy it at first. But, doing so and seeing it work, then comparing it with mine does help immensely.

My thanks.

If I wanted to add in say a single character password prefix, would it be best to have first under " void loop" or at the end? I imagine since it is a main factor for the module whether to even look at a 2nd char as a command that it should be at the top.
utoto
 
Posts: 28
Joined: Mon Aug 23, 2010 3:08 pm

Re: ASCII LED control

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

So, I tried to build on it by doubling the same code, but, tried to get it to behave as 2 separate thoughts.
For example:
perhaps red could be on and green at the same time, or, yellow and blue, but, still not red and yellow or blue and green.

I tried it but, again get a brace error at the last one. Doesn't this tell me to look just above the highlighted error section as to where to fix?

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 led off
char yellow = 'y';  // ascii LED 2 on
char blue = 'b';  // ascii LED 1 on
char off2 = 'f';  // ascii led off
char green = 'g';  // ascii LED 2 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;
}

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");
       }
       digitalWrite(led1, ledState1);
       digitalWrite(led2, ledState2);
    }
    else if (nextChar == blue)  // check for 'b'
       {
         ledState3 = HIGH;
         ledState4 = LOW;
         Serial.println("led 3 On-blue");
       }
       else if (nextChar == green)  // check for 'green'
       {
         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");
       }
       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:08 pm

Right. Sorry about that. I copied and pasted and then changed the bottom half and forgot to modify the upper half as well.

This works correctly now. But, why(as to the syntax of the lower section)?

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

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 'green'
       {
         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");
       }
       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.