Go Down

Topic: change mode when button press help (Read 645 times) previous topic - next topic

packingtape

hello, i just need a little help on my coding, i have 2 set of mode of led blinks with sounds, with two buttons the 1st button is for executing the program and the sencond button is for changign mode, what i want is when the arduino starts the mode 1 select when i press the button#1 the sound file 1play  and then blink the led then off. when i press the button#2 it change to mode two, when i press the button#1 the other sound file with is the soundfile #2 play then the led blink the off. :) thanks
i think i have a problem on the debounce code.


/
This code is show how Arduino Wave Module works with Arduino.
Code is not optimized. Any improving work on it is encouraged.
*/
int RST = 3;
int CLK = 9;
int DAT = 8;
int switchPin = 4;
int ledPin = 11;   
int switchPin2 = 10;
int button = 0;
//mode select code
int val;                        // variable for reading the pin status
int val2;                       // variable for reading the delayed/debounced status
int buttonState;
int lightMode = 0;
           

// mode 1
void mode1(){

 
  if(digitalRead(switchPin) == HIGH && button == 0){ /* if D2 is HIGH */
  delay(100);  /* Soft Debounce delay for 500mS */
 
   button = 1;
//sound
 
  send(0x0001);//play file 0000
  delay(100);
  send(0xfff7);//set voice volumn to 7
  delay(500);
   
  //light
analogWrite(ledPin, 5);
  delay(300);
  analogWrite(ledPin, 10);
  delay(300);
  analogWrite(ledPin, 15);
  delay(300);
  analogWrite(ledPin, 20);
  delay(300);
  analogWrite(ledPin, 25);
  delay(200);
  analogWrite(ledPin, 30);
  delay(200);
  analogWrite(ledPin, 35);
  delay(200);
  analogWrite(ledPin, 40);
  delay(200);
  analogWrite(ledPin, 45);
  delay(500);
 
  analogWrite(ledPin, 50);
  delay(400);

 
analogWrite(ledPin, 255);
delay(100);
analogWrite(ledPin, 0);
delay(100);
analogWrite(ledPin, 255);
delay(100);

analogWrite(ledPin,20);
// light ^
send(0xfffe);// pause



  } 
  else if(digitalRead(switchPin) == LOW && button == 1){
    button = 0;
   // light
   analogWrite(ledPin, 20);
  delay(300);
  analogWrite(ledPin, 18);
  delay(300);
  analogWrite(ledPin, 16);
  delay(300);
  analogWrite(ledPin, 14);
delay(300);
  analogWrite(ledPin, 12);
delay(300);


  analogWrite(ledPin, 10);
  delay(300);
  analogWrite(ledPin, 8);
  delay(300);
  analogWrite(ledPin, 6);
  delay(300);
  analogWrite(ledPin, 4);
delay(300);
  analogWrite(ledPin, 2);
delay(300);

analogWrite(ledPin,0);
     
 
      send(0x0003);
      delay(350);
    send(0xfffe);// pause

    send(0xfff7);//set voice volumn to 7
 

}
}

void mode2()
{
   if(digitalRead(switchPin) == HIGH && button == 0){ /* if D2 is HIGH */
  delay(200);  /* Soft Debounce delay for 500mS */
 
   button = 1;
//sound
 
  send(0x0002);//play file 0000
  delay(100);//delay 10 seconds
 
//light
analogWrite(ledPin, 5);
  delay(100);
  analogWrite(ledPin, 20);
  delay(100);
  analogWrite(ledPin, 40);
  delay(100);
  analogWrite(ledPin, 50);
  delay(100);
  analogWrite(ledPin, 70);
  delay(100);

 
analogWrite(ledPin, 255);
delay(100);
analogWrite(ledPin, 0);
delay(100);
analogWrite(ledPin, 255);
delay(100);
analogWrite(ledPin,60);

//stop
  delay( 200);
  send(0xfff7);//set voice volumn to 7
  delay(300);
   


send(0xfffe);// pause

  } 
  else if(digitalRead(switchPin) == LOW && button == 1){
    button = 0;
  analogWrite(ledPin, 20);
  delay(100);

  analogWrite(ledPin, 12);
  delay(100);


  analogWrite(ledPin, 6);
  delay(100);
  analogWrite(ledPin, 5);

 
 
}
}

void setup() {   
   
   
    pinMode(RST, OUTPUT);
    pinMode(CLK, OUTPUT);
    pinMode(DAT, OUTPUT);
    pinMode(switchPin, INPUT);
    pinMode(ledPin, OUTPUT);
    digitalWrite(button, LOW); /* Set button initially as LOW */
    Serial.begin(9600);
    digitalWrite(RST, HIGH);
    digitalWrite(CLK, HIGH);
    digitalWrite(DAT, HIGH);
    pinMode(switchPin2, INPUT);
    digitalWrite(RST, LOW);
    delay(5);
    digitalWrite(RST, HIGH);
    delay(300);
buttonState = digitalRead(switchPin2);

}




void loop() {
val = digitalRead(switchPin2);      // read input value and store it in val
  delay(10);                         // 10 milliseconds is a good amount of time
  val2 = digitalRead(switchPin2);     // read the input again to check for bounces
  if (val == val2) {                 // make sure we got 2 consistant readings!
    if (val != buttonState) {          // the button state has changed!
      if (val == LOW) {                // check if the button is pressed
        if (lightMode == 0) {          // if its off
          lightMode = 1;              // turn lights on!
              //mode
             
              mode1();
             
             
             
             
             
             
        } else {   
          lightMode = 0;
         
         
         
          mode2();
 
         
         
         
         
         
         
        }
      }
    }
    buttonState = val;                 // save the new state in our variable
  }
 
 

Serial.print("Read switch input: ");
Serial.println(digitalRead(switchPin2));    // Read the pin and display the value
  delay(100);

 
 
}


//function pew
void send(int data)
{
  digitalWrite(CLK, LOW);
  delay(2);
  for (int i=15; i>=0; i--)
  {
    delayMicroseconds(50);
    if((data>>i)&0x0001 >0)
      {
        digitalWrite(DAT, HIGH);
        //Serial.print(1);
      }
    else
       {
         digitalWrite(DAT, LOW);
        // Serial.print(0);
       }
    delayMicroseconds(50);
    digitalWrite(CLK, HIGH);
    delayMicroseconds(50);
   
    if(i>0)
    digitalWrite(DAT, LOW);
    else
    digitalWrite(DAT, HIGH);
    delayMicroseconds(50);
   
    if(i>0)
    digitalWrite(CLK, LOW);
    else
    digitalWrite(CLK, HIGH);
  }
 
  delay(20);
}

AWOL

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

kllsamui

#2
Oct 20, 2012, 05:00 am Last Edit: Oct 20, 2012, 05:04 am by kllsamui Reason: 1
your code for debounce is this delay ?

Quote
if(digitalRead(switchPin) == HIGH && button == 0){ /* if D2 is HIGH */
  delay(100);  /* Soft Debounce delay for 500mS */

   button = 1;



pls check on lib http://arduino.cc/playground/Code/Bounce

the variable "button" is a memory for the pushbutton/switch state on  Din4 ? not Din2 ?
so this code in setup, where the value of "button" "0" is questionable
Quote
digitalWrite(button, LOW); /* Set button initially as LOW */

i think it might break the communication?

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy