I wanted to make this project related to aviation where I want to make LED flash when the dedicated ON button is pressed and turn it off when the dedicated OFF button is pressed.
Basically in the code provided I am controlling the led with bluetooth module and ardurino.
The issue with the code is that it works (LED is flashing) when the ON button is kept pressed and upon releasing the LED stops flashing.
This makes things happen only when characters arrive from serial. Change that.
Let the ON button set a boolean systemOn = true. The OFF button sets systemOn = false.
Then check for systemOn in the code in loop and do what You want.
As @Railroader has said... at the moment you have everything inside the while loop, so the code will only be executed when something arrives from the monitor. You need to just set a variable, an then check that in the main loop.
It is not a good idea to use delay() for the LED flashing. This statement blocks your code, so everything has to wait while this completes, including for example, whether you got something new from Serial. You should use millis() for anything needing timing. Have a look at the following example...
Hello dhruv_24
Welcome to the best ever Arduino Forum.
Here comes my standard recipe to get started:
Take some time, study the IPO model and take a piece of paper plus a pencil and design a program structure. Identify modules could to be coded and tested separetly. Merge after sucessfull testing all modules together to the needed project.
It is highly recommented to take a view into some powerful C++ instructions like STRUCT, ENUM, ARRAY, CASE/SWITCH,UDT, RANGE-BASED-FOR-LOOP, etc, too.
Input: read buttons, including deboucing and transition detection
Processing: Analyze button and timer behaiviour and prepare action
Output: Switch lights
As kick of for your project run some examples provided by the IDE.
Have a nice day and enjoy programming in C++ and learning.
MIND THE GAP
//1. previous time when we blinked the LED
//2. continously check if enough time has passed since previous tume
//3. if enough time has passed, blink the LED and update previous tume
//--> repeat this structure for each action
#define LED_1_PIN 12
#define LED_2_PIN 11
//LED 1
unsigned long previousTimeLED1Blink = 0;
unsigned long LED1BlinkDelay = 600;
byte LED1State = LOW;
//LED 2
unsigned long previousTimeLED2Blink = 0;
unsigned long LED2BlinkDelay = 800;
byte LED2State = LOW;
void setup() {
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
}
void loop() {
unsigned long timeNow = millis();
//Toggle LED 1
if (timeNow - previousTimeLED1Blink > LED1BlinkDelay){
previousTimeLED1Blink += LED1BlinkDelay;
if (LED1State == HIGH){
LED1State = LOW;
}
else{
LED1State = HIGH;
}
digitalWrite(LED_1_PIN, LED1State);
}
//Toggle LED 2
if (timeNow - previousTimeLED2Blink > LED2BlinkDelay){
previousTimeLED2Blink += LED2BlinkDelay;
if (LED2State == HIGH){
LED2State = LOW;
}
else{
LED2State = HIGH;
}
digitalWrite(LED_2_PIN, LED2State);
}
}
This is for 2 LEDs to multitask without any button or bluetooth module.
Now I want to try use bluetooth module directly but it dosen't work. The LED keeps on blinking before anything is pressed. Can't share the code as I rage quit and now its all gone Sorry!
It would be really great help if you can send me example that i can mess with and create my own!
Or
Can you suggest me a way to not use millis and make a ON/OFF button for the whole system running on delay?
Basically my project is to make a set of LEDs to work/blink when that button is toggled ON till turned OFF. It either could be a button that controls the whole system (note: it is a system of 10 LEDs in which a group of 5 are blinking and rest are meant to just stay ON) or a group of only 5 blinking LEDs.
If you can provide me code for 1 single LEDs i can try doing it for rest, but i want it to be using bluetooth only.
Great job getting the LEDs blinking... that is a big step forward.
Adding a button to this is now easier because of the way you have written the blinking code.
So.. you need to check that a button has been pressed... to do this you will need to use the digitalRead() function.
An example of this...
#define BUTTON_PIN 2 // At top of program
. . .
pinMode(BUTTON_PIN, INPUT_PULLUP); // In setup
. . .
if (digitalRead(BUTTON_PIN) == LOW) // In loop... Is the button being pressed
{
// Do something
}
Note that your button should be connected to the pin, with the other side connected to GND. When the button is pressed the value of digitalRead will be LOW.
Try this to start with and just print something to the monitor when the button is pressed.
The HC-05 device communicates in a protocol known as serial. Serial has a Transmit (Tx) and Receive (Rx) line. Your Uno has a Serial port (a pair of pins) ... pins 0 & 1... this is known as the hardware serial port. Unfortunately this port is used for other things on the Uno, like uploading code, and sending debug statements to the monitor so it's best not to use in this case.
Instead we can use something called Software Serial... basically a software version of Serial that can run on any 2 pins. Let's use pins 7 (Rx) and 8 (Tx).
To connect up to the HC-05
Uno 5v to HC05 5v.
Uno GND to HC05 GND.
Uno Rx to HC05 Tx. (note Rx to Tx)
Uno Tx to HC05 Rx. (note Tx to Rx).... don't connect this for now. The HC05 has a 3.3v logic level so we should use a voltage divider here. Can do later as for now you only need to receive.
On to the coding... firstly you need to include the Software Serial library.
#include <SoftwareSerial.h>
Then at the top of the program.
SoftwareSerial bluetooth(7, 8); // Give our connection a name, and state which pins to use (Rx,Tx)
Then in setup.
bluetooth.begin(9600); // Start the connection.
OK.. now we are ready to go.
In the main loop...
if (bluetooth.available() > 0) // Has something been received?
{
char c = bluetooth.read(); // Read a character and display it to the monitor.
Serial.print("Received : ");
Serial.println(c);
}
...and that's about it. Have a go and see how you get on.
Actually I was making a lot of mistakes initially regarding connections and I also wanted to know how to dedicate RX and TX if there aren't any, for some boards like ATTINY 88. So thank you for that!
From your example I made a program that would make led turn ON and OFF using bluetooth module and mobile app
int led = 13;
int data;
void setup()
{
Serial.begin(9600);
pinMode(13, OUTPUT);
}
void loop()
{
while(Serial.available() >0)
{
data = Serial.read();
Serial.println(data);
if(data =='A')
{
digitalWrite(13, HIGH);
}
if(data =='B')
{
digitalWrite(13, LOW);
}
}
}
It was easy and learned a lot on how it works.
I am now looking forward to proceed with the final step which is to blink that LED, i.e. Start blinking when button is pressed and Stop blinking when button is pressed again. Hope you'd help me here as well with an example.
So now you only want to execute that code when something is true... let's use a boolean to keep track of that something.
boolean isBlinking = false;
So you should put the code above inside an if statement, so that it only runs when isBlinking is true.
if (isBlinking == true)
{
// Run my blinking code
}
else
{
// Turn off the LED
}
Now we just need some code to set the value of isBlinking to either true or false.
'A' via bluetooth will make it true
'B' via bluetooth will make it false
If a button is pressed it will either make it true or false depending on its current value... so when a button is pressed we will swap its value... we can do this with the NOT (!) operator. isBlinking = !isBlinking;
Get a command from bluetooth and set isBlinking.
Based on isBlinking, either do the blinking stuff or don't.
Try (untested)...
#define LED_1_PIN 12
int data;
boolean isBlinking = false;
//LED1
unsigned long previousTimeLED1Blink = 0;
unsigned long LED1BlinkDelay = 600;
byte LED1State = LOW;
void setup()
{
Serial.begin(9600);
pinMode(LED_1_PIN, OUTPUT);
}
void loop()
{
unsigned long timeNow = millis(); // Capture current time.
while (Serial.available() > 0) // Anything received via bluetooth.
{
data = Serial.read(); // Read a character
Serial.println(data); // Display data received
if (data == 'A') // Turn on command received
isBlinking = true; // Set blinking on.
if (data == 'B') // Turn off command received.
isBlinking = false; // Set blinking off.
}
if (isBlinking) // Do we need to blink the LED?
{
if (timeNow - previousTimeLED1Blink > LED1BlinkDelay) // Has enough time passed since last toggle?
{
previousTimeLED1Blink += LED1BlinkDelay; // Reset the last time LED blinked.
if (LED1State == HIGH) // Toggle the LED state.
LED1State = LOW; // If HIGH, set LOW.
else
LED1State = HIGH; // If LOW, set HIGH.
digitalWrite(LED_1_PIN, LED1State); // Update LED based on state.
}
}
else // Don't blink LED.
{
digitalWrite(LED_1_PIN, LOW); // Turn LED off.
previousTimeLED1Blink = timeNow; // Keep resetting the last time we blinked.
}
}
One last question before we wrap up, can I adjust timing/pattern like we used to do in delay()
say when:
LED, HIGH -> delay(200)
LED, LOW -> delay(1000)
can this patten happen in millis() ??
I tried to mess up but the start time and end time seems to remain same.....
I wrote this can you help me to make it work with bluetooth? I did try but same nothing seems to work!
const int LEDpin = 3;
const long onDuration = 100;// OFF time for LED
const long offDuration = 500;// ON time for LED
int LEDState =HIGH;// initial state of LED
long rememberTime=0;// this is used by the code
void setup() {
pinMode(LEDpin,OUTPUT);// define LEDpin as output
digitalWrite(LEDpin,LEDState);// set initial state
}
void loop() {
if( LEDState ==HIGH )
{
if( (millis()- rememberTime) >= onDuration){
LEDState = LOW;// change the state of LED
rememberTime=millis();// remember Current millis() time
}
}
else
{
if( (millis()- rememberTime) >= offDuration){
LEDState =HIGH;// change the state of LED
rememberTime=millis();// remember Current millis() time
}
}
digitalWrite(LEDpin,LEDState);// turn the LED ON or OFF
}
Thank you so much for helping me out, from start to finish. This project is officially over as of coding part, the only thing now remains is to finish soldering a lot of LEDs and make connections.