Hello, so I'm having some difficulty with setup.
Basically, I have an Arduino Mega with an IR sensor input, and 8 motor outputs. I am connecting an Andriod phone to the Mega, serially (via usb).
The Andriod app is sending 49's - 56's when a button (1-8) is pressed on the app (and sending -1 when it's not pressed/idle).
I wish to press a button on the app, let's say number 1, and have the motor#1 turn on when I put my hand under the IR sensor (the reading is "0"). Putting my hand under the IR sensor might be 20 seconds after I select the #1 button on the app.
I'm sort of new at coding. How can I tell the Arduino to know when a serial number (49 in this case) has been sent and wait to turn on a motor after the IR sensor notifies my hand. I've been trying to use a WHILE loop, but it doesn't seem to work. It keeps turning on motor on and off (loop keeps running) and it doesn't print anything on the serial monitor, regardless if the sensor is reading 0 or 1.
Could someone guide me with what functions or loops I could use? Thanks.
int Andr = 0;
int ok = 0;
int sensor=1;
void setup() {
Serial.begin(9600);
pinMode(2, INPUT); // input reading of IR sensor
pinMode(3, OUTPUT); // output to motor1 (Face Wash)
pinMode(4, OUTPUT); // output to motor2 (Toner)
pinMode(5, OUTPUT); // output to motor3 (Serum)
pinMode(6, OUTPUT); // output to motor4 (Moisturizer)
pinMode(7, OUTPUT); // output to motor5
pinMode(8, OUTPUT); // output to motor6
pinMode(9, OUTPUT); // output to motor7
pinMode(10, OUTPUT); // output to motor8
pinMode(12, INPUT); // input from Andriod App
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
int Andr = Serial.read();
delay(10);
int sensor = digitalRead(2); // read sensor input from digital input (pin 2)
Serial.print(sensor);
delay(10); // wait 10 ms
if (Andr == 49) {
while (sensor == 0) {
int sensor= digitalRead(2);
Serial.print(sensor);
digitalWrite(3,HIGH); //turns on motor1 if hand is under sensor and when Andriod sends a signal "1" meaning the Face Wash product choice
delay(250);
digitalWrite(3,LOW);
delay(5000);
}
}
if (Andr == 50) {
while (sensor == 0) {
int sensor= digitalRead(2);
Serial.print(sensor);
digitalWrite(4,HIGH); //turns on motor2
delay(250);
digitalWrite(4,LOW);
delay(5000);
}
}
if (Andr == 51) {
while (sensor == 0) {
int sensor= digitalRead(2);
Serial.print(sensor);
digitalWrite(5,HIGH); //turns on motor3
delay(250);
digitalWrite(5,LOW);
delay(5000);
}
}
if (Andr == 52) {
while (sensor == 0) {
int sensor= digitalRead(2);
Serial.print(sensor);
digitalWrite(6,HIGH); //turns on motor4
delay(250);
digitalWrite(6,LOW);
delay(5000);
}
}
if (Andr == 53) {
while (sensor == 0) {
int sensor= digitalRead(2);
Serial.print(sensor);
digitalWrite(7,HIGH); //turns on motor5
delay(250);
digitalWrite(7,LOW);
delay(5000);
}
}
if (Andr == 54) {
while (sensor == 0) {
int sensor= digitalRead(2);
Serial.print(sensor);
digitalWrite(8,HIGH); //turns on motor6
delay(250);
digitalWrite(8,LOW);
delay(5000);
}
}
if (Andr == 55) {
while (sensor == 0) {
int sensor= digitalRead(2);
Serial.print(sensor);
digitalWrite(9,HIGH); //turns on motor7
delay(250);
digitalWrite(9,LOW);
delay(5000);
}
}
if (Andr == 56) {
while (sensor == 0) {
int sensor= digitalRead(2);
Serial.print(sensor);
digitalWrite(10,HIGH); //turns on motor8
delay(250);
digitalWrite(10,LOW);
delay(5000);
}
}
}
