Controlling Led's from Buttons and Bluetooth by same method
The problem now :
the LED light up when you send an A over Bluetooth and it will turn off after 2 sec automatically without you doing anything (this is the wrong now in video).
I Want the same char when send data the led turn on and when i want closed led send same char to turn off light .
The code: bottom this page
thank
this is the wrong now in video
#define CLK 10
#define CLKI 9
#define LD 8
#define SO 11
#define SER 12
byte ledstates;
byte copy=0;
unsigned long bt_leds_timestamp=0;
byte bt_leds_activated=0;
void setup(){
Serial.begin(9600);
pinMode(CLK,OUTPUT);
pinMode(CLKI,OUTPUT);
pinMode(LD,OUTPUT);
pinMode(SO,INPUT);
pinMode(SER,OUTPUT);
}
void loop(){
if (Serial.available()>0){
message();
}
shift_In();
shift_out(ledstates);
if(bt_leds_activated && millis()-bt_leds_timestamp > 2000){
ledstates &= ~bt_leds_activated;
shift_out(ledstates);
bt_leds_activated = 0;
}
}
void message(){
if(Serial.available()){ // Send data only when you receive data:
char data = Serial.read(); //Read the incoming data & store into data
Serial.print(data); //Print Value inside data in Serial monitor
switch (data){
case 'A' :
bt_leds_activated |= 1;
break;
case 'B' :
bt_leds_activated |= 2;
break;
case 'C' :
bt_leds_activated |= 4;
break;
case 'D' :
bt_leds_activated |= 8;
break;
case 'E' :
bt_leds_activated |= 16;
break;
case 'F' :
bt_leds_activated |= 32 ;
break;
case 'G' :
bt_leds_activated |= 64;
break;
case 'H' :
bt_leds_activated |= 128;
break;
default : return; }
}
ledstates |= bt_leds_activated;
shift_out(ledstates);
bt_leds_timestamp = millis();
}
void shift_out (byte copy){
digitalWrite(LD, LOW);
shiftOut(SER,CLK,MSBFIRST,copy);
digitalWrite(LD, HIGH);
delay(100);
}
void shift_In(){
digitalWrite(CLKI,HIGH);
digitalWrite(LD,HIGH);
digitalWrite(LD,LOW);
digitalWrite(LD,HIGH);
digitalWrite(CLK,HIGH);
digitalWrite(CLKI,LOW);
byte buttonstates = shiftIn(SO,CLK,MSBFIRST); //read button states in a local variable
digitalWrite(CLKI,HIGH);
for(int i=0;i<8;i++){ // loop through all 8 bits of the buttonstates
if(!(copy & (1<<i)) && (buttonstates & (1<<i))){ // check if old button state (in copy) is zero and new buttonstate is 1 (for the currently check bit)
ledstates ^= 1<<i; // toggle the corresponding bit in the ledstates variable (will be shifted out later in the loop() function)
}
}
copy = buttonstates;
delay(10); // delay for debouncing
}