Hi everyone, I am creating a simple application to smart control LED through Bluetooth. I have Arduino coding which works smoothly by sending char through the serial monitor by itself. However,when I switch over to the MIT app inventor, I am not able to exit the while loop from the Arduino when the signal was sent from the MIT app inventor after a while. Why is this happening? "k" will enter the while loop
and "z" is suppose to end the while loop. Attached are my current MIT program.
I have only screenshot the basic button for context on the MIT app inventor.
//Initialization and calibration code
char incoming_value = 0; // default value for bluetooth communication
int dimLed = 5; //First LED
int dimLed2 = 6; //Second LED
const int DUTY_25 = 32; //Preset Brightness level to 25% for LED 1
const int DUTY_50 = 64; //Preset Brightness level to 50% for LED 1
const int DUTY_75 = 128; //Preset Brightness level to 75% for LED 1
const int DUTY2_25 = 32; // Preset Brightness level to 25% for LED 2
const int DUTY2_50 = 64; // Preset Brightness level to 25% for LED 2
const int DUTY2_75 = 128; // Preset Brightness level to 25% for LED 2
int pirsensor = 2; //pir sensor to be at pin 2
int fan = 8; //dc motor to be at pin 8
int buzzer = 12; //buzzer to be at pin 12
int calibrationTime = 30; //calibration timing for PIR set at 30 sec
//--------------------------------------------------------------------
void setup()
{
Serial.begin(9600);
pinMode(dimLed, OUTPUT); //declaring dimLed as the output for LED 1
pinMode(dimLed2, OUTPUT); //declaring dimLed as the output for LED 2
pinMode(pirsensor, INPUT); //A0 connects to LDR as input
pinMode(A0, INPUT);
pinMode(fan, OUTPUT);
pinMode(buzzer,OUTPUT);
Serial.println("Waiting for the sensor to warm up.");
delay(calibrationTime * 1000); // Convert the time from seconds to milliseconds.
Serial.println("SENSOR ACTIVE");
digitalWrite(buzzer, HIGH); //Alarm to tell users that the calibration is over
delay(500);
digitalWrite(buzzer, LOW);
}
//--------------------------------------------------------------------
void loop()
{
pirsensor = digitalRead(2); // Read PIR status for dimmer routines
if (Serial.available()) //Any incoming value
{
Serial.println(analogRead(A0));
Serial.println(digitalRead(2));
delay(300);
incoming_value = Serial.read(); //whatever read on serial to store into incoming_value
}
// Manual mode , users turning on/off and adjusting brightness manually
if (incoming_value == '7') //when user send '7' , turn on 1st led
{
digitalWrite(dimLed, HIGH);
Serial.println(incoming_value); //show the value read by Serial.read
}
else if (incoming_value == '8') //when user send '8' , turn off 1st led
{
digitalWrite(dimLed, LOW);
Serial.println(incoming_value);
}
else if (incoming_value == 'c') //when user send 'c' , adjust brightness level to 25%
{
Serial.println(incoming_value);
analogWrite( dimLed, DUTY_25 );
}
else if (incoming_value == 'd') //when user send 'd' , adjust brightness level to 50%
{
Serial.println(incoming_value);
analogWrite( dimLed, DUTY_50 );
}
else if (incoming_value == 'e') //when user send 'd' , adjust brightness level to 75%
{
Serial.println(incoming_value);
analogWrite( dimLed, DUTY_75 );
}
//LED2 //LED 2 be same as 1st LED
if (incoming_value == 'f')
{
Serial.println(incoming_value); // For DEBUG only
digitalWrite(dimLed2, HIGH);
}
else if (incoming_value == 'g')
{
Serial.println(incoming_value); // For DEBUG only
digitalWrite(dimLed2, LOW);
}
else if (incoming_value == 'h')
{
Serial.println(incoming_value); // For DEBUG only
analogWrite( dimLed2, DUTY2_25 );
}
else if (incoming_value == 'i')
{
Serial.println(incoming_value); // For DEBUG only
analogWrite( dimLed2, DUTY2_50 );
}
else if (incoming_value == 'j')
{
Serial.println(incoming_value); // For DEBUG only
analogWrite( dimLed2, DUTY2_75 );
}
//fan
else if (incoming_value == 't')
{
Serial.println(incoming_value);
digitalWrite( fan, HIGH );
}
else if (incoming_value == 'y')
{
Serial.println(incoming_value);
digitalWrite( fan, LOW );
}
//Auto mode
else if (incoming_value == 'k') // Activating "Auto" mode when incoming_value is "k"
{
Serial.println(incoming_value); // For DEBUG only
while ( analogRead(A0) > 20 ) // Enters while loop ,upon receiving "k"
{
if (Serial.available())
{
incoming_value = Serial.read();
if (incoming_value == 'z')
{
Serial.println(incoming_value);
break; //will exit while loop
}
}
else if (analogRead(A0) > 400 && digitalRead(2) == HIGH) // When LDR reading is above 400 and PIR detected motion = HIGH
{
digitalWrite(dimLed, HIGH); //LDR reading > 400 indicating sky is dark
digitalWrite(dimLed2, HIGH); //Both LED will turned on automatically
digitalWrite(fan, HIGH); //Fan on when motion is detected
delay(1000);
digitalWrite(fan, LOW);
Serial.println("Sky is dark and motion is detected"); // Displaying current status in Auto mode
}
else if (analogRead(A0) > 400 && digitalRead(2) == LOW) // When LDR reading is above 400 and PIR detected no motion = LOW
{
digitalWrite(dimLed, LOW); //Both LED will remained off
digitalWrite(dimLed2, LOW);
Serial.println("Sky is Dark and no motion is detected"); // Displaying current status in Auto mode
}
else if (analogRead(A0) < 400 && digitalRead(2) == LOW) // When LDR reading is below 400 and PIR detected no motion = LOW
{
digitalWrite(dimLed, LOW);
digitalWrite(dimLed2, LOW); //Both LED will remained off
Serial.println("Sky is bright and no motion is detected"); // Displaying current status in Auto mode
}
else if (analogRead(A0) < 400 && digitalRead(2) == HIGH) // When LDR reading is below 400 and PIR detected motion = HIGH
{
digitalWrite(dimLed, LOW);
digitalWrite(dimLed2, LOW); //Both LED remained off
digitalWrite(fan, HIGH); //Fan on when motion is detected
delay(1000);
digitalWrite(fan, LOW);
Serial.println("Sky is bright and motion is detected"); //Display current status
}
}
}
incoming_value = ' '; // Clear incoming_value to avoid repeat action
}
Are you able to find out the reason?
Why did you deleted the arduino code, I asked because that was making your question close to complete, so that anyone else who is having knowledge about the Arduino and BT communication can provide suggestion/help. I checked you arduino code and as you said that you are able to enter Auto Mode but unable to break the loop, as I can remember that conditional statement was surrounded by A0 analog input (>20) which was for LDR, if I am correct. What I can suggest you to debug the A0 value as well.
Please keep that arduino code posted here, according to the topic there seems to be issue there at .ino file that why I asked to share.
I am only unable to break the loop when using MIT app inventor after the code is running for a while. It is working fine without using the MIT app.
I notice your Arduino code receives single letter inputs on Serial.read(), and uses Serial.println() to send output. In fact, it's a bit of a blabbermouth.
Your AI2 code has buttons that send single letters, but I don't see anything in your blocks to try to read incoming text from the Arduino.
So my question is, does that text pile up somewhere on the Arduino side, or does it just pass unheard, like yelling into the wind?
When you use a serial monitor instead of AI2, is it performing a necessary service by soaking up all that blabber from the Arduino?