0

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

Photo for my Project

 #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
  }
0

1 Answer 1

0

C has an "Exclusive Or" operator which is ^. You can use that instead of | so that it toggles the bit rather than always setting it:

case 'A' :
    bt_leds_activated ^= 1;
    break; 

With ^ if you OR a 0 with a 1 you get a 1. If you OR a 0 with a 0 you get a 0. If you OR a 1 with a 1 you get a 0.

That means every time you send A bit one of bt_leds_activated will invert. If it's 0 it will become 1. If it's 1 it will be come 0.

Also you seem to have two sets of states - the button states, and the bluetooth states. Instead you only want one set of states, and both the buttons and the bluetooth toggle bits in that single state set.

Because of that if either of the methods of turning an LED on has turned the LED on then the other method will have no chance at all of turning the LED off.

So just drop bt_leds_activated completely and use ledstates:

case 'A':
    ledstates ^= 1;
    break;

Now both the button and the character through bluetooth will toggle the LED.

13
  • No any that change for project state after edit code. the wrong still . hey man, please watch video to understand bug . thanks youtu.be/EcpJeewE1AU Commented Jan 2, 2020 at 12:51
  • I think your problem is that you have two sets of states, when you really only want one.
    – Majenko
    Commented Jan 2, 2020 at 12:54
  • What is the solution . Can you edit this bug. I think bug in this if condition Commented Jan 2, 2020 at 12:58
  • if(bt_leds_activated && millis()-bt_leds_timestamp > 2000){ ledstates &= ~bt_leds_activated; shift_out(ledstates); bt_leds_activated = 0; } Commented Jan 2, 2020 at 13:01
  • Forget bt_leds_activated completely. Just use ledstates everywhere.
    – Majenko
    Commented Jan 2, 2020 at 13:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.