I'm trying to break this while loop with the serial input of "Clear" but my serial monitor shuts down when the loop is going. This loop basically makes my siren and light blink until the serial input of "clear" is given but it's not executing. Any suggestions?
my code with the loop
I tried to use the current method, then tried to use the method of basically declaring two ints of hubHazard; and clear; and coding it to if i type "hubHazard == 1" into the monitor it would activate the siren and "clear == 1" would end it, but since I found out the problem is the loop, that obviously didn't work. I also couldn't manage to get both working in the same code at the same time. I couldn't use the method of just trying to do all of this under a void loop function. The loop would only run it once when activated by a button, but I need it to go until it's told to stop, which is why I used a while loop.
Your approach should be like this:
bool alertOn = false;
void alert(){
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
//stuff
//stuff
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
//stuff
if(message_received == "Hazzard"){
alertOn = true;
}
else if(message_received == "Clear"){
alertOn = false;
}
if(alertOn){
alert();
}
}
Related
So I have come to a problem where I can not get out of a loop after I'm in it. My project is to have two functions, one for manual and another for automatic which will run forever, but I want that when I am in automatic, I will be able to get out by clicking the manual button on my phone. I don't know if that makes sense, but I really need help.
//Program to control LED (ON/OFF) from ESP32 using Serial Bluetooth
#include <Arduino.h>
#include <BluetoothSerial.h> //Header File for Serial Bluetooth, will be added by default into Arduino
BluetoothSerial ESP_BT; //Object for Bluetooth
int incoming;
int yellow_led = 13;
void setup() {
Serial.begin(9600); //Start Serial monitor in 9600
ESP_BT.begin("ESP32_LED_Control"); //Name of your Bluetooth Signal
Serial.println("Bluetooth Device is Ready to Pair");
pinMode (yellow_led, OUTPUT);//Specify that LED pin is output
}
void automatic()
{
while (incoming != 51)
{
digitalWrite(yellow_led, HIGH);
ESP_BT.println("LED turned ON");
delay(1000);
digitalWrite(yellow_led, LOW);
ESP_BT.println("LED turned OFF");
delay(1000);
}
}
void manual()
{
if (incoming == 49)
{
digitalWrite(yellow_led, HIGH);
ESP_BT.println("LED turned ON");
}
else if(incoming == 48)
{
digitalWrite(yellow_led, LOW);
ESP_BT.println("LED turned OFF");
}
}
void loop() {
if (ESP_BT.available()) //Check if we receive anything from Bluetooth
{
incoming = ESP_BT.read(); //Read what we recevive
Serial.print("Received:"); Serial.println(incoming);
if (incoming == 51) //#3
{
ESP_BT.println("In Manual Mode");
manual();
}
else if (incoming == 52) //#4
{
ESP_BT.println("In Automatic Mode");
automatic();
}
}
delay(20);
}
The while loop in the automatic function is unnecessary. You're already looping infinitely within loop() so that should be enough. Adding another loop, even though at first may seem like it will break, turns out to be an infinite loop. I'll get to that in a second.
So all you need to do is get rid of the while loop and it should work. However, this works only if you are guaranteed to have an incoming value for each time in the loop, which is what seems like from your code.
Another problem I see here is that if you enter say manual(), there is nothing really that changes the value of incoming, you're bound to have 51 as you're reusing the previous value and nothing no desired code path will be triggered.
Same also applies to automatic(), I hope you can see how that loop becomes infinite because of this.
So you also need something on the following lines.
void manual()
{
if (!ESP_BT.available())
return;
incoming = ESP_BT.read();
if (incoming == 49)
{
digitalWrite(yellow_led, HIGH);
ESP_BT.println("LED turned ON");
}
else if(incoming == 48)
{
digitalWrite(yellow_led, LOW);
ESP_BT.println("LED turned OFF");
}
}
Lastly, need help lol, please and thank you may not be descriptive enough for anybody to help with your problem, please go through, how to ask a good question for the next time you're posting.
I am trying to get the Qwiic Real Time Clock Module by Sparkfun working. I am using an esp8266. The clock works fine with the standard I2C pinouts, but when I change it with Wire.begin(2, 3);, it suddenly doesn't work anymore.
I've tried changing the pinouts to other examples (Wire.begin(0, 1); etc.) but nothing semes to work. I checked the wiring a ton of times and I think it is correct.
As code, I am just using a modified example written by Sparkfun.
This is my code:
void setup() {
Wire.begin(2 , 3);
Serial.begin(9600);
Serial.println("Read Time from RTC Example");
while(1) {
if (rtc.begin() == false) {
Serial.println("Something went wrong, check wiring");
} else
break;
delay(1000);
}
if (rtc.setToCompilerTime() == false) {
Serial.println("Something went wrong setting the time");
}
Serial.println("RTC online!");
}
void loop() { }
When i run the Program, it just stays in the while loop and never progresses further.
When I upload this script, my normal keyboard is not usable anymore and I think the right alt key is stuck in, even if I unplug the arduino the problem is still there.
The goal of this is to have a push to talk pedal for teamspeak. I was able to get around the problem using another script that passes itself as a controller but this causes other issues with games since they think I am using a controller all of a sudden.
From what I understand, this code should loop, while the pin is active, it should simulate the right alt key being pressed. When it is done it should release all key pressed by the script. It is hard to debug this since whenever I upload it I have to spend 10 minutes to be able to use my keyboard again.
Any help would be appreciated. Extra info, I am using the arduino leonardo.
#include <Keyboard.h>
void setup() {
// Initialize Button Pins
Keyboard.begin();
pinMode(9, INPUT_PULLUP);
}
void loop() {
Keyboard.releaseAll();
while (digitalRead(9) == HIGH) {
Keyboard.press(KEY_RIGHT_ALT);
delay(500);
}
delay(500);
Keyboard.releaseAll();
// wait for new window to open:
}
Note: without the actual hardware at my disposal for some tests, it is hard to know what is going wrong, so I'll try to review your code looking for possible point of failures.
According to the docs, press() functions as if a key were pressed and held down on the keyboard.
According to these other docs, INPUT_PULLUP effectively inverts the behavior of the INPUT mode, where HIGH means the sensor is off, and LOW means the sensor is on.
Therefore, a simple modification would be:
#include <Keyboard.h>
void setup() {
// Initialize Button Pins
Keyboard.begin();
pinMode(9, INPUT_PULLUP);
}
void loop() {
static bool pressed = false;
// does the pedal close the circuit with GND?
if (digitalRead(9) == HIGH) { // no
if (pressed) {
Keyboard.release(KEY_RIGHT_ALT);
pressed = false;
}
} else { // yes
if (!pressed) {
Keyboard.press(KEY_RIGHT_ALT);
pressed = true;
}
}
delay(500); // waste time
}
Does this code display the same issues?
i loaded a basic program to my Arduino Leonardo:
void setup() {
// make pin 2 an input and turn on the
// pullup resistor so it goes high unless
// connected to ground:
pinMode(2, INPUT_PULLUP);
Keyboard.begin();
}
void loop() {
//if the button is pressed
if(digitalRead(2)==LOW){
//Send the message
Keyboard.print("Hello!");
}
}
This example works, but it generate a infinite loop printing "Hello!". How can i control the loop?
The basic example is: http://arduino.cc/en/Reference/KeyboardPrint
Thanks !
If you want to say hello whenever the button transitions from off to on, you need to remember the previous state in a variable.
Then you only say hello when the current state is pressed and the previous state was not-pressed.
That would be something like:
int curr, prev = HIGH;
void loop () {
curr = digitalRead (2);
if ((prev == HIGH) && (curr == LOW)) {
Keyboard.print("Hello!");
}
prev = curr;
}
I'm wondering if there are any reasons not to loop inside the loop() function.
To illustrate my question, let's say I want to make a LED blink a thousand times.
Here are two ways to do it with the Arduino.
In the following one, I make sure not to "lock" the loop() function :
const int PIN_LED = 2;
const int BLINKING_LIMIT = 1000;
int blinkCount = 0;
void setup() {
// initialize serial:
pinMode(PIN_LED, OUTPUT);
}
// Here, I make sure not to "lock" the loop() function
void loop() {
blinkCount++;
if (blinkCount < BLINKING_LIMIT) {
digitalWrite(PIN_LED, HIGH);
delay(200);
digitalWrite(PIN_LED, LOW);
delay(200);
}
}
In the second one, there is a long loop (which could last even longer) in the loop() function. The Arduino is "locked" inside the for loop :
// Here, I make sure not to "lock" the loop() function
void loop() {
for (int i = 0; i < BLINKING_LIMIT; i++) {
digitalWrite(PIN_LED, HIGH);
delay(200);
digitalWrite(PIN_LED, LOW);
delay(200);
}
}
What is the best practice ? Should I care not to "lock" the loop() function, or can I just don't care ? Would an infinite loop inside the loop() function be acceptable ?
loop() and setup() are just 2 functions defined for Arduino. It will be compiled with the main code for your board.
The code of the Arduino board will be something like:
void main()
{
setup();
for(;;) {
loop();
}
}
And you just have the possibility to write the code for setup and loop.
It is like #Piglet said. It is your code and you can write it how you want.
You can not "lock" the loop, since it is not an interrupt and there is no OperatingSystem behind your loop.
Once the loop is terminated, it is called automatically again. So you can also write:
void loop()
{
for(;;) {
// your code
}
}
If you like it, so the loop will never terminate and you can write it like on a 8051 processor ;)
Just don't care. Nesting loops is super common practice in programming. Many problems cannot be solved efficiently without it.
It is your program. You may stay within a loop as long as you wish. Your Arduino won't timeout or anything.
Personally, I would use the second code. It's precise and short. Who wants to make the code long and boring. Loop will stop when you want to stop just add a condition, so don't care until and unless your code works perfectly.
If you just want to make a LED blink, it's not important but if you want to add something to your code, the behavior will be different:
void loop() {
blinkCount++;
if (blinkCount < BLINKING_LIMIT) {
digitalWrite(PIN_LED, HIGH);
delay(200);
digitalWrite(PIN_LED, LOW);
delay(200);
}
//ANYTHING ELSE
}
In this part, your LED will blink and the rest of your code will be executed in each 400ms about.
void loop() {
for (int i = 0; i < BLINKING_LIMIT; i++) {
digitalWrite(PIN_LED, HIGH);
delay(200);
digitalWrite(PIN_LED, LOW);
delay(200);
}
//ANYTHING ELSE
}
In this part, your LED will blink "BLINKING_LIMIT" times and only after, the rest of your code will be executed.
Just be careful if you want to add something.