Hi All
Why will my LEDs not switch ON or OFF?
I am trying to set up the logic to use an HC-05 as a controller. The aim is to send two command strings via the HC-05 to switch a pinout from LOW to HIGH. The RISING will trigger a time-stamp on a separate board.
In my test scenario, I am using:
- send string "Test1" to HC-05, this turns LED ON using digitalWrite(ledPin, HIGH);
- send string "Test2" to HC-05, this turns LED OFF using digitalWrite(ledPin, LOW);
Problem is: the LED does not go HIGH or LOW.
On the bread board, the LED circuit is: Pin-10=>220ohm=>long-leg of LED=>short-leg of LED to GND. [The two attached image show the breadboard circuits.]
I have checked the circuitry:
- my ammeter is checked against a flashlight and its batteries
- with the ammeter directly bridging Pin-10 and GND, it shows NO voltage when digitalWrite is HIGH
- the ammeter shows 220ohms in circuit from Pin-10 to the long-leg of LED
- the LED is tested to work.
The RX[HC-06] to TX[UNO] goes through a voltage bridge, 2K and 1K resistors, reducing the Voltage at Pin-6[TX] to 3.3V. [See image attached.]
The serial output shows that the text inputs run through the code where digitalWrite(ledPin, LOW) and digitalWrite(ledPin, HIGH) are executed. This is my serial output:
Test1
LED: ON
readString: Test1: 5
Test1
LED: ON
readString: Test1: 5
Here is my full sketch. (Please note: The code has a LOT of steps in it to help me debug - and understand what's going on.)
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(5, 6); // RX | TX // Same as other tests
#define ledPin 10
char c = "";
String readString = "";
void setup() {
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
delay(1000);
digitalWrite(ledPin, HIGH); // Assumed the LED should go ON
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
// Serial.begin(38400); // Default communication rate of the HC-05
Serial.begin(9600); // EGB! Prduces readable output on Serial.println
// BTSerial.begin(38400); // HC-05 default speed in AT command more
BTSerial.begin(9600); // HC-05 default speed in AT command more
}
void loop() {
// -- Serial ---------------------------------------------------
if(Serial.available() > 0){ // Checks whether data is comming from the serial port
while(Serial.available() > 0) { // While there is more, keep reading.
delay(3); // wait 100ms for next reading
c = Serial.read(); //Conduct a serial read
readString += c; //build the string.
Serial.print(c);
}
Serial.println("");
}
delay(100); // wait 100ms for next reading
// -- Bluetooth ---------------------------------------------------
// Keep reading from HC-05 and send to Arduino Serial Monitor
if (BTSerial.available() > 0) {
while(BTSerial.available() > 0) { // While there is more, keep reading.
delay(3);
// Serial.write(BTSerial.read());
c = BTSerial.read(); //Conduct a serial read
readString += c; //build the string.
Serial.write(c);
}
Serial.write("\n");
}
delay(100); // wait 100ms for next reading
// -- LEDs ON/OFF ---------------------------------------------------
if (readString == "Test2") {
digitalWrite(ledPin, LOW); // Turns LED OFF
Serial.println("LED: OFF");
}
if (readString == "Test1") {
digitalWrite(ledPin, HIGH); // Turn LED ON
Serial.println("LED: ON");;
}
delay(100); // wait 100ms for next reading
// -- Validate String-------------------------------------------------
if(readString.length() > 0){ // Checks whether data is comming from the serial port
int j = readString.length(); //
Serial.print("readString: " + readString + ": "); //
Serial.print(j); //
Serial.println(""); //
}
readString=""; //Reset the variable
delay(100); // wait 100ms for next reading
}
So, why does my LED not switch ON or OFF?
I Look forward to your reply. Thanks in advance
EGB