Blink until.......

Hi everyone this is my first project and also my first post so i'll try to be as clear as i possibly can ( i realize that's important)

so simple question (i hope)

im sending data from an android tablet to an arduino Nano V3.0 via an HC-05 Module and al is well so far but i would like for a blinker (in this case "leftblink" to... well blink untill i send another character on the android tablet to get it to stop, i will then also do the same with the rightblink (obviously)

im emulating a cars turn signal

Here is my code:
(i apologize if its not pretty i just starter coding about 1 month ago)

any help is greatly appreciated in advance.

char val; // variable to receive data from the serial port

int Leftblink = 5;
int Rightblink = 6;
int Ignition = 7;
int Relay4 = 8;
int Party_lights = 9;
int Lights = 10;
int Fan_low = 11;
int Fan_high = 12;

                
void setup() 
{
  Serial.begin(9600);         
  pinMode(Leftblink, OUTPUT);       
  pinMode(Rightblink, OUTPUT);   
  pinMode(Ignition, OUTPUT);   
  pinMode(Relay4, OUTPUT);   
  pinMode(Party_lights, OUTPUT);   
  pinMode(Lights, OUTPUT);   
  pinMode(Fan_low, OUTPUT);   
  pinMode(Fan_high, OUTPUT);   
  digitalWrite(Leftblink,HIGH);
  digitalWrite(Rightblink,HIGH);
  digitalWrite(Ignition,HIGH);
  digitalWrite(Relay4,HIGH);
  digitalWrite(Party_lights,HIGH);
  digitalWrite(Lights,HIGH);
  digitalWrite(Fan_low,HIGH);
  digitalWrite(Fan_high,HIGH);
    
}

void loop()
{
  if( Serial.available() )       // if data is available to read
  {
    ;
  }
  val = Serial.read();         // read it and store it in 'val'
     


//Left Blinker
             
    {   if(val == 'A') {                     
      digitalWrite(Leftblink, LOW); 
          Serial.println("lefton");
                        delay(500);
      digitalWrite(Leftblink, HIGH); 
          Serial.println("leftoff");a
                        delay(500);
       digitalWrite(Leftblink, LOW); 
          Serial.println("lefton");
                        delay(500);
      digitalWrite(Leftblink, HIGH); 
          Serial.println("leftoff");
                        delay(500);
       digitalWrite(Leftblink, LOW); 
          Serial.println("lefton");
                        delay(500);
      digitalWrite(Leftblink, HIGH); 
          Serial.println("leftoff");
                        delay(500);
     digitalWrite(Leftblink, LOW); 
          Serial.println("lefton");
                        delay(500);
      digitalWrite(Leftblink, HIGH); 
          Serial.println("leftoff");
                        delay(500);
       digitalWrite(Leftblink, LOW); 
          Serial.println("lefton");
                        delay(500);
      digitalWrite(Leftblink, HIGH); 
          Serial.println("leftoff");
                        delay(500);
  }
  else if(val == 'a'){
         digitalWrite(Leftblink, HIGH); 
          Serial.println("leftoff");
}

  
      
    if(val == 'C')  {           
      digitalWrite(Fan_high, LOW); 
      Serial.println("Fans Are High");
      digitalWrite(Fan_low, HIGH);  
    }           
    }          if(val == 'D') {                     
      digitalWrite(Fan_low, LOW); 
       Serial.println("Fans Are Low");
      digitalWrite(Fan_high, HIGH); 
}      

         else if(val == 'c')       
         {
      digitalWrite(Fan_low, HIGH); 
      digitalWrite(Fan_high, HIGH); 
         


         { if(val == 'E')                      
      digitalWrite(Lights, LOW); 
         else if(val == 'e')       
      digitalWrite(Lights, HIGH);   

          if(val == 'F')                      
      digitalWrite(Party_lights, LOW); 
         else if(val == 'f')       
      digitalWrite(Party_lights, HIGH);   

          if(val == 'Z')  
          {                    
      digitalWrite(Leftblink, LOW); 
      digitalWrite(Rightblink, LOW); 
      digitalWrite(Ignition, LOW); 
      digitalWrite(Relay4, LOW); 
      digitalWrite(Party_lights, LOW); 
      digitalWrite(Lights, LOW); 
      digitalWrite(Fan_low, LOW); 
      digitalWrite(Fan_high, LOW); 
          }
         else if(val == 'z')       
         {
      digitalWrite(Leftblink, HIGH); 
      digitalWrite(Rightblink, HIGH); 
      digitalWrite(Ignition, HIGH); 
      digitalWrite(Relay4, HIGH); 
      digitalWrite(Party_lights, HIGH); 
      digitalWrite(Lights, HIGH); 
      digitalWrite(Fan_low, HIGH); 
      digitalWrite(Fan_high, HIGH); 
         }
      
  }                            
         }
}
if( Serial.available() )       // if data is available to read
  {
    ;
  }
  val = Serial.read();         // read it and store it in 'val'

If there is something to read, do nothing.
But read something anyway.

What is your code not doing?
Also, it would be a lot more readable if you had a few more comments. YOU may know what Relay4 does, but anyone reading your code won't.

Also, common practice but not a requirement is that variables are lower case and constants are UPPER case. When appropriate, variables should be formatted in "Camel Case": For example:

int leftBlink = 5;
int rightBlink = 6;
int ignition = 7;
int relay4 = 8;
int partyLights;

Some people like to use underscores with Camel:

int party_Lights;

You can use anything for variables that you are comfortable with, but if others are going to read your code, it helps to format it as we're used to seeing.

SteveMann:
What is your code not doing?
Also, it would be a lot more readable if you had a few more comments. YOU may know what Relay4 does, but anyone reading your code won’t.

Also, common practice but not a requirement is that variables are lower case and constants are UPPER case. When appropriate, variables should be formatted in “Camel Case”: For example:

int leftBlink = 5;

int rightBlink = 6;
int ignition = 7;
int relay4 = 8;
int partyLights;



Some people like to use underscores with Camel:


int party_Lights;




You can use anything for variables that you are comfortable with, but if others are going to read your code, it helps to format it as we're used to seeing.

true…
will do that. thanx for your input.

Here's a snippet you can try out for the left and right blinkers only.

It doesn't use delay(...) because you have other things going on and can't have the processor blocked doing basically nothing for the blink timing. For this I use millis(...) techniques. I have a single boolean flag that toggles true-false-true-false... every 500mS, all the time. When a blinker, left or right, is "on" I "connect" the state of that toggling flag to the pin.

You can expand on this for your other functions and make changes as needed to support what looks like all-on and all-off functionality (Z and z).

const uint8_t Leftblink = 5;
const uint8_t Rightblink = 6;

bool
    bBlinkLeft,
    bBlinkRight,
    bBlinkFlag;
               
void setup()
{
    Serial.begin(9600);         
    pinMode(Leftblink, OUTPUT);       
    pinMode(Rightblink, OUTPUT);   
    
    digitalWrite(Leftblink,HIGH);
    digitalWrite(Rightblink,HIGH);
   
    bBlinkLeft = false;
    bBlinkRight = false;
    bBlinkFlag = false;
    
}//setup

void loop()
{
    //toggle the common blink flag every 500mS
    BlinkFlag();
    
    if( Serial.available() )
    {
        char val = Serial.read();
        switch( val )
        {
            case    'A':
                bBlinkLeft = true;
                bBlinkRight = false;
                Serial.println("lefton");
            break;

            case    'a':
                bBlinkLeft = false;
                Serial.println("leftoff");
            break;
            
            case    'B':
                bBlinkRight = true;
                bBlinkLeft = false;
                Serial.println("righton");
            break;

            case    'b':
                bBlinkRight = false;
                Serial.println("rightoff");
            break;
            
        }//switch

    }//if

    //
    //...
    //

    //effectively:
    //  if( bBlinkLeft == true )    //if left blinker active
    //  {
    //      if( bBlinkFlag == true )            //if blink flag is true, write low to turn on
    //          digitalWrite( Leftblink, LOW );
    //      else
    //          digitalWrite( Leftblink, HIGH );    //flag is high; turn off by writing high
    //  }
    //  else
    //      digitalWrite( Leftblink, HIGH );    //left blinker is off; turn off by writing high                
    digitalWrite( Leftblink, (bBlinkLeft == true) ? ((bBlinkFlag == true) ? LOW:HIGH) : HIGH );
    digitalWrite( Rightblink, (bBlinkRight == true) ? ((bBlinkFlag == true) ? LOW:HIGH) : HIGH );
    
}//loop

void BlinkFlag( void )
{
    static uint32_t
        timeBlink = 0;
    uint32_t
        timeNow = millis();

    //every 500mS...
    if( (timeNow - timeBlink) >= 500ul )
    {
        //toggle the state of the common (same used for both left and right) flag
        //^ operation is essentially same as:
        //  if( bBlinkFlag == true )
        //      bBlinkFlag = false;
        //  else
        //      bBlinkFlag = true;
        bBlinkFlag ^= true;

        //save the millis value for next toggle time
        timeBlink = timeNow;

    }//if

}//BlinkFlag

Re: Comments - as noted above comments are good - not just for other people but for yourself - having comments also helps you to understand the program - if you go to change the program in say a month or two having good comments will help YOU

good luck

saildude:
Re: Comments - as noted above comments are good - not just for other people but for yourself - having comments also helps you to understand the program - if you go to change the program in say a month or two having good comments will help YOU

good luck

A month?? I forget what I did in a week.

Here’s a way to do blinking… (With no blocking!)

I only did the left blinker. If you like this approach, you can have a go at adding the logic to run the right blinker.

#include <blinker.h>
#include <mechButton.h>

blinker     leftBlink(13);    // Our test LED we use 13, you used 5
blinker     rightBlink(6);    // The right LED blinker. You can use this later.
mechButton  leftButton(2);    // Hook a button from pin 2 to ground. This is our test button.
bool        leftBlinking;     // We save the state if the left blinker here.
bool        rightBlinking;    // Later you may want to save the state of the right blinker here.


void setup(void) {

   leftBlink.setOnOff(false);    // Setup the left blinker. Make sure its off.
   leftBlinking = false;         // Note that it's off.
   rightBlink.setOnOff(false);   // Setup the right blinker for later. Make sure its off. 
   rightBlinking = false;        // Note that it's off.
}


void loop(void) {

   idle();                                   // Let things like background blinker run.
   if (!leftButton.trueFalse()) {            // If left blinker button has been hit..
      if (!leftBlinking) {                   // If we are NOT blinking the left..
         leftBlink.setOnOff(true);           // Start the left blinking.
         leftBlinking = true;                // Note that it's blinking
         while(!leftButton.trueFalse()) {    // While the lazy usewr is holding down the button.
            sleep(10);                       // Let the background stuff run for awhile while we wait.
         }
      } else {                               // Else, we ARE blinking the left..
         leftBlink.setOnOff(false);          // Shut off the left blinker.
         leftBlinking = false;               // Note we are NOT blinking the left.
         while(!leftButton.trueFalse()) {    // And again, the user is asleep holding down the button..
            sleep(10);                       // We hold this thread while letting everything else run.
         }
      }
   }
}

Install LC_baseTools form the library manager to get this to compile.

Good luck!

-jim lee

Blackfin:
Here's a snippet you can try out for the left and right blinkers only.

It doesn't use delay(...) because you have other things going on and can't have the processor blocked doing basically nothing for the blink timing. For this I use millis(...) techniques. I have a single boolean flag that toggles true-false-true-false... every 500mS, all the time. When a blinker, left or right, is "on" I "connect" the state of that toggling flag to the pin.

You can expand on this for your other functions and make changes as needed to support what looks like all-on and all-off functionality (Z and z).

const uint8_t Leftblink = 5;

const uint8_t Rightblink = 6;

bool
   bBlinkLeft,
   bBlinkRight,
   bBlinkFlag;
             
void setup()
{
   Serial.begin(9600);        
   pinMode(Leftblink, OUTPUT);      
   pinMode(Rightblink, OUTPUT);  
   
   digitalWrite(Leftblink,HIGH);
   digitalWrite(Rightblink,HIGH);
 
   bBlinkLeft = false;
   bBlinkRight = false;
   bBlinkFlag = false;
   
}//setup

void loop()
{
   //toggle the common blink flag every 500mS
   BlinkFlag();
   
   if( Serial.available() )
   {
       char val = Serial.read();
       switch( val )
       {
           case    'A':
               bBlinkLeft = true;
               bBlinkRight = false;
               Serial.println("lefton");
           break;

case    'a':
               bBlinkLeft = false;
               Serial.println("leftoff");
           break;
           
           case    'B':
               bBlinkRight = true;
               bBlinkLeft = false;
               Serial.println("righton");
           break;

case    'b':
               bBlinkRight = false;
               Serial.println("rightoff");
           break;
           
       }//switch

}//if

//
   //...
   //

//effectively:
   //  if( bBlinkLeft == true )    //if left blinker active
   //  {
   //      if( bBlinkFlag == true )            //if blink flag is true, write low to turn on
   //          digitalWrite( Leftblink, LOW );
   //      else
   //          digitalWrite( Leftblink, HIGH );    //flag is high; turn off by writing high
   //  }
   //  else
   //      digitalWrite( Leftblink, HIGH );    //left blinker is off; turn off by writing high                
   digitalWrite( Leftblink, (bBlinkLeft == true) ? ((bBlinkFlag == true) ? LOW:HIGH) : HIGH );
   digitalWrite( Rightblink, (bBlinkRight == true) ? ((bBlinkFlag == true) ? LOW:HIGH) : HIGH );
   
}//loop

void BlinkFlag( void )
{
   static uint32_t
       timeBlink = 0;
   uint32_t
       timeNow = millis();

//every 500mS...
   if( (timeNow - timeBlink) >= 500ul )
   {
       //toggle the state of the common (same used for both left and right) flag
       //^ operation is essentially same as:
       //  if( bBlinkFlag == true )
       //      bBlinkFlag = false;
       //  else
       //      bBlinkFlag = true;
       bBlinkFlag ^= true;

//save the millis value for next toggle time
       timeBlink = timeNow;

}//if

}//BlinkFlag




This loops like something that i can handle, 
Also thank you for your patience, like i mentioned this is my FIRST proyect and i mada several mistakes i know... (commenting is one of them)

i'll try this tomorrow.