Show Posts
Pages: 1 [2]
16  Using Arduino / Programming Questions / Re: Recording switch press sequences problem on: July 09, 2011, 04:01:55 pm
the 328
17  Using Arduino / Programming Questions / Recording switch press sequences problem on: July 09, 2011, 03:54:17 pm
Hello,
I'm trying to make a controller that records a switch press sequence on four switches and plays them back when triggered.  I have went through several examples and tutorials trying to make this work and thought I had it down but when I upload and try and run the sketch it doesn't seem to work (I bet we are all very familiar with that  smiley-eek)  I have been attempting to debug it for hours and cant seem to nail down how to fix the problem, though I think I know about where it is.
Project background:
There are 4 switches which are recorded, one for trigger, and one for record connected on pins 2-7.  The switches are momentary NO tactile push-buttons.   The LED's that indicate when a output is high are on 8-11, the record indicator light is on 12 and play indicator is on 13.  When the record switch is pressed and released the arduino (duemilanove 328) records the sequence of the 4 programming switches storing the times they are pressed as well in a 2d array.  When the trigger is pressed it plays back the switch sequence.  All of the switches are on pullup resistors and wait for a LOW signal.  
I have included the whole sketch below and have tried to comment it such that it is easy to follow.  
Code:
// I/O pin varible config
int but1 = 2;
int but2 = 3;
int but3 = 4;
int but4 = 5;
int trig1= 6;  
int rec  = 7;
int out1 = 8;
int out2 = 9;
int out3 = 10;
int out4 = 11;
int pla_lite = 12;
int rec_lite = 13;
bool show_pres = false;
unsigned long  show_seq[100][3];
int rButState;
int tButState;
int but1State;
int but2State;
int but3State;
int but4State;

void setup(){
  //pinMode config
  pinMode(rec,INPUT);
  pinMode(but1,INPUT);
  pinMode(but2,INPUT);
  pinMode(but3,INPUT);
  pinMode(but4,INPUT);
  pinMode(trig1,INPUT);
  pinMode(out1,OUTPUT);
  pinMode(out2,OUTPUT);
  pinMode(out3,OUTPUT);
  pinMode(out4,OUTPUT);
  pinMode(rec_lite,OUTPUT);
  pinMode(pla_lite,OUTPUT);
  //read initial button states
  rButState = digitalRead(rec);
  tButState = digitalRead(trig1);
  but1State = digitalRead(but1);
  but2State = digitalRead(but2);
  but3State = digitalRead(but3);
  but4State = digitalRead(but4);

}

void loop(){
  int rval;                                                         //
  int rval2;
  int tval;
  int tval2;
  digitalWrite(out1, LOW);                                          //Make sure outputs are low
  digitalWrite(out2, LOW);
  digitalWrite(out3, LOW);
  digitalWrite(out4, LOW);                      
  tval = digitalRead(trig1);                                        //Read Trigger State
  delay(10);                                                        //Debounce
  tval2 = digitalRead(trig1);                                       //
  if (tval == tval2 && tval == LOW && tval != tButState && show_pres == true){     //Trigger show if one is recorded
    playShow();
    tButState = tval;
  }
  rval = digitalRead(rec);                                          //Read REC button state
  delay(10);                                                        //Debounce
  rval2 = digitalRead(rec);                                         //
  if (rval == rval2 && rval == LOW && rval != rButState){           //Record show
    recordShow();
    rButState = rval;
  }

}

void recordShow(){                                                  //Record show function
  int b1val;                                                        //---------------------
  int b1val2;                                                       //
  int b2val;                                                        //
  int b2val2;                                                       //
  int b3val;                                                        //Comparison values of buttons
  int b3val2;                                                       //for debounce
  int b4val;                                                        //
  int b4val2;                                                       //
  int rval;                                                         //
  int rval2;                                                        //---------------------
  unsigned long start_time = millis();                              //Log time recording starts
  bool isrec = true;
  memset(show_seq, 0, sizeof(show_seq[0][0])*300*3);                //Clear show
  int counter = 0;
  digitalWrite(rec_lite,HIGH);                                      //Turn on REC light
  while (isrec == true){                                            //Keep looping until record is pressed again
    b1val = digitalRead(but1);
    delay(10);
    b1val2 = digitalRead(but1);
    if (b1val == b1val2 && b1val == LOW && b1val != but1State){   //Is button 1 pressed?
      digitalWrite(out1, HIGH);                                   //Echo button press on output
      show_seq[counter][1] = out1;                                //Register the button pressed in 1st column
      if (counter == 0){
        show_seq[counter][2] = millis() - start_time;             //Register time until button is pressed from start if first button press
      }
      else{
        show_seq[counter][2] = millis() - show_seq[counter-1][3]; //Register time since last button press
      }
      while (digitalRead(but1)==0){
        delay(10);
      }                                                           //Wait until button is released
      show_seq[counter][3] = millis() -  show_seq[counter][2];    //Register time button is held
      digitalWrite(out1, LOW);
      counter++;
      but1State = b1val;
    }
  }
  b2val = digitalRead(but2);
  delay(10);
  b2val2 = digitalRead(but2);
  if (b2val == b2val2 && b2val == LOW && b2val != but2State){
    digitalWrite(out2, HIGH);
    show_seq[counter][1] = out2;
    if (counter == 0){
      show_seq[counter][2] = millis() - start_time;
    }
    else{
      show_seq[counter][2] = millis() - show_seq[counter-1][3];
    }
    while (digitalRead(but1)==0){
      delay(10);
    }
    show_seq[counter][3] = millis() -  show_seq[counter][2];
    digitalWrite(out2, LOW);
    counter++;
    but2State = b2val;
  }
  show_pres = true;

  b3val = digitalRead(but3);
  delay(10);
  b3val2 = digitalRead(but3);
  if (b3val == b3val2 && b3val == LOW && b3val != but3State){
    digitalWrite(out3, HIGH);
    show_seq[counter][1] = out3;
    if (counter == 0){
      show_seq[counter][2] = millis() - start_time;
    }
    else{
      show_seq[counter][2] = millis() - show_seq[counter-1][3];
    }
    while (digitalRead(but1)==0){
      delay(10);
    }
    show_seq[counter][3] = millis() -  show_seq[counter][2];
    digitalWrite(out3, LOW);
    counter++;
    but3State = b3val;
  }

  b4val = digitalRead(but4);
  delay(10);
  b4val2 = digitalRead(but4);
  if (b4val == b4val2 && b4val == LOW && b4val != but4State){
    digitalWrite(out4, HIGH);
    show_seq[counter][1] = out4;
    if (counter == 0){
      show_seq[counter][2] = millis() - start_time;
    }
    else{
      show_seq[counter][2] = millis() - show_seq[counter-1][3];
    }
    while (digitalRead(but1)==0){
      delay(10);
    }
    show_seq[counter][3] = millis() -  show_seq[counter][2];
    digitalWrite(out4, LOW);
    counter++;
    but4State = b4val;
  }

  rval = digitalRead(rec);
  delay(10);
  rval2 = digitalRead(rec);
  if (rval == rval2 && rval == LOW && rval != rButState){                    //Trigger show if one is recorded
    isrec = false;
    rButState = rval;
  }

  digitalWrite(rec_lite,LOW);  
}

void playShow(){                                                    //Play the show function
  int counter = 0;
  digitalWrite(pla_lite,HIGH);                                      //Turn on play light
  while (show_seq[counter][1] > 0){                                 //Play show as long as there is a button value present
    delay(show_seq[counter][2]);
    digitalWrite(show_seq[counter][1], HIGH);
    delay(show_seq[counter][3]);
    digitalWrite(show_seq[counter][1], LOW);
    counter++;
  }
  digitalWrite(pla_lite,LOW);                                       //Turn off play light
}

I am not the best programmer so the code may not be very efficient and it might not follow proper code etiquette,  but the problem seems to be in the playShow and recordShow functions and I may have not debounced properly because commenting the body's of the two if statements in the main loop and adding a command to send "test" to the serial monitor instead works.  I modeled the debounce after the adafruit tutorial.  I'm sure the mistake is elementary and noobish but appreciate any help anyone could give.  I'm kinda at a brick wall on how to fix this.
Pages: 1 [2]