Virtual numeric keypad

#9

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

#10

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”

#11

Desperately need your help Shramik. : )

#12

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.

#13
#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;
}
#14

That"s clever Shramik!Thanks for rhe help!

#15

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:

#16

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?

#17

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.

#18

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);
}
#19

yup, should work.

#20

thanks buddy

#21

HI Shramik. I tried the code (the one I asked you if it will work). For reasons that I don’t know it didn’t work. Dunno what I’ve been doin’ wrong. I thought of another approach and this time I will use the status of the widget (whether it’s on or off) in my if statement.

#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);
}

void loop()
{
Cayenne.loop();
if (VIRTUAL_CHANNEL_2 == HIGH &&
    VIRTUAL_CHANNEL_3 == HIGH &&
    VIRTUAL_CHANNEL_4 == HIGH &&
    VIRTUAL_CHANNEL_5 == HIGH &&
    VIRTUAL_CHANNEL_6 == LOW &&
    VIRTUAL_CHANNEL_7 == LOW &&
    VIRTUAL_CHANNEL_8 == LOW &&
    VIRTUAL_CHANNEL_9 == LOW) 
{
    digitalWrite(13,HIGH);
    }
else 
{
    digitalWrite(13,LOW);
}
}

// 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);
}

What do u think? Will this one work?

Send a password from cayenne to arduino when a button is pushed
#22

Try this 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[] = "0";
char password[] = "";
char clientID[] = "d";

#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)
{
  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)
{
 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)
{
  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)
{
  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)
{
  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)
{
   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)
{
  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)
{
  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);
}
#23

what’s changed Shramik?

#24

Does it work?

#25

yes Shramik but I wonder what was changed? Is it the same code I have posted or you made some changes

#26

if it works i guess you can compare and check what was the mistake :wink:

1 Like
#27

nonetheless thank u buddy as always

#28

I found the mistake! What a difference a small mistake makes! Learning lesson for me

1 Like