I came across this code posted in the forum and tried it and found to be perfectly working. However, i made an attempt to increase the number of buttons to 3 by simple copying the lines for button 2. However, when i run it the 3rd led keeps blinking. Any help on how to correctly add the 3rd button? thanks.
/*
This code changes the state of two variables by pressing two pushbuttons and sends
the information wireless over 433MHz, allowing to remote control two LEDs.
Use the "receiver_LED" code for the receiver.
Made by Matthias - https://www.youtube.com/user/Experimentaltechnik
25.6.2014
*/
#include <VirtualWire.h>
//download the library here: http://www.airspayce.com/mikem/arduino/VirtualWire/
//author of the library: Mike McCauley
byte ledState = B00;
byte ledState2 = B00;
byte ledStates = B00;
byte sendState =0;
int counter =0;
int counter2 =0;
const int buttonPin = 2; // the number of the pushbutton pin
const int buttonPin2 = 3;
int buttonState; // the current reading from the input pin
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
int buttonState2; // the current reading from the input pin
long lastDebounceTime2 = 0; // the last time the output pin was toggled
long debounceDelay2 = 50; // the debounce time; increase if the output flickers
char ledStatesChar[2]; //char array; stores the Led States; needed in vw_send function
void setup()
{
// Initialise the IO and ISR
// vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(2000); // Bits per sec
pinMode(buttonPin, INPUT);
pinMode(buttonPin2, INPUT);
}
void loop()
{
if(sendState == 1){ //data is only send if a button was pressed
ledStates =(ledState | ledState2);
itoa(ledStates, ledStatesChar, 10);
digitalWrite(13, true); // Flash a light to show transmitting
vw_send((uint8_t *)ledStatesChar, strlen(ledStatesChar));
vw_wait_tx(); // Wait until the whole message is gone
digitalWrite(13, false);
}
sendState =0;
//debounce code button 1:
int buttonValue = digitalRead(buttonPin);
if ((millis() - lastDebounceTime) > debounceDelay) {
if (buttonState != buttonValue) {
if(buttonValue == HIGH){
counter++;
if ((counter%2 ==0)) {
ledState = B00; //00 = first LED off, 01 = first LED on
}
else {
ledState = B01;
}
sendState =1;
}
lastDebounceTime = millis();
buttonState = buttonValue;
}
}
//debounce code button 2:
int buttonValue2 = digitalRead(buttonPin2);
if ((millis() - lastDebounceTime2) > debounceDelay2) {
if (buttonState2 != buttonValue2) {
if(buttonValue2 == HIGH){
counter2++;
if ((counter2%2 ==0)) {
ledState2 = B00; //00 = second LED off, 10 = second LED on
}
else {
ledState2 = B10;
}
sendState =1;
}
lastDebounceTime2 = millis();
buttonState2 = buttonValue2;
}
}
}
This is the code i made for three buttons and using three bits.
#include <VirtualWire.h>
byte ledState = B000;
byte ledState2 = B000;
byte ledState3 = B000;
byte ledStates = B000;
byte sendState =0;
int counter =0;
int counter2 =0;
int counter3 =0;
const int buttonPin = 2; // the number of the pushbutton pin
const int buttonPin2 = 3;
const int buttonPin3 = 4;
int buttonState; // the current reading from the input pin
int buttonState2;
int buttonState3;
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
// the current reading from the input pin
long lastDebounceTime2 = 0; // the last time the output pin was toggled
long debounceDelay2 = 50; // the debounce time; increase if the output flickers
long lastDebounceTime3 = 0; // the last time the output pin was toggled
long debounceDelay3 = 50;
char ledStatesChar[2]; //char array; stores the Led States; needed in vw_send function
void setup()
{
// Initialise the IO and ISR
// vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(2000); // Bits per sec
pinMode(buttonPin, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
Serial.begin(9600);
}
void loop()
{
if(sendState == 1){ //data is only send if a button was pressed
ledStates =(ledState | ledState2 | ledState3);
Serial.print("ledStates: ");
Serial.println(ledStates);
Serial.print("ledStatesChar: ");
Serial.println(ledStatesChar);
itoa(ledStates, ledStatesChar, 10);
digitalWrite(13, true); // Flash a light to show transmitting
vw_send((uint8_t *)ledStatesChar, strlen(ledStatesChar));
Serial.println(strlen(ledStatesChar));
vw_wait_tx(); // Wait until the whole message is gone
digitalWrite(13, false);
}
sendState =0;
//debounce code button 1:
int buttonValue = digitalRead(buttonPin);
if ((millis() - lastDebounceTime) > debounceDelay) {
if (buttonState != buttonValue) {
if(buttonValue == HIGH){
counter++;
// Serial.println(counter);
if ((counter%2 ==0)) {
ledState = B000; //00 = first LED off, 01 = first LED on
}
else {
ledState = B001;
}
sendState =1;
}
lastDebounceTime = millis();
buttonState = buttonValue;
}
}
//debounce code button 2:
int buttonValue2 = digitalRead(buttonPin2);
if ((millis() - lastDebounceTime2) > debounceDelay2) {
if (buttonState2 != buttonValue2) {
if(buttonValue2 == HIGH){
counter2++;
if ((counter2%2 ==0)) {
ledState2 = B000; //00 = second LED off, 10 = second LED on
}
else {
ledState2 = B010;
}
sendState =1;
}
lastDebounceTime2 = millis();
buttonState2 = buttonValue2;
}
}
//debounce code button 3:
int buttonValue3 = digitalRead(buttonPin3);
if ((millis() - lastDebounceTime3) > debounceDelay3) {
if (buttonState3 != buttonValue3) {
if(buttonValue3 == HIGH){
counter3++;
if ((counter3%3 ==0)) {
ledState3 = B000; //00 = second LED off, 10 = second LED on
}
else {
ledState3 = B100;
}
sendState =1;
}
lastDebounceTime3 = millis();
buttonState3 = buttonValue3;
}
}
}
If you had used arrays for the pin numbers, pin states (current and previous), and times, changing from two pins to 3 would have been just about trivial, and the stupid mistake you made wouldn't have happened.
"It doesn't work" is too lame for words. The code does something that you did not describe. How can we help you make it do what you want when you don't tell us what it does and how that differs from what you want?