system
August 26, 2011, 10:56pm
1
does know how to control multiple led's via serial. here's my code for some reason it only turns on the led connected to pin 13.
int incomingByte;
void setup (){
Serial .begin(9600);
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
pinMode(8, OUTPUT);
}
void loop (){
if (Serial .available() > 0) {
incomingByte = Serial .read();
if (incomingByte == '1') {
digitalWrite(13, HIGH);
}
if (incomingByte == '2') {
digitalWrite(13, LOW);
if (Serial .available() > 0) {
incomingByte = Serial .read();
if (incomingByte == '3') {
digitalWrite(12, HIGH);
}
}
if (incomingByte == '4') {
digitalWrite(12, LOW);
incomingByte = Serial .read();
if (incomingByte =='5'){
digitalWrite(11, HIGH);
}
}
if (incomingByte == '6');
digitalWrite(11, LOW);
if (Serial .available() > 0) {
incomingByte = Serial .read();
if (incomingByte == '7') {
digitalWrite(10, HIGH);
}
}
if (incomingByte =='8');
digitalWrite(10, LOW);
if (Serial .available() > 0) {
incomingByte = Serial .read();
if (incomingByte == '9') {
digitalWrite(9, HIGH);
}
}
if (incomingByte =='10');
digitalWrite(9, LOW);
if (Serial .available() > 0) {
incomingByte = Serial .read();
if (incomingByte == '11') {
digitalWrite(8, HIGH);
}
}
if (incomingByte =='12');
digitalWrite(8, LOW);
}
}
}
system
August 26, 2011, 11:04pm
2
Perhaps if you lined up your { and your } correctly, things would be different.
system
August 26, 2011, 11:11pm
3
If you look around you will find the exact same code but working somewhere here on the forum, even with the maxp/msp patch to control it with.
system
August 26, 2011, 11:14pm
4
And if you looked at lining up those braces, you might also find your code working.
system
August 27, 2011, 2:30pm
5
if (incomingByte =='10');
if (incomingByte == '11') {
if (incomingByte =='12');
Those semicolons at the end of the if statements are not helping, either.
Once you get the nesting correct, and remove the extraneous semicolons, these statements will never evaluate to true, no matter what you type in the Serial Monitor. The Serial Monitor is not capable of sending multi-byte characters.
Strings, yes. Multi-byte characters, no.
dc42
August 27, 2011, 2:57pm
6
What I think you meant is:
void loop(){
if (Serial.available() > 0) {
incomingByte = Serial.read();
if (incomingByte == '1') {
digitalWrite(13, HIGH);
}
if (incomingByte == '2') {
digitalWrite(13, LOW);
}
if (incomingByte == '3') {
digitalWrite(12, HIGH);
}
if (incomingByte == '4') {
digitalWrite(12, LOW);
}
if (incomingByte =='5'){
digitalWrite(11, HIGH);
}
if (incomingByte == '6') {
digitalWrite(11, LOW);
}
if (incomingByte == '7') {
digitalWrite(10, HIGH);
}
if (incomingByte =='8') {
digitalWrite(10, LOW);
}
if (incomingByte == '9') {
digitalWrite(9, HIGH);
}
if (incomingByte =='10') {
digitalWrite(9, LOW);
}
if (incomingByte == '11') {
digitalWrite(8, HIGH);
}
if (incomingByte =='12') {
digitalWrite(8, LOW);
}
}
In other words, just check and read the serial port once per loop.