ASCII LED control

For Adafruit customers who seek help with microcontrollers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
utoto
 
Posts: 28
Joined: Mon Aug 23, 2010 3:08 pm

Re: ASCII LED control

Post by utoto »

At first I thought that would be best, but, could not come up with a decent way to execute it since the digitalWrites only respond to ledState.

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

Re: ASCII LED control

Post by utoto »

OK.

I tried several times and keep coming up wrong. This is the latest attempt. Any help is appreciated. :)

Code: Select all

    /*
    * Mega
    * 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 = 12;      // led is hooked up to this pin
    int led2 = 11;      // led is hooked up to this pin
    int led3 = 10;      // led is hooked up to this pin
    int led4 = 9;       // led is hooked up to this pin
    int ledState1;
    int ledState2;
    int ledState3;
    int ledState4;
    int pswdChar = 'm';   // the first character of the password

    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
    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()
{                          // open main thought
  if (Serial.available())
   {                       // open sub thought
    char check_password = Serial.read();
    Serial.println(pswdChar);
    
    if (check_password == pswdChar) // does the password match?
    {                                 // open sub-2 thought             
             nextChar = Serial.read();      // read just one character          
             {                                         // open a sub-3 thought
             if (nextChar == red)             // check for 'r'
               {                                       // open a sub-4 thought
                 ledState1 = HIGH;
                 ledState2 = LOW;
                 Serial.println("led 1 On-red");
               }                                          // close sub-4 thought
               else if (nextChar == yellow)  // check for 'y'
               {                                          // open a sub-4 thought
                 ledState1 = LOW;
                 ledState2 = HIGH;
                 Serial.println("led 2 On-yellow");
               }                                        // close sub-4 thought
               else if (nextChar == off)     // check for 'o'
               {                                       // open a sub-4 thought                 
                 ledState1 = LOW;
                 ledState2 = LOW;
                 Serial.println("leds 1 & 2 Off");
               }                                         // close sub-4 thought   
               else if (nextChar == blue)    // check for 'b'
               {                                        // open a sub-4 thought
                 ledState3 = HIGH;
                 ledState4 = LOW;
                 Serial.println("led 3 On-blue");
               }                                          // close sub-4 thought
               else if (nextChar == green)   // check for 'g'
               {                                         // open a sub-4 thought
                 ledState3 = LOW;
                 ledState4 = HIGH;
                 Serial.println("led 4 On-green");
               }                                        // close sub-4 thought
               else if (nextChar == off2)    // check for 'f'
               {                                        // open a sub-4 thought                 
                 ledState3 = LOW;
                 ledState4 = LOW;
                 Serial.println("leds 3 & 4 Off");
               }                                        // close sub-4 thought
               else if (nextChar == M1)      // check for '1'
               {                                        // open a sub-4 thought
                 ledState1 = HIGH;
                 ledState2 = LOW;
                 ledState3 = HIGH;
                 ledState4 = LOW;
                 Serial.println("led 1 & 3 On-red & blue");
               }                                        // close sub-4 thought
               else if (nextChar == M2)      // check for '2'
               {                                       // open a sub-4 thought
                 ledState1 = LOW;
                 ledState2 = HIGH;
                 ledState3 = LOW;
                 ledState4 = HIGH;
                 Serial.println("led 2 & 4 On-yellow & green");
               }                                        // close sub-4 thought
               else if (nextChar == Moff)   // check for 'a'
               {                                       // open a sub-4 thought
                 ledState1 = LOW;
                 ledState2 = LOW;
                 ledState3 = LOW;
                 ledState4 = LOW;
                 Serial.println("All leds Off");
               }                           // close sub-4 thought 
            }                              // close sub-3 thought     
         }                                 // close sub-2 thought
          
           digitalWrite(led1, ledState1);
           digitalWrite(led2, ledState2);
           digitalWrite(led3, ledState3);
           digitalWrite(led4, ledState4);
           {
           if (ledState1 == HIGH)           
             {
             delay(1000);
             ledState1 = LOW;
             }
           
           if (ledState2 == HIGH)
             {
             delay(1000); 
             ledState2 = LOW;
             }
           
           if (ledState3 == HIGH)
             {
             delay(1000);
             ledState3 = LOW;
             }
           
           if (ledState4 == HIGH)
             {
             delay(1000);
             ledState4 = LOW;
             }
           }
        
      }                                    // close sub thought
    }                                      // close main thought

User avatar
adafruit_support_bill
 
Posts: 88088
Joined: Sat Feb 07, 2009 10:11 am

Re: ASCII LED control

Post by adafruit_support_bill »

Please be specific about the problem. What do you expect it to do? And how exactly does it not do what you expect?

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

Re: ASCII LED control

Post by utoto »

I am trying to add a LED delay so that whenever one is sent high, it will have a delay and then be sent low. You mentioned that it would be best to put it at the end with the dw's, so I have been trying different ways to accomplish this to no avail.

It seems that it does delay, somewhat. I increased my delay time to get a better feel.

I send "m1" and 2 LEDs go high. Nice.
I send "ma" within 2sec, I must wait for them to go low.
I send "m1" and wait 2-3 sec, then send "ma", they go low immediately.

I send "m1" and quickly send "m2".
"m1" stays high for 2sec then switches off and "m2" goes high.

I send "m1" then "m2" then "ma".
"m1" goes high, 2 sec later "m1" goes low for "m2 to go high, the 2 sec later "ma" is able to send "m2" low.

I believe what is happening here is that a delay attributed to a command is not being canceled for the delay attributed to the opposite command which results in multiple delays fully recognized instead of just the last one. Also, it keeps "ma" from sending the current "high" choice low immediately.

User avatar
adafruit_support_bill
 
Posts: 88088
Joined: Sat Feb 07, 2009 10:11 am

Re: ASCII LED control

Post by adafruit_support_bill »

The 'delay()' function stops the processor from doing anything else until the delay is finished. That includes processing any other commands.

You can't 'cancel' a delay by sending another command, because that command will not be processed until the delay is finished.

If you want to have 'interruptable' delays, you should look into the various timer libraries for the Arduino. http://arduino.cc/en/Reference/Libraries

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

Re: ASCII LED control

Post by utoto »

But can't it be an "if" where if all is fine and good the delay will run and time out and everybody is happy, but, "if else" another command arrives during the delay having the LED in question, then the new command will be executed with its own delay.

I am probably not saying this the appropriate way, so, I am just trying to get my idea across.

Thanks.

User avatar
adafruit_support_bill
 
Posts: 88088
Joined: Sat Feb 07, 2009 10:11 am

Re: ASCII LED control

Post by adafruit_support_bill »

No. *Nothing* happens during a delay. Your Arduino will not be able to receive any input until the delay completes.

You can use an interrupt-based timer such as the ones in the libraries suggested earlier.

OR you can break your delay up into smaller parts. Instead of one delay for 1000 ms, you can break it up into 10 delays of 100 ms each. In your loop, you can check for inputs between delays.

Locked
Please be positive and constructive with your questions and comments.

Return to “Microcontrollers”