I am trying to code something that tells the user to input the number 20 from the keypad and when the he enters it correctly it will turn pin 13 high for 100ms or something like that. and when the user inputs the number 200 it will turn pin 13 high for longer time say 1000ms.
Now, I am testing a simple code to see if it will at least respond to the user's input first. Note the code is not my final code.
The problem I am getting is the following:
When I run the code it spits an error "duplicate case value"
See the screen shot for the code I have.
I had to take two screen shots to get the whole thing
Thanks
-
Why didn't you read the highlighted sticky at the top of the forum - that says read this before posting a programming question. Someone took the time to write it, in the hope that people find it helpful.
-
Please post your code in `` tags. It means people can read it.
-
In C++, (and the arduino language is C++), a value thus 'a' is a character literal. A value this '0x41' is also a character literal. I think your variable holding (the character literal) might be an signed char, which cannot store more than 128, which might explain the error. I cannot read the .png well so am guessing a bit.
4 Do you expect the keypad to return a '2', a '0', a '0'? (3 characters), or will it return (integer) 200 if you press 2,0,0?
What does the documentation for the library say?
5 Put some debug printing in, to see what the keypad is actually giving.
6 You might want to set the LED pin to an output.
Sorry here is this code again:
I just want to implement the idea: if the users inputs 20 it will turn pin13 high for a period of time and when the user inputs 200 it will turn pin 13 high for a longer period than the 20 case.
As I mentioned in my previous post. My code is not complete. Yes I need to declare pin 13. But for now I just trying to get something from the input and later I'll use the input to do what I want.
I am just having problem.
I want to get and integer 20 when user inputs 20 and integer 200 when user inputs 200.
Thank
[#include <Keypad.h>
//new code start here
int k, amCount=0;
//new code end here
const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
{
'1','2','3' }
,
{
'4','5','6' }
,
{
'7','8','9' }
,
{
'*','0','#' }
};
// 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] = {
12, 11, 10 };
// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
#define ledpin 13
void setup()
{
digitalWrite(ledpin, HIGH);
Serial.begin(9600);
}
void loop()
{
char key = kpd.getKey();
if(key) // Check for a valid key.
{
switch (key)
{
case '100':
digitalWrite(ledpin, LOW);
break;
case '3':
digitalWrite(ledpin, HIGH);
break;
default:
Serial.println(key);
}
}
}
]
I assume you followed this tutorial:
http://www.arduino.cc/playground/Code/Keypad
As the tutorial says, the function getKey() returns the key that is pressed. Not a succession of keys.
If you expect someone to type 20, then you will have to call getKey() twice and check for for the sequence 2, then 0.
I guess you can achive that with nested if checking char by char
'100' is nonsense.
BZZZT!
unsigned long oneHundred = '100';
is perfectly valid C/C++.
Sometimes, it can even be useful, though I agree, in this case (no pun intended), useless.