Go Down

Topic: Using a button to continue your loop, or to break in switch case (Read 531 times) previous topic - next topic

hi everyone............
i'm new to arduino i was lately working on a project .
i faced a problem in my code, the problem is i can't use the button i attached to break the switch statment.
ie.
i have the following switch statment
if (Serial.available()>0 && sensorread==LOW)   
  {
    int incomingserial = Serial.read();
    switch (incomingserial)
  {         
    case 'a':
    Serial.print("This Is Case A");
    digitalWrite(motor1, HIGH);
    delay(500);
    digitalWrite(motor1, LOW);
    digitalWrite(transmitter, HIGH);
    digitalWrite(buzzer1, HIGH);
    break;

as you can see here is part of the switch statment i posted only this because the othr cases are the same but with different motor to rotate, the problem here is that i want to end this case when button1 is pressed so the transmitter and buzzer1 are on until button1 is pressed. when its pressed it goes to main loop.


other thing is that i have also a switchcase statment what i want to do is somehow the same as above ie.
if (Serial.available() > 0 && sensorread== HIGH)
  {
    int incomingserial = Serial.read();
      switch (incomingserial) {
    case 'a':   
     digitalWrite(transmitter,HIGH);
     digitalWrite(buzzer,HIGH);
[here i want to insert code that if button2 is pressed turn of motor(x) then delay 500 break;
Please reply as soon as possible. with all my respect to all who will help me  :%



   
       
 

robtillaart

Code: [Select]
if (Serial.available()>0 && sensorread==LOW)   
  {
    int incomingserial = Serial.read();
    switch (incomingserial)
  {         
    case 'a':
    Serial.print("This Is Case A");
    digitalWrite(motor1, HIGH);
    delay(500);
    digitalWrite(motor1, LOW);
    digitalWrite(transmitter, HIGH);
    digitalWrite(buzzer1, HIGH);
    while(digitalRead(BUTTONPIN, LOW));  // wait until buttonpin goes high  // may revert logic
    break;
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

liudr

The complete code will be helpful. Also how sure are you that you connected the button correctly? Have you tested the button?

bastukee

#3
Mar 28, 2011, 09:41 pm Last Edit: Mar 28, 2011, 10:07 pm by bastukee Reason: 1
Quote

while(Serial.available()>0)   
{
  int incomingserial = Serial.read();
  if(incomingserial == a)
  {         
    while(sensorread==LOW)
    {
      Serial.print("This Is Case A");
      digitalWrite(motor1, HIGH);
      delay(500);
      digitalWrite(motor1, LOW);
      digitalWrite(transmitter, HIGH);
      digitalWrite(buzzer1, HIGH);
      sensorread=digitalRead(pinx);
    }
  }
}



The way I wrote it, the while statement will run indefinitely until sensorread becomes HIGH. Could you post your entire code?
No trees were harmed by the transmission of this message. However a few million electrons were temporarily inconvenienced.

Go Up