Blink2 leds different times

Hello, Im very newbie in this parts, i would like to state my question as follows:

Let's say I send a 3char message trough the serial port, e.g '123'.

What I need to do is, process the first '1' then it must process the other chars, and reading the second char, in this case '2' means that i want to blink a red led 2 times, and the 3 (last char) means that i want to blink yellow 3 times....

I try to do some coding to get the chars, and i manage to get them like this:

while (Serial.available() < 1 )
 {;}
msg[0]=Serial.read();

while (Serial.available() < 1 )
 {;}
msg[1]=Serial.read();

while (Serial.available() < 1 )
 {;}
msg[2]=Serial.read();


msg[0] = msg[0]-48;
msg[1] = msg[1]-48;
msg[2] = msg[2]-48;

if (msg[0]==1) // if the 1st char is equal to 1 then its blinking time ;-P
{

//....blinking code

}

The characters i managed to get them, but i try a lot of code in that if statment (blinking code) but never managed to get them blink... anyone could give me a 'light' ? lols :slight_smile:

I heard that this can be done using Metro and Messenger library also, if anyone knows anything about it i would be more then happy to hear :wink:

Thanks for spending your time reading my question.

It sounds like you want to wait until three characters are available, read each one and blink the led according to the value.

Perhaps the following will give you an idea on how you can do this:

int ledPin = 13;                // LED connected to digital pin 13

void setup(){                    // run once, when the sketch starts
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output
}

void loop(){                     // run over and over again
  if( Serial.available() >= 3){
    int value = Serial.read() - '0';  // convert ascii number into a decimal value
    blink(value);
  }
}

void blink( int nbrOfBlinks){
  while(  nbrOfBlinks > 0 ){
    digitalWrite(ledPin, HIGH);   // sets the LED on
    delay(1000);                  // waits for a second
    digitalWrite(ledPin, LOW);    // sets the LED off
    delay(1000);                  // waits for a second 
    nbrOfBlinks--;
  }
}

I got the idea on the code you give me. but i guess i need to explain better something.

I will be using 2 arduinos both with 2 leds (reds and yellows).. in one of the arduinos it will be hardcoded a number 1 and the other 2. its kinda like an ID, so when i read the first char, I will only process if the number is equal to the arduino ID. this is because i just want to send to specific arduinos...

So, e.g, if i send '123' it the 2 and 3 will only be processed in the arduino that has in the code the ID=1.. if i send '223' it's for arduino 2...

Your code is very clear and easy to get it, I will try to get to see with 2 leds...

edited---

I have this code now, dont know if will work....

#include <Metro.h> //Include Metro library



int REDLEDpin = 12;                // Red LED connected to digital pin 12
int YELLOWLEDpin = 13;                 // Yellow LED connected to digital pin 13
int MOTEID=1;
int msg[3];


// Instanciate a metro object and set the interval to 250 milliseconds (0.25 seconds).
Metro ledMetro_RED = Metro(250); 
Metro ledMetro_YELLOW = Metro(250);
int state_RED=LOW;
int state_YELLOW=LOW;
 

void setup(){                    
  pinMode(REDLEDpin, OUTPUT);      
  pinMode(YELLOWLEDpin, OUTPUT);
  digitalWrite(REDLEDpin,state_RED);
  digitalWrite(YELLOWLEDpin,state_YELLOW);

}

void loop(){

                
  while (Serial.available() < 1 ) // reading 1st char to identify the ID
   {;}
   msg[0]=Serial.read();


  while (Serial.available() < 1 ) // reading 2nd char ( number times red will blink)
   {;}
   msg[1]=Serial.read();


  while (Serial.available() < 1 ) // reading 3nd char ( number times yellow will blink)
   {;}
   msg[2]=Serial.read();

  msg[0] = msg[0]-'0';
  msg[1] = msg[1]-'0';
  msg[2] = msg[2]-'0';
  
  if(msg[0]==MOTEID) {
    blink(msg[1],REDLEDpin); //blink the RED 'msg[1]' times
    blink(msg[2],YELLOWLEDpin); //blink the YELLOW 'msg[2]' times
  }
}


void blink( int nbrOfBlinks,int ledPin){
  
  while(  nbrOfBlinks > 0 ){
    digitalWrite(ledPin, HIGH);   // sets the LED on
    delay(1000);                  // waits for a second
    digitalWrite(ledPin, LOW);    // sets the LED off
    delay(1000);                  // waits for a second
    nbrOfBlinks--;
  }
}

Will this work?

I also have a question, the delay fucntion holds down the cpu i whould like to use metro library for that... you have any idea?

thanks

I am not clear on what you want your application to do. Can you describe the logic in more detail.

Here is an example of how you might start the description:

If an identifier character is received on the serial port and the character matches the id of this arduino, read the next two characters and use the following logic to determine how an led will blink (describe how you want the first and second digit to blink an led)

Also describe what should happen if the identifier does not match this arduino (presumably it will throw away two characters and wait for another id)

Describe when and where you want to have a 250ms delay

One way to help you determine the logic is to define a few likely scenarios and think about what the arduono should do.

Good luck

Well, let's see if I can explain what im trying to do...

I developed a small application in C# (windows.forms) wich will communicate via serial port with a zigbee radio... the radio receives a messages via serial ( message format is always 1xx or 2xx, [x = to a number between 0 and 9])
Then that radio will Start sending over air that message.

In my scenario I will have 2 arduinos (both) equiped with radios waiting to receive the message. One arduino will have the ID set to '1' the other to '2' (in the C code).

If I send the message '123', both will receive the message but only one will blink the corresponding leds with the corresponding times. and HOW?
THe how part:

When the arduino reads the 1st digit it will check to see if it matches their ID variable (hard coded). if not it simply doenst do nothing just wait for other message.

basically i get all 3 chars and then i test that condition as follows:
if(msg[0]==MOTEID)

if it matches the ID condition, then i already have the next two chars saved in a array.

So in my example the next chars will be '23'. The '2' char is now stored in msg[1] and '3' is stored in msg[2] (both) has integers.

So the red will blink two times (intervaled in 250ms), and the yellow 3 times (same 250ms interval).

That's it ;-)...

I hope I explained better.. sorry my english, not my born language...

Thanks :wink:

Yes but... Why ?

wOOt ? "yes but why? " why what? lol... I posted the code up i was wondering if would solve my prob.

I mean why do you want this to do this? What is it for?

I'll think out loud here.... (I believe this is what you want)

loop
grab the 3 bytes from the network...
check ID byte.
If the packet was for me, process it otherwise discard

read 1st data byte (2nd real byte)
blink(redLED, NumberOfTimes)
read 2nd data byte
blink(yellowLED, NumberOfTimes)

end of loop

At the top of the program, define your pins like so:

#define redLED x // where x is the pin of the LED
example:
#define redLED 13 // LED conected to pin 13
#define yellowLED 12 // LED connected to pin 12
#define greenLED 11 // you get the idea?

You should have a routine that says:

blink(LEDpin,numberoftimes)
{
for (counter=0; counter != numberoftimes; counter++)
{
digitalWrite(LEDpin, HIGH);
delay(500);
digitalWrite(LEDpin, LOW);
delay(500);
}
}

My syntax might need checking but you should have the idea.

yep it's kinda like that, but red and yellow might blink different times :slight_smile: the code i posted befor is working, but now what im tryinh to do is doing the same thing but using the Messenger library because the delay function HOLDS down the cpu of the arduino ( not good).

why is it not good, what else do you want to do when its blinking?

the idea is almost trying to make the feeling of both leds starting to blink at same time instead of red blink x times and after that yellow blinking y times... I might be confuzed on this and dunno if its possible, because im not using threads.

ps- i guess i got confuzed with the library I supose its metro not messenger... sorry

What is this thing FOR? What does it do ?!?! :o

I do not know what is the messenger library. However, you can blink both LEDS at the same time by putting both in the same for loop and only turn a LED on if the num-to-blink =< current value of the counter. The upper bound of the counter is the max of 1&2.

If you do not like the delay function, use the mills to determine elapsed time and blin on&off from that.

Nice tip Mark, I will try to do that :wink: ty.

Mike this is for a small Project at University, Im learning how to use zigbee radios with arduino to preform remote actions, this is kinda like a "toy" project... By learning more of how can I interact with arduino it could open me some "doors" to a possible MASTER'S THESIS, because im doing a Master degree on ubiquitous computing .