Serial.read gets a number from 1 to 36 (1 to 8 added together
The code works fine to toggle single LEDs on and off, but not for when two or more LEDs need to be on.
(ignore the radio buttons and other stuff in the GUI, I want to get this part working first)
I need a way of turning on LED 1 and 2 with a serial input of 3 as shown above or any combination,depending on which checkboxes are checked.
So for example, If checkboxes 7 and 8 are checked, serial input = 15, LED 7 and 8 should be on.
I`ve spent hours trying various things to work this out, but I need some help.
/*
Switch statement with serial input from Autoit
*/
int InByte;
void setup()
{
Serial.begin(57600);
Serial.println ("Arduino Started");
for (int thisPin = 2; thisPin < 17; thisPin++) {
pinMode(thisPin, OUTPUT);
// turn the pin on:
digitalWrite(thisPin, HIGH);
delay(100);
// turn the pin off:
digitalWrite(thisPin, LOW);
}
}
void loop() {
//Get Serial input from Autoit
//START
if (Serial.available() > 0) {
int inByte = Serial.read();
switch (inByte) {
case 1:
digitalWrite(2, HIGH);
Serial.println("2");
break;
case 2:
digitalWrite(3, HIGH);
Serial.println("3");
break;
case 3:
digitalWrite(4, HIGH);
Serial.println("4");
break;
case 4:
digitalWrite(5, HIGH);
Serial.println("5");
break;
case 5:
digitalWrite(6, HIGH);
Serial.println("6");
break;
case 6:
digitalWrite(7, HIGH);
Serial.println("7");
break;
case 7:
digitalWrite(8, HIGH);
Serial.println("8");
break;
case 8:
digitalWrite(9, HIGH);
break;
default:
// turn all the LEDs off:
for (int thisPin = 2; thisPin < 17; thisPin++) {
digitalWrite(thisPin, LOW);
} // End LED`s off
} //End Switch
} //End Serial
} //END
I`m trying to simulate the input from another program.
5 bits would be to ID each LED (actually 8 Bi-colour LED`s or 8 Red and 8 Green) as the this requires no extra hardware.
The other bits (bit 6 and 7) will be used to ID Read and Write(actually only two bits will be used/needed for Read and Write), Red for read, Green for write (or the other way round, Red for Write etc).
I`ll have a look at using the Bitread function for this.
/*
serial input from serial monitor
*/
// an array of pin numbers to which LEDs are attached
int ledPins[] = {
2, 3, 4, 5, 6, 7,8,9,10,11,12,13, 14, 15, 16, 17 };
int InByte;
void setup() {
Serial.begin(57600);
Serial.println ("");
Serial.println ("Arduino Started");
// loop over the pin array and set them all to output
for (int thisLed = 0; thisLed < 17; thisLed++) {
pinMode(ledPins[thisLed], OUTPUT);
//test all the LEDs
digitalWrite(ledPins[thisLed],HIGH);
delay(100);
digitalWrite(ledPins[thisLed],LOW);
}
}
void loop() {
//Get Serial input
/*
RED GREEN RED/GREEN
33 = ! 65 = A 97 = a
34 = " 66 = B 98 = b
35 = # 67 = C 99 = c
36 = $ 68 = D 100= d
37 = % 69 = E 101= e
38 = & 70 = F 102= f
39 = ' 71 = G 103= g
*/
//START
if (Serial.available() > 0) {
int inByte = Serial.read();
Serial.print("inByte = ");
Serial.println(inByte);
Serial.println(inByte,BIN);
int R = (bitRead(inByte,6));
if (R = 1){
// if (bitRead(inByte,6)){
Serial.println("RED");
digitalWrite(ledPins[inByte-33],HIGH);
delay(200);
digitalWrite(ledPins[inByte-33],LOW);
} //EndIF
//End Serial
}
}//END
and it works, but instead of
int R = (bitRead(inByte,6));
if (R = 1){
I`d rather use
if (bitRead(inByte,6)){
it compiles ok, but does nothing, what am I doing wrong?
With your first method, the bit being read is always set to be 1 by the if statment, which means it will call the code regardless of whether or not the bit is set or not. For if statements you have to use the equality sign: (R==1)