abrookfield:
try this as a first attempt
/*
==========
KICKMASTER
switches should be tied to gnd
DO NOT FORGET LED'S NEED CURRENT LIMIT RESISTOR
0, 1, 2, 3 = kick bag switches
4, 5, 6, 7 = led's for kick switches
8, 9, 10, 11 = time select switches
12 = go switch (send low -> high to activate) - LED13 lights when running
Start switch must be cycled for sequence to start again
MISSING THINGS:
storage/display of time taken to kick
confirmation of good kick
confirmation of WRONG kick
portions ripped from adafruit samples
*/
#define DEBOUNCE 5 // kick input button debouncer
byte buttons[] = {0, 1, 2, 3, 12}; // 0-4 = kick buttons, 12=start button
byte kickMe[] = {4, 5, 6, 7}; // LEDs to light on kick enabled
byte whichTime[] = {8, 9, 10, 11}; // pins to use for time select
int KickSwitchTime[] = {900, 1800, 3600, 7200}; // allow kick switch input time selector (multiples of 16ms)
int kicked, sw_enabled, tryInput, nextKick, currentTime, kickTimer, j, timeInput;
// This handy macro lets us determine how big the array up above is, by checking the size
#define NUMBUTTONS sizeof(buttons)
// we will track if a button is just pressed, just released, or 'currently pressed'
volatile byte pressed[NUMBUTTONS], justpressed[NUMBUTTONS], justreleased[NUMBUTTONS];
void setup() {
byte i;
// set up serial port
Serial.begin(9600);
Serial.print("Button checker with ");
Serial.print(NUMBUTTONS, DEC);
Serial.println(" buttons");
// pin13 LED
pinMode(13, OUTPUT);
// Make input & enable pull-up resistors on switch pins
for (i=0; i< NUMBUTTONS; i++) {
pinMode(buttons[i], INPUT);
digitalWrite(buttons[i], HIGH);
pinMode(KickSwitchTime[i], INPUT);
digitalWrite(KickSwitchTime[i], HIGH);
pinMode(kickMe[i], OUTPUT);
digitalWrite(kickMe[i], LOW);
}
// Run timer2 interrupt every 15 ms
TCCR2A = 0;
TCCR2B = 1<<CS22 | 1<<CS21 | 1<<CS20;
//Timer2 Overflow Interrupt Enable
TIMSK2 |= 1<<TOIE2;
}
SIGNAL(TIMER2_OVF_vect) {
check_switches();
}
void check_switches()
{
static byte previousstate[NUMBUTTONS];
static byte currentstate[NUMBUTTONS];
static long lasttime;
byte index;
kickTimer++;
currentTime++;
if (millis() < lasttime) {
// we wrapped around, lets just try again
lasttime = millis();
}
if ((lasttime + DEBOUNCE) > millis()) {
// not enough time has passed to debounce
return;
}
// ok we have waited DEBOUNCE milliseconds, lets reset the timer
lasttime = millis();
for (index = 0; index < NUMBUTTONS; index++) {
currentstate[index] = digitalRead(buttons[index]); // read the button
/*
Serial.print(index, DEC);
Serial.print(": cstate=");
Serial.print(currentstate[index], DEC);
Serial.print(", pstate=");
Serial.print(previousstate[index], DEC);
Serial.print(", press=");
*/
if (currentstate[index] == previousstate[index]) {
if ((pressed[index] == LOW) && (currentstate[index] == LOW)) {
// just pressed
justpressed[index] = 1;
}
else if ((pressed[index] == HIGH) && (currentstate[index] == HIGH)) {
// just released
justreleased[index] = 1;
}
pressed[index] = !currentstate[index]; // remember, digital HIGH means NOT pressed
}
//Serial.println(pressed[index], DEC);
previousstate[index] = currentstate[index]; // keep a running tally of the buttons
}
}
void loop() {
digitalWrite(13,HIGH); delay(1000);
digitalWrite(13,LOW); delay(1000);
digitalWrite(13,HIGH); delay(1000);
digitalWrite(13,LOW); delay(1000);
kicked = 1;
nextKick=0;
kickTimer=0;
//if start button already low, wait for it to go high
while(pressed[4]==LOW);
// wait for start button to go low
while (pressed[4]==HIGH);
// lets see whick time scenario to use
for (j=0; j < 4; j++){
digitalWrite(whichTime[j],HIGH);
if (digitalRead(whichTime[j])==LOW) timeInput=j;
}
currentTime=0;
// main timer loop
while(currentTime < KickSwitchTime[timeInput]){
digitalWrite(13,HIGH);
if (kicked==1){
// choose a new position to kick
nextKick=random(1,4);
kickTimer=0;
digitalWrite(kickMe[nextKick], HIGH);
}
for (byte i = 0; i < NUMBUTTONS; i++){
if ( (pressed[i]) && (i==nextKick) ){
// we got a good kick, lets move on
kicked=1;
// kickTimer = multiples of 16mS
// save kickTimer somewhere
// turn off current led
digitalWrite(kickMe[nextKick], LOW);
}
}
}
}
Dude, you are the best! Thats extremely utterly helpful, I'm very grateful i would never be able to code such thing on my own. That is a great help, a huge kick-start although I'll need to sit my ass down and try to understand what everything does. Even so, just a confirmation of a good kick will be super enough for me. If there is a possible way my team can donate to you please tell me