Hi everyone I'm stuck on keypad, because I want 3 products like 111 beats per minute. So if I press 1 once then it saves it as a variable of 1, if i press it again it saves it in a another variable as 1, etc... Then calculates. The code supplied is what I have so far and kind of explains what is happening, thanks for the help to those who have contributed.
It's hard to tell what's going on and what's supposed to happen, but I can tell you that all the delays between a "break" and the next "case" will never get executed, so they may as well not be there.
Something that isn't there but should be, is a pair of code tags.
Hi everyone I updated the post to an extent that if you choose a number i.e. 111 and follow the code it would make sense and special thanks to AWOL for the help and look forward this project working.
Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.
Hi,
PLEASE DO NOT KEEP GOING BACK AND UPDATING/EDITING YOUR FIRST POST.
You are not helping others to help you by changing the initial code you had problems with.
Each time you update YOUR CODE please post a NEW message with your updated code, NOT a message that says
New Update evryone
This forum is to help all users, so anyone who had a similar problem to you, SHOULD be able to follow how you solved your problem, by changing the initial post you are removing any hope of this thread being used to help other users.
SO PLEASE NEW MESSAGES EACH TIME, INCLUDING CODE UPDATES.
Hi,
How does the code know if you are putting in a 1 or 2 or 3 digit number?
Would it be better to store each key press and look for a # press to be the equivalent of ENTER/CALCULATE?
You wouldn't need all these IF statements to check for number of digits and do the calculation?
And you would be able to input any number of digits.
#include "Keypad.h"
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {12, 11, 10, 9}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
int VARone = 0;
int VARtwo = 0 ;
int VARthree = 0;
int One1 = 0;
int Two2 = 0;
int Three3 = 0;
int total = 0;
int RUN = 0;
int varcalc = 1;
void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(2,INPUT);
pinMode(5,OUTPUT);
pinMode(4,OUTPUT);
}
void loop()
{
// put your main code here, to run repeatedly:
char key = keypad.getKey();
Serial.print(total);
if (digitalRead(2))//Mode Loop
{
varcalc = 0;
VARone = 0;
VARtwo = 0;
VARthree = 0;
One1 = 0;
Two2 = 0;
Three3 = 0;
total = 0;
}
else
{
//Keypad closed loop
Serial.print(key);
if((VARone)==(0))//Test if variable one is equal to 0
{
//Get key from 0 - 9 and set it to variable one
delay(11);
char key = keypad.getKey();
if(key != NO_KEY)
{
One1 = (key);
varcalc = 0;
VARone = 1;
}
}
else
{
if(((VARone)==(1))&&((VARtwo)==(0)))//Test variable one is equal to 1, whilst variable two is equal to 0
{
//Get key from 0 - 9 and set it to variable two
delay(11);
char key = keypad.getKey();
if(key != NO_KEY)
{
Two2 = (key);
varcalc = 0;
VARtwo = 1;
}
}
else
{
if(((VARone)==(1))&&((VARtwo)==(1))&&((VARthree)==(0)))//Test variable one and variable two is equal to 1, whilst variable three is equal to 0
{
//Get key from 0 - 9 and set it to variable three
delay(11);
char key = keypad.getKey();
if(key != NO_KEY)
{
Three3 = (key);
varcalc = 0;
VARthree = 1;
}
}
}
if((varcalc)==(0))//Calculation Loop
{
if((VARone)==(1))//Runs calculation when there is only one variable
{
total = (One1);//Takes one inputted number like; '5'
varcalc = 1;
}
else
{
if(((VARone) == (1)) && ((VARtwo)==(1)))//Runs calculation when there is only two variables
{
total = (((One1)*(10))+((Two2)));//Takes two inputted numbers and calculates like; '60'
varcalc = 1;
}
else
{
if(((VARone)==(1))&&((VARtwo)==(1))&&((VARthree)==(1)))//Runs calculation when there is only three variables
{
total = ((((One1)*(100) )+((Two2)*(10)))+(Three3));//Takes three inputted numbers and calculates like; '111'
varcalc = 1;
}
else
{
varcalc = 1;
}
}
}
}
else//loop
{
digitalWrite(4,HIGH);
analogWrite(5,255);
delay(11);
analogWrite(5,0);
digitalWrite(4,LOW);
delay((60000)/(total));//makes a delay, e.g. 60000/111 = 540.540540541ms
}
}
}
}
#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'#','0','*'}
};
byte rowPins[ROWS] = {12, 11, 10, 9}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
int VARone = 0;
int VARtwo = 0 ;
int VARthree = 0;
int One1 = 0;
int Two2 = 0;
int Three3 = 0;
int total = 0;
int RUN = 0;
int varcalc = 1;
void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(2,INPUT);
pinMode(5,OUTPUT);
pinMode(4,OUTPUT);
}
void loop()
{
// put your main code here, to run repeatedly:
char key = keypad.getKey();
Serial.println(total);
if (digitalRead(2))//Reset Loop
{
varcalc = 1;
VARone = 0;
VARtwo = 0;
VARthree = 0;
One1 = 0;
Two2 = 0;
Three3 = 0;
total = 0;
}
else
{
if((varcalc)==(1))//Keypad Loop
{
if((VARone)==(0))//Test if variable one is equal to 0
{
//Get key from 0 - 9 and set it to variable one
char key = keypad.getKey();
if(key != NO_KEY)
{
One1 = key;
varcalc = 0;
VARone = 1;
}
}
else
{
if(((VARone)==(1))&&((VARtwo)==(0)))//Test variable one is equal to 1, whilst variable two is equal to 0
{
//Get key from 0 - 9 and set it to variable two
char key = keypad.getKey();
if(key != NO_KEY)
{
Two2 = key;
varcalc = 0;
VARtwo = 1;
}
}
else
{
if(((VARone)==(1))&&((VARtwo)==(1))&&((VARthree)==(0)))//Test variable one and variable two is equal to 1, whilst variable three is equal to 0
{
//Get key from 0 - 9 and set it to variable three
char key = keypad.getKey();
if(key != NO_KEY)
{
Three3 = key;
varcalc = 0;
VARthree = 1;
}
}
}
}
}
else
{
if((varcalc)==(0))//Calculation Loop
{
if((VARone)==(1))//Runs calculation when there is only one variable
{
total = (One1);//Takes one inputted number like; '5'
varcalc = 1;
}
else
{
if(((VARone) == (1)) && ((VARtwo)==(1)))//Runs calculation when there is only two variables
{
total = (((One1)*(10))+((Two2)));//Takes two inputted numbers and calculates like; '60'
varcalc = 1;
}
else
{
if(((VARone)==(1))&&((VARtwo)==(1))&&((VARthree)==(1)))//Runs calculation when there is only three variables
{
total = ((((One1)*(100) )+((Two2)*(10)))+(Three3));//Takes three inputted numbers and calculates like; '111'
varcalc = 1;
}
else
{
varcalc = 1;
}
}
}
}
else//loop
{
digitalWrite(4,HIGH);
analogWrite(5,255);
delay(11);
analogWrite(5,0);
digitalWrite(4,LOW);
delay((60000)/(total));//makes a delay, e.g. 60000/111 = 540.540540541ms
}
}
}
}
#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'#','0','*'}
};
byte rowPins[ROWS] = {12, 11, 10, 9}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
int VARone = 0;
int VARtwo = 0 ;
int VARthree = 0;
int One1 = 0;
int Two2 = 0;
int Three3 = 0;
int total = 0;
void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(2,INPUT);
pinMode(5,OUTPUT);
pinMode(4,OUTPUT);
}
void loop()
{
// put your main code here, to run repeatedly:
if (digitalRead(2))//Reset Loop
{
VARone = (0);
VARtwo = (0);
VARthree = (0);
One1 = (0);
Two2 = (0);
Three3 = (0);
total = (0);
}
else
{
if(digitalRead(3))//Calculation loop
{
if(VARone == 1)//Runs calculation when there is only one variable
{
total = (One1);//Takes one inputted number like; '5'
}
else
{
if((VARone == 1)&&(VARtwo == 1))//Runs calculation when there is only two variables
{
total = (((One1)*(10))+((Two2)));//Takes two inputted numbers and calculates like; '60'
}
else
{
if((VARone == 1)&&(VARtwo == 1)&&(VARthree== 1))//Runs calculation when there is only three variables
{
total = ((((One1)*(100))+((Two2)*(10)))+(Three3));//Takes three inputted numbers and calculates like; '111'
}
}
}
}
else//keypad loop
{
char key = keypad.getKey();
Serial.println(One1);
delay(11);
if((VARone)==(0))//Test if variable one is equal to 0
{
//Get key from 0 - 9 and set it to variable one
char key = keypad.getKey();
if(key != NO_KEY)
{
One1==(key);
VARone==1;
}
}
else
{
delay(11);
if(((VARone)==(1))&&((VARtwo)==(0)))//Test variable one is equal to 1, whilst variable two is equal to 0
{
//Get key from 0 - 9 and set it to variable two
char key = keypad.getKey();
if(key != NO_KEY)
{
Two2==(key);
VARtwo==1;
}
}
else
{
delay(11);
if(((VARone)==(1))&&((VARtwo)==(1))&&((VARthree)==(0)))//Test variable one and variable two is equal to 1, whilst variable three is equal to 0
{
//Get key from 0 - 9 and set it to variable three
char key = keypad.getKey();
if(key != NO_KEY)
{
Three3==(key);
VARthree==1;
}
}
else//loop
{
digitalWrite(4,HIGH);
analogWrite(5,255);
delay(11);
analogWrite(5,0);
digitalWrite(4,LOW);
delay((60000)/(total));//makes a delay, e.g. 60000/111 = 540.540540541ms
}
}
}
}
}
}
Hello Everyone I am very grateful for the help and I have the full working code for you guys for this is a silent metronome which vibrates to a BPM and will help other people will find this useful. Thanks for this everyone...
CODE PT.1? V.F
#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {12, 11, 10, 9}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
int VARdelay = 0;
int VARone = 0;
int VARtwo = 0;
int VARthree = 0;
int One1 = 0;
int Two2 = 0;
int VARtotal = 0;
void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(2,INPUT);
pinMode(5,OUTPUT);
pinMode(4,OUTPUT);
}
void loop()
{
// put your main code here, to run repeatedly:
if((VARone)==(0))//Variable One: Calcuation and keypad setting from 0-9
{
delay(11);
char key = keypad.getKey();
Serial.println(VARdelay);
if (key)
{
switch(key)
{
case '#':
VARone = 1;
VARtwo = 1;
VARthree =1;
break;
case '0'://set variable one to this number
One1 = 0;
VARdelay = 60000;//set delay
VARone = 1;
break;
case '1'://set variable one to this number
One1 = 1;
VARdelay = ((60000)/(1));//set delay
VARone = 1;
break;
case '2'://set variable one to this number
One1 = 2;
VARdelay = ((60000)/(2));//set delay
VARone = 1;
break;
case '3'://set variable one to this number
One1 = 3;
VARdelay = ((60000)/(3));//set delay
VARone = 1;
break;
case '4'://set variable one to this number
One1 = 4;
VARdelay = ((60000)/(4));//set delay
VARone = 1;
break;
case '5'://set variable one to this number
One1 = 5;
VARdelay = ((60000)/(5));//set delay
VARone = 1;
break;
case '6'://set variable one to this number
One1 = 6;
VARdelay = ((60000)/(6));//set delay
VARone = 1;
break;
case '7'://set variable one to this number
One1 = 7;
VARdelay = ((60000)/(7));//set delay
VARone = 1;
break;
case '8'://set variable one to this number
One1 = 8;
VARdelay = ((60000)/(8));//set delay
VARone = 1;
break;
case '9'://set variable one to this number
One1 = 9;
VARdelay = ((60000)/(9));//set delay
VARone = 1;
break;
}
}
}
else
{
if(((VARone)==(1))&&((VARtwo)==(0)))//Variable Two: Calculation and keypad setting from 0-9
{
delay(11);
char key = keypad.getKey();
Serial.println(VARdelay);
if (key)
{
switch (key)
{
case '#':
VARtwo = 1;
VARthree =1;
break;
case '0'://set variable one to this number
Two2 = 0;
VARtotal = (((One1)*(10))+(0));//Caculation. If there is a previous calculation overwrite it
VARdelay = ((60000)/(VARtotal));//Set delay. If there is a previous delay overwrite it
VARtwo = 1;
break;
case '1'://set variable one to this number
Two2 = 1;
VARtotal = (((One1)*(10))+(1));//Caculation. If there is a previous calculation overwrite it
VARdelay = ((60000)/(VARtotal));//Set delay. If there is a previous delay overwrite it
VARtwo = 1;
break;
case '2'://set variable one to this number
Two2 = 2;
VARtotal = (((One1)*(10))+(2));//Caculation. If there is a previous calculation overwrite it
VARdelay = ((60000)/(VARtotal));//Set delay. If there is a previous delay overwrite it
VARtwo = 1;
break;
case '3'://set variable one to this number
Two2 = 3;
VARtotal = (((One1)*(10))+(3));//Caculation. If there is a previous calculation overwrite it
VARdelay = ((60000)/(VARtotal));//Set delay. If there is a previous delay overwrite it
VARtwo = 1;
break;
case '4'://set variable one to this number
Two2 = 4;
VARtotal = (((One1)*(10))+(4));//Caculation. If there is a previous calculation overwrite it
VARdelay = ((60000)/(VARtotal));//Set delay. If there is a previous delay overwrite it
VARtwo = 1;
break;
case '5'://set variable one to this number
Two2 = 5;
VARtotal = (((One1)*(10))+(5));//Caculation. If there is a previous calculation overwrite it
VARdelay = ((60000)/(VARtotal));//Set delay. If there is a previous delay overwrite it
VARtwo = 1;
break;
case '6'://set variable one to this number
Two2 = 6;
VARtotal = (((One1)*(10))+(6));//Caculation. If there is a previous calculation overwrite it
VARdelay = ((60000)/(VARtotal));//Set delay. If there is a previous delay overwrite it
VARtwo = 1;
break;
case '7'://set variable one to this number
Two2 = 7;
VARtotal = (((One1)*(10))+(7));//Caculation. If there is a previous calculation overwrite it
VARdelay = ((60000)/(VARtotal));//Set delay. If there is a previous delay overwrite it
VARtwo = 1;
break;
case '8'://set variable one to this number
Two2 = 8;
VARtotal = (((One1)*(10))+(8));//Caculation. If there is a previous calculation overwrite it
VARdelay = ((60000)/(VARtotal));//Set delay. If there is a previous delay overwrite it
VARtwo = 1;
break;
case '9'://set variable one to this number
Two2 = 9;
VARtotal = (((One1)*(10))+(9));//Caculation. If there is a previous calculation overwrite it
VARdelay = ((60000)/(VARtotal));//Set delay. If there is a previous delay overwrite it
VARtwo = 1;
break;
}
}
}
else
{
if(((VARone)==(1))&&((VARtwo)==(1))&&((VARthree)==(0)))//Variable Three: Calculation and keypad setting from 0-9
{
delay(11);
char key = keypad.getKey();
Serial.println(VARdelay);
if (key)
{
switch (key)
{
case '#':
VARthree = 1;
break;
case '0'://set variable one to this number
VARtotal = (((One1)*(100))+((Two2)*(10))+(0));//Caculation. If there is a previous calculation overwrite it
VARdelay =((60000)/(VARtotal));//Set delay. If there is a previous delay overwrite it
VARthree = 1;
break;
case '1'://set variable one to this number
VARtotal = (((One1)*(100))+((Two2)*(10))+(1));//Caculation. If there is a previous calculation overwrite it
VARdelay =((60000)/(VARtotal));//Set delay. If there is a previous delay overwrite it
VARthree = 1;
break;
case '2'://set variable one to this number
VARtotal = (((One1)*(100))+((Two2)*(10))+(2));//Caculation. If there is a previous calculation overwrite it
VARdelay =((60000)/(VARtotal));//Set delay. If there is a previous delay overwrite it
VARthree = 1;
break;
case '3'://set variable one to this number
VARtotal = (((One1)*(100))+((Two2)*(10))+(3));//Caculation. If there is a previous calculation overwrite it
VARdelay =((60000)/(VARtotal));//Set delay. If there is a previous delay overwrite it
VARthree = 1;
break;
case '4'://set variable one to this number
VARtotal = (((One1)*(100))+((Two2)*(10))+(4));//Caculation. If there is a previous calculation overwrite it
VARdelay =((60000)/(VARtotal));//Set delay. If there is a previous delay overwrite it
VARthree = 1;
break;
case '5'://set variable one to this number
VARtotal = (((One1)*(100))+((Two2)*(10))+(5));//Caculation. If there is a previous calculation overwrite it
VARdelay =((60000)/(VARtotal));//Set delay. If there is a previous delay overwrite it
VARthree = 1;
break;
case '6'://set variable one to this number
VARtotal = (((One1)*(100))+((Two2)*(10))+(6));//Caculation. If there is a previous calculation overwrite it
VARdelay =((60000)/(VARtotal));//Set delay. If there is a previous delay overwrite it
VARthree = 1;
break;
case '7'://set variable one to this number
VARtotal = (((One1)*(100))+((Two2)*(10))+(7));//Caculation. If there is a previous calculation overwrite it
VARdelay =((60000)/(VARtotal));//Set delay. If there is a previous delay overwrite it
VARthree = 1;
break;
case '8'://set variable one to this number
VARtotal = (((One1)*(100))+((Two2)*(10))+(8));//Caculation. If there is a previous calculation overwrite it
VARdelay =((60000)/(VARtotal));//Set delay. If there is a previous delay overwrite it
VARthree = 1;
break;
case '9'://set variable one to this number
VARtotal = (((One1)*(100))+((Two2)*(10))+(9));//Caculation. If there is a previous calculation overwrite it
VARdelay =((60000)/(VARtotal));//Set delay. If there is a previous delay overwrite it
VARthree = 1;
break;
}
}
}
else
{
if(digitalRead(2))//Reset all int
{
VARone = 0;
VARtwo = 0;
VARthree = 0;
One1 = 0;
Two2 = 0;
VARtotal = 0;
VARdelay = 0;
}
else
{
Serial.println(1337);//LEET
Serial.println(VARdelay);
digitalWrite(5,HIGH);
digitalWrite(4,255);
delay(36);
digitalWrite(4,0);
digitalWrite(5,LOW);
delay(VARdelay);
Serial.println(1337);//LEET
}
}
}
}
}