Virtual numeric keypad

Hi Cayenne. I am working on a project where the cayenne widgets will serve as buttons of a keypad and when the correct four (out of the eight) widgets are selected (regardless of the order those four widgets are pressed), it will light up an LED.

image

Here is the code:

#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <CayenneMQTTEthernet.h>

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "";
char password[] = "";
char clientID[] = "";

#define VIRTUAL_CHANNEL_2 2
#define ACTUATOR_PIN_2 2 
#define VIRTUAL_CHANNEL_3 3
#define ACTUATOR_PIN_3 3 
#define VIRTUAL_CHANNEL_4 4
#define ACTUATOR_PIN_4 4 
#define VIRTUAL_CHANNEL_5 5
#define ACTUATOR_PIN_5 5 
#define VIRTUAL_CHANNEL_6 6
#define ACTUATOR_PIN_6 6 
#define VIRTUAL_CHANNEL_7 7
#define ACTUATOR_PIN_7 7 
#define VIRTUAL_CHANNEL_8 8
#define ACTUATOR_PIN_8 8 
#define VIRTUAL_CHANNEL_9 9
#define ACTUATOR_PIN_9 9

void setup()
{
	Serial.begin(9600);
  pinMode(ACTUATOR_PIN_2, OUTPUT);
  pinMode(ACTUATOR_PIN_3, OUTPUT);
  pinMode(ACTUATOR_PIN_4, OUTPUT);
  pinMode(ACTUATOR_PIN_5, OUTPUT);
  pinMode(ACTUATOR_PIN_6, OUTPUT);
  pinMode(ACTUATOR_PIN_7, OUTPUT);
  pinMode(ACTUATOR_PIN_8, OUTPUT);
  pinMode(ACTUATOR_PIN_9, OUTPUT);
	Cayenne.begin(username, password, clientID);
}

void loop()
{
Cayenne.loop();
if (digitalRead(2) == HIGH &&
    digitalRead(3) == HIGH &&
    digitalRead(4) == HIGH &&
    digitalRead(5) == HIGH &&
    digitalRead(6) == LOW &&
    digitalRead(7) == LOW &&
    digitalRead(8) == LOW &&
    digitalRead(9) == LOW) 
{
    digitalWrite(10,HIGH);
}
}

// This function is for button 2.
CAYENNE_IN(2)
{
  int value = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_2, ACTUATOR_PIN_2, value);
  // Write the value received to the digital pin.
  digitalWrite(ACTUATOR_PIN_2, value);
}

// This function is for button 3.
CAYENNE_IN(3)
{
  int value = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_3, ACTUATOR_PIN_3, value);
  // Write the value received to the digital pin.
  digitalWrite(ACTUATOR_PIN_3, value);
}

// This function is for button 4.
CAYENNE_IN(4)
{
  int value = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_4, ACTUATOR_PIN_4, value);
  // Write the value received to the digital pin.
  digitalWrite(ACTUATOR_PIN_4, value);
}

// This function is for button 5.
CAYENNE_IN(5)
{
  int value = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_5, ACTUATOR_PIN_5, value);
  // Write the value received to the digital pin.
  digitalWrite(ACTUATOR_PIN_5, value);
}

// This function is for button 6
CAYENNE_IN(6)
{
  int value = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_6, ACTUATOR_PIN_6, value);
  // Write the value received to the digital pin.
  digitalWrite(ACTUATOR_PIN_6, value);
}

// This function is for button 7.
CAYENNE_IN(7)
{
  int value = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_7, ACTUATOR_PIN_7, value);
  // Write the value received to the digital pin.
  digitalWrite(ACTUATOR_PIN_7, value);
}

// This function is for button 8.
CAYENNE_IN(8)
{
  int value = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_8, ACTUATOR_PIN_8, value);
  // Write the value received to the digital pin.
  digitalWrite(ACTUATOR_PIN_8, value);
}


// This function is for button 9.
CAYENNE_IN(9)
{
  int value = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_9, ACTUATOR_PIN_9, value);
  // Write the value received to the digital pin.
  digitalWrite(ACTUATOR_PIN_9, value);
}

Can u help me figure out what’s wrong with my code? Appreciate your help always.

modify the code from here Smart Lock and Doorbell using cayenne

Hi Shramik. It is overwhelmingly complicated. Just got a headache. : ) Anything simple in approach?

Will that work on an Arduino Programmer?

it will work on arduino. a simple approach will just increment the counter on right input and decrement on the wrong input. compare value on 4 trial and if correct turn ON led.

I just cannot get the code Shramik. I am not going to use a physical keypad. The widgets are themselves the keypad.

image

Something tells me there is something wrong with this code I created

Is there a way I can use the status of the virtual channel (the widgets themselves) instead of the physical status of the pins to create the if statement?. Thanks again buddy

what is the above code for?

this is the simplest approach i can think of:

int x;
void loop()
{
  Cayenne.loop();
  if (x == 4)
  {
     x = 0;
    //Do whatever you want
  }
}

CAYENNE_IN(4)
{
x++;
}

// This function is for button 5.
CAYENNE_IN(5)
{
x++;
}

// This function is for button 6
CAYENNE_IN(6)
{
x = 0;
}

// This function is for button 7.
CAYENNE_IN(7)
{
x++;
}

// This function is for button 8.
CAYENNE_IN(8)
{
x++;
}

// This function is for button 9.
CAYENNE_IN(9)
{
x = 0;
}

My goal is to turn on the LED (pin 10) if widgets 2, 3, 4 and 5 are all ON. Otherwise, LED is off

Will the code that u gave satisfy this one?

“My goal is to turn on the LED (pin 10) if widgets 2, 3, 4 and 5 are all ON”

Desperately need your help Shramik. : )

i gave you an example how to do it, as i have not taken into consideration which widget are ON or OFF. you need to modify it to your need. for examples, when widget 2, 3, 4, 5 will be ON, the x will increment and when any other widget is ON x will be zero. after 4 correct ON the x value will be 4 and then it will turn ON whatever you want.

#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <CayenneMQTTEthernet.h>

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "";
char password[] = "";
char clientID[] = "";

#define VIRTUAL_CHANNEL_2 2
#define ACTUATOR_PIN_2 2 
#define VIRTUAL_CHANNEL_3 3
#define ACTUATOR_PIN_3 3 
#define VIRTUAL_CHANNEL_4 4
#define ACTUATOR_PIN_4 4 
#define VIRTUAL_CHANNEL_5 5
#define ACTUATOR_PIN_5 5 
#define VIRTUAL_CHANNEL_6 6
#define ACTUATOR_PIN_6 6 
#define VIRTUAL_CHANNEL_7 7
#define ACTUATOR_PIN_7 7 
#define VIRTUAL_CHANNEL_8 8
#define ACTUATOR_PIN_8 8 
#define VIRTUAL_CHANNEL_9 9
#define ACTUATOR_PIN_9 9

void setup()
{
	Serial.begin(9600);
  pinMode(ACTUATOR_PIN_2, OUTPUT);
  pinMode(ACTUATOR_PIN_3, OUTPUT);
  pinMode(ACTUATOR_PIN_4, OUTPUT);
  pinMode(ACTUATOR_PIN_5, OUTPUT);
  pinMode(ACTUATOR_PIN_6, OUTPUT);
  pinMode(ACTUATOR_PIN_7, OUTPUT);
  pinMode(ACTUATOR_PIN_8, OUTPUT);
  pinMode(ACTUATOR_PIN_9, OUTPUT);
	Cayenne.begin(username, password, clientID);
}
int x;
void loop()
{
Cayenne.loop();
if (x == 4 )
{
    digitalWrite(10,HIGH);
}
}

// This function is for button 2.
CAYENNE_IN(2)
{
x++;
}

// This function is for button 3.
CAYENNE_IN(3)
{
x++;
}

// This function is for button 4.
CAYENNE_IN(4)
{
x++;
}

// This function is for button 5.
CAYENNE_IN(5)
{
x++;
}

// This function is for button 6
CAYENNE_IN(6)
{
x = 0;
}

// This function is for button 7.
CAYENNE_IN(7)
{
x = 0;
}

// This function is for button 8.
CAYENNE_IN(8)
{
x = 0;
}


// This function is for button 9.
CAYENNE_IN(9)
{
x = 0;
}

That"s clever Shramik!Thanks for rhe help!

next, you might need to make all widgets zero when the wrong widget is pressed. you can do from the code itself. I guess this will be an exercise for you as you have not asked for it :grin:

jast a thought shamik what if a person is trying all possible combinations and for some reason lets say he turned widget 5 on and off 4 times. will that turn on the LED?

i gave you the simplest code that does not handle all those issues. if the user only switches ON/OFF widget 5 only 2 times it turns ON the led. you got write the code that handles this issue and that is done the link i gave first.

Hi Shramik.

Will this work?

#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <CayenneMQTTEthernet.h>

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "";
char password[] = "";
char clientID[] = "";

#define VIRTUAL_CHANNEL_2 2
#define ACTUATOR_PIN_2 2 
#define VIRTUAL_CHANNEL_3 3
#define ACTUATOR_PIN_3 3 
#define VIRTUAL_CHANNEL_4 4
#define ACTUATOR_PIN_4 4 
#define VIRTUAL_CHANNEL_5 5
#define ACTUATOR_PIN_5 5 
#define VIRTUAL_CHANNEL_6 6
#define ACTUATOR_PIN_6 6 
#define VIRTUAL_CHANNEL_7 7
#define ACTUATOR_PIN_7 7 
#define VIRTUAL_CHANNEL_8 8
#define ACTUATOR_PIN_8 8 
#define VIRTUAL_CHANNEL_9 9
#define ACTUATOR_PIN_9 9

void setup()
{
	Serial.begin(9600);
  pinMode(ACTUATOR_PIN_2, OUTPUT);
  pinMode(ACTUATOR_PIN_3, OUTPUT);
  pinMode(ACTUATOR_PIN_4, OUTPUT);
  pinMode(ACTUATOR_PIN_5, OUTPUT);
  pinMode(ACTUATOR_PIN_6, OUTPUT);
  pinMode(ACTUATOR_PIN_7, OUTPUT);
  pinMode(ACTUATOR_PIN_8, OUTPUT);
  pinMode(ACTUATOR_PIN_9, OUTPUT);
  pinMode(13, LOW);
	Cayenne.begin(username, password, clientID);
}

int s;
int t;
int u;
int v;
int w;
int x;
int y;
int z;

void loop()
{
Cayenne.loop();
if ((s == 1) &&
    (t == 1) &&
    (u == 1) &&
    (v == 1) &&
    (w == 0) &&
    (x == 0) &&
    (y == 0) &&
    (z == 0))
    {
    digitalWrite(13,HIGH);
    }
else 
    {
    digitalWrite(13,LOW);
    }
}

// This function is for button 2.
CAYENNE_IN(2)
{
  int s = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_2, ACTUATOR_PIN_2, s);
  // Write the value received to the digital pin.
  digitalWrite(ACTUATOR_PIN_2, s);
}

// This function is for button 3.
CAYENNE_IN(3)
{
  int t = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_3, ACTUATOR_PIN_3, t);
  // Write the value received to the digital pin.
  digitalWrite(ACTUATOR_PIN_3, t);
}

// This function is for button 4.
CAYENNE_IN(4)
{
  int u = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_4, ACTUATOR_PIN_4, u);
  // Write the value received to the digital pin.
  digitalWrite(ACTUATOR_PIN_4, u);
}

// This function is for button 5.
CAYENNE_IN(5)
{
  int v = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_5, ACTUATOR_PIN_5, v);
  // Write the value received to the digital pin.
  digitalWrite(ACTUATOR_PIN_5, v);
}

// This function is for button 6
CAYENNE_IN(6)
{
  int w = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_6, ACTUATOR_PIN_6, w);
  // Write the value received to the digital pin.
  digitalWrite(ACTUATOR_PIN_6, w);
}

// This function is for button 7.
CAYENNE_IN(7)
{
  int x = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_7, ACTUATOR_PIN_7, x);
  // Write the value received to the digital pin.
  digitalWrite(ACTUATOR_PIN_7, x);
}

// This function is for button 8.
CAYENNE_IN(8)
{
  int y = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_8, ACTUATOR_PIN_8, y);
  // Write the value received to the digital pin.
  digitalWrite(ACTUATOR_PIN_8, y);
}


// This function is for button 9.
CAYENNE_IN(9)
{
  int z = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_9, ACTUATOR_PIN_9, z);
  // Write the value received to the digital pin.
  digitalWrite(ACTUATOR_PIN_9, z);
}

yup, should work.

thanks buddy