Well I'm kinda new at this programing thing but I have done some coding in the past so its coming to me quite easily. I am just curious as to how to make an two LEDs alternate upon recieving a signal from Processing. (I have Processing properly sending out a signal so I know that the program works)
What I have is when Processing plays a song it detects beats, when a beat is detected it sends a H to the arduino via serial. Now the problem I have is when the arduino recieves the signal, each time I want it to alternate which LED to light up, until another "H" signal is recieved.
I have used if/else codes and tried many ways of linking them up to alternate but I just cant get them to, it will always just light one up.
:exclamation here is the code, I KNOW ITS WRONG, but I want you to see what I have so far and how I should code whats in the loop.
char val;
int blue = 13;
int red = 10;
void setup() {
pinMode(blue, OUTPUT);
pinMode(red, OUTPUT);
Serial.begin(9600);
}
void loop() {
if (Serial.available()) {
val = Serial.read();
}
if (val == 'H') {
digitalWrite(blue, HIGH);
digitalWrite(red,LOW);
}else{
if (val == 'L') {
digitalWrite(red, HIGH);
}
}
}
I am aware that this code will have the red light up on L and Blue on H. But what i want is, for example, H is sent for the first time, blue lights up permanantly until the next H is sent where it will turn blue off and Red will come on permanantly until the next H where red turns off and blue turns on ... and so on....
In between sending H's what do you send.? You need something else to switch the H off otherwise you only get to the If statement when it receives and H never any other time.
Thanks, but Im not sure if you understand what I am asking help for...
This is what happens.
In processing, I have a code that listens to a track and while it is playing it detects beats. When it finds a beat an H is sent to the Arduino, and anyother time a L is sent so it only reacts to an H. The code what first made to just blink when an H was recieved so it would look for an H and if H was present, it would turn on an LED, else (any toher letter than H) it would be off.
What I want:
I have two LEDs hooked up. A red and blue. What i want to happen is for when an H is sent, one will turn on (example blue) and stay on UNTIL the next H is sent, when in response to the second H (if the blue was on first) the blue would turn off and the red would turn on until the next H was recieved where it would switch and alternate until the completion of the song.
PLEASE NOTE
I Know the code I posted is WRONG and does something TOTALLY different than what I just said I wanted above.
I am asking for help on how to code as all the other approaches I have taken have failed. I would post what I have tried but all it is, is if...else statements that would seem to go endlessly. I would like to know what command to use and how I should use it to achieve what I want.
I am NOT asking for anyone to code it for me, but to tell me what command to use, and how it should be used.
when H is sent, one will turn on (example blue) and stay on UNTIL the next H is sent
One approach to program design is to express the logic in terms that are not quite a "real" programming language, but can be easily translated to a "real" programming language. That's called "pseudo-code."
In setup, maybe turn on one of the LEDs, or maybe turn off both of them. Your choice.
Here is some pseudo-code for your loop() function:
Wait until something comes in on the serial port.
Read the char.
IF the char is an 'H' THEN
IF the Blue LED is on THEN
Turn off the Blue LED and turn on the Red one.
ELSE
Turn on the Blue LED and turn off the Red one.
END IF
END IF
A possible implementation:
const int blue = 13;
const int red = 10;
void setup()
{
pinMode(blue, OUTPUT);
pinMode(red, OUTPUT);
// Start with blue on and red off
digitalWrite(blue, HIGH);
digitalWrite(red, LOW);
Serial.begin(9600);
}
void loop()
{
while (!Serial.available())
; //Stay in this "do-nothing" loop until something comes in
char val = Serial.read();
if (val == 'H') { // This is the only thing that changes the states of the LEDs
if (digitalRead(blue) == HIGH) { // Blue is on: turn it off and turn on the red.
digitalWrite(blue, LOW);
digitalWrite(red, HIGH);
}
else { // Blue is off: turn it on and turn off the red.
digitalWrite(blue, HIGH);
digitalWrite(red, LOW);
}
}
}