Virtual numeric keypad


#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?


#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:


#27

nonetheless thank u buddy as always


#28

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