Hi! I need help with this code

So, what my code is supposed to do is receive an instruction from an AppInventor app through bluetooth. Then if case 0 is chose, the arduino is supposed to wait for me to push a physical button, execute the commands after I press that button, and then brake the case 0. Any ideas on how to do that? Below you can find the code I'm working in. Be patient! I'm a newbie in programing.

int val;
void setup() {
Serial1.begin(9600);
pinMode (2, INPUT);
pinMode (3, INPUT);
pinMode (12, OUTPUT);
pinMode (13, OUTPUT);
}

void loop() {
bool button1 = digitalRead(3);
bool button2 = digitalRead(2);
int i = 0;
if (Serial1.available()) {
val = Serial1.parseInt();
switch (val) {
case 0:
do {
digitalWrite(13, 1);
delay(200);
digitalWrite(13, 0);
delay(200);
} while (button1 == LOW);
if (button1 == HIGH) {
digitalWrite(13, 1);
delay(7000);
digitalWrite(13, 0);
}
break;
case 1:
digitalWrite(12, 1);
delay(4000);
digitalWrite(12, 0);
break;
case 2:
digitalWrite(12, 1);
delay(3000);
digitalWrite(12, 0);
break;
case 3:
digitalWrite(12, 1);
delay(2000);
digitalWrite(12, 0);
break;

}
}
}

What does the code do now ?

How are the button inputs wired ?

UKHeliBob:
What does the code do now ?

How are the button inputs wired ?

My inputs are two buttons, one connected to pin 2 and the other to pin 3. With the do...while my led in pin 13 just blinks and when I push the button nothing happens. Both buttons receive the 5v and they go to GND with a resistance . In case 1 the app sends a 1 and then the led in pin 12 stays on for 4000 ms and turns off, then case 1 ends, and so on.
Basically what I want is that in case 0 the arduino waits for the button to be HIGH to do the instructions afterwards, which are:
digitalWrite (13,1);
delay (7000);
digitalWrite (13,0);
This will turn on a led for 7 seconds then it will turn off and case 0 is ended

Both buttons receive the 5v and they go to GND with a resistance .

Sorry, but I am still not clear how the inputs are wired. Is the input pin permanently connected to GND or 5V via a resistor and if so what is its value ? When the button is pressed is the pin taken to GND or 5V ?

What arduino are you using ? clearly it has hardware Serial1.

I recommend you put in some debug statements so you can see on the serial console (Serial that is, not Serial1) what you get from the bluetooth device and follow the progress of the sketch. Then it will be clearer to you where your logic is failing.

UKHeliBob:
Sorry, but I am still not clear how the inputs are wired. Is the input pin permanently connected to GND or 5V via a resistor and if so what is its value ? When the button is pressed is the pin taken to GND or 5V ?

Here are some images, but it goes from 5V to the positive part of the breadboard then from there to the horizontal part then a jumper to the button, then a jumper from the button to the breadboard then a jumper to pin 3 and the other cable is to power up a motor when the connection completes as the button is pushed down, that motor has its GND connected to the negative part of the breadboard and the button just ends with a bridge made by a resistance to the negative part of the breadboard
https://drive.google.com/folderview?id=10LlTEIIl0Xh2nonxtpyPydoRTOlw_t5F

6v6gt:
What arduino are you using ? clearly it has hardware Serial1.

I recommend you put in some debug statements so you can see on the serial console (Serial that is, not Serial1) what you get from the bluetooth device and follow the progress of the sketch. Then it will be clearer to you where your logic is failing.

Arduino Leonardo

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile: