Help Needed

Hi, Im new to arduino and on my first project. Im trying to get codes from a 433mhz remote so I can control remote power sockets from a 4 by 4 keypad. The control has on and off buttons for each adapter instead of just 1 button for on and off. Iv got it working using 2 buttons for on and off but I wanted to use just one button to turn each socket on and off. Has anyone got any ideas how I could do this please. Here is some sample code I am using:

#include <Keypad.h>
#include <RCSwitch.h>

RCSwitch mySwitch = RCSwitch();

const byte ROWS = 4;
const byte COLS = 4;
// Define the Keymap
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 9, 8, 7, 6 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 5, 4, 3, 2 };

// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup()
{

// Transmitter is connected to Arduino Pin #10
mySwitch.enableTransmit(10);
mySwitch.setPulseLength(190);

Serial.begin(9600);
}

void loop()
{
char key = kpd.getKey();
if(key) // Check for a valid key.
{
switch (key)
{
case '1':
mySwitch.send("000101010101110100000011"); \button on code
break;
case '2':
mySwitch.send("000101010101110100001100"); \button off code
break;
default:
Serial.println(key);
}
}
}

Thanks

Use a variable to remember the current state:

byte state = 0;

and modify your case statement as follows:

  switch (key)
  {
  case '1':
    if (state == 0) {
      mySwitch.send("000101010101110100000011"); 
      \\button on code
    } 
    else {
      mySwitch.send("000101010101110100001100"); 
      \\button off code
    }
    state = !state; //Swap the state: if it is 0, make it 1, if it is 1, make it 0
    break;
  }

Hi, Thanks for the reply. It worked perfectly. Now im trying to add the second button but it doesn't seem to wont to work. Button 2 is working fine but button 1 turns both button 1 and 2 on and off at the same time. Do you know what might be wrong. Here is the code.

#include <Keypad.h>
#include <RCSwitch.h>

RCSwitch mySwitch = RCSwitch();

byte state1 = 0;
byte state2 = 0;

const byte ROWS = 4;
const byte COLS = 4;
// Define the Keymap
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 9, 8, 7, 6 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 5, 4, 3, 2 };

// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup()
{

// Transmitter is connected to Arduino Pin #10
mySwitch.enableTransmit(10);
mySwitch.setPulseLength(190);

Serial.begin(9600);
}

void loop()
{
char key = kpd.getKey();
if(key) // Check for a valid key.
{
switch (key)
{
case '1':
if (state1 == 0) {
mySwitch.send("000101010111010100000011");
}
else {
mySwitch.send("000101010111010100001100");
}
case '2':
if (state2 == 0) {
mySwitch.send("000101010101110100000011");
}
else {
mySwitch.send("000101010101110100001100");
}
state1 = !state1; //Swap the state: if it is 0, make it 1, if it is 1, make it 0
state2 = !state2; //Swap the state: if it is 0, make it 1, if it is 1, make it 0
break;
}
}
}

Thanks

You have the state swapping in the wrong place.
Try this:

void loop()
{
  char key = kpd.getKey();
  if(key)  // Check for a valid key.
  {
    switch (key)
    {
      case '1':
        if (state1 == 0) {
        mySwitch.send("000101010111010100000011");
        }
      else {
        mySwitch.send("000101010111010100001100");
      }
      state1 = !state1; //Swap the state: if it is 0, make it 1, if it is 1, make it 0
      break;
      case '2':
      if (state2 == 0) {
        mySwitch.send("000101010101110100000011");
      }            
      else {
        mySwitch.send("000101010101110100001100");
      }                 
      state2 = !state2; //Swap the state: if it is 0, make it 1, if it is 1, make it 0
      break;
    }
  }
}

Thank you. Its all working perfectly now. :slight_smile: