Pages: [1]   Go Down
Author Topic: Using a button to continue your loop, or to break in switch case  (Read 467 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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  smiley-draw

 

   
       
   
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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;
Logged

Rob Tillaart

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

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 72
Posts: 7171
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


0
Offline Offline
Full Member
***
Karma: 0
Posts: 112
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
« Last Edit: March 28, 2011, 03:07:09 pm by bastukee » Logged

No trees were harmed by the transmission of this message. However a few million electrons were temporarily inconvenienced.

Pages: [1]   Go Up
Jump to: