I have coded the following in the context of controlling how long a small electric car runs.
const int start = 2;
const int ledpin = 13;
const int motorcontrol = 4;
int count = 0;
int tens = 10;
int ones = 1;
int tenths = .1;
int hundredths = .01;
int Time = 0;
int buttonstate = digitalRead(start);
#include <Keypad.h>
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 };
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup() {
pinMode(ledpin, OUTPUT);
pinMode(start, INPUT);
Serial.begin(9600);
}
void loop() {
buttonstate = digitalRead(start);
if (count <= 4) {
char key = kpd.waitForKey();
if(key){ // Check for a valid key.
switch (key){
default :
digitalWrite(ledpin, HIGH);
delay(1000*key);
digitalWrite(ledpin, LOW);
count = count + 1;
}
if (count == 1) {
tens = tens*key ;}
if (count == 2) {
ones = ones*key ;}
if (count == 3) {
tenths = tenths*key ;}
if (count == 4){
hundredths = hundredths*key;
}
}
Time = tens + ones + tenths + hundredths ; }
else {
digitalWrite(ledpin, LOW);}
if (buttonstate == HIGH && count < 5) {
digitalWrite(ledpin, HIGH);
delay(1000*Time);
}
else {
digitalWrite(ledpin, LOW);
}
}
Pin 13 is the output, and it should be outputing for time Time*1000, but it's not outputing anything when the buttonstate is high. It is also accepting 5 numbers to add, as opposed to the 4 I thought it would. Where did I go Wrong?