Void loop not looping properly - arduino-uno

so I'm creating a traffic light system on an arduino uno. my current issue is that my main loop isn't working. it is supposed to go through regular mode which makes each light turn on for one second then the next and so on. currently all that happens is that it does one rotation of regular mode then stays stuck on red.ive kept the rest of the code on here just incase its somthing elsewere thats causing the issue.
//light variables
int red = 10;
int yellow = 9;
int green = 8;
int inches = 0;
int cm = 0;
float distance;
String inString = "";
int button = 2;
int ECHOPIN = 6;
int TRIGPIN = 7;
int BUZZER = 12;
int sensorPin = A0;
unsigned int sensorValue = 0;
char inVal;
int darkness = 150;
void setup()
{
pinMode(red, OUTPUT);//red led
pinMode(yellow, OUTPUT);//yellow led
pinMode(green, OUTPUT);//green led
pinMode(ECHOPIN, INPUT);//rangefinder input
pinMode(TRIGPIN, OUTPUT);//rangefiner output
pinMode(BUZZER,OUTPUT);//buzzer
Serial.begin(9600);
Serial.begin(115200);
}
void loop(){
distance = distancefind();
sensorValue = LDRfind();
if(sensorValue < darkness)
{
nightMode();
}
else if(distance < 10)
{
pedestrianMode();
}
else
{
regularMode();
}
}
void regularMode(){
// Green + yellow off. Red on for 2 seconds
digitalWrite(green, LOW);
digitalWrite(yellow, LOW);
digitalWrite(red, HIGH);
delay(2000);
// Green + red off. Yellow on for 1 second
digitalWrite(red, LOW);
digitalWrite(yellow, HIGH);
delay(1000);
// Red + yellow off. Green on for 2 seconds
digitalWrite(yellow, LOW);
digitalWrite(green, HIGH);
delay(2000);
// Red + green off. Yellow on for 1 second
digitalWrite(green, LOW);
digitalWrite(yellow, HIGH);
delay(1000);
}
void pedestrianMode(){
// buzzer buzzes. yellow bulb flashes for 10 seconds.
for(int i = 0; i <= 9; i++)
{
digitalWrite(yellow, HIGH);
digitalWrite(BUZZER, HIGH);
delay(200);
digitalWrite(yellow, LOW);
digitalWrite(BUZZER, LOW);
delay(200);
}
}
void nightMode(){
digitalWrite(yellow, LOW);
digitalWrite(green, LOW);
digitalWrite(red, HIGH);
if(distance <10){
digitalWrite(yellow, LOW);
digitalWrite(green, HIGH);
digitalWrite(red, LOW);
}
else{
nightMode();
}
}
float distancefind(){
digitalWrite(TRIGPIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGPIN,HIGH);
delayMicroseconds(10);
digitalWrite(TRIGPIN,LOW);
distance = pulseIn(ECHOPIN, HIGH); // return 0 if no high pulse in 1sec
distance = distance/58;
Serial.print(distance);
return distance;
}
int LDRfind(){
sensorValue = analogRead(sensorPin);
Serial.println("Sensor value: ");
Serial.println(sensorValue);
return sensorValue;
}
void buttonPress(){
if (digitalRead(button) == HIGH){
delay(15); // software debounce
if (digitalRead(button) == HIGH) {
// if the switch is HIGH, ie. pushed down - change the lights!
digitalWrite(green, HIGH);
digitalWrite(yellow, HIGH);
digitalWrite(red, HIGH);
delay(15000); // wait for 15 seconds
}
}
}

Okay so before posting this i had been playing around for a good hour. the minuite after posting this i had an idea that the constant red was from my night mode. ive added some distance checking and it works fine. sorry to have wasted whoever is looking time.

Related

How do I turn off my void loop function after a certain IF condition is met? (arduino)

I am trying to make a simple burglar alarm using magnet value as my door. After pressing button1 it goes into countdown and then the user should be able to press button1 again to insert a 3 digit code with the help of pmeter. Except that after button1 is clicked it calls toggleOnOff() again. How do I block my void toggleOnOff() function after the conditions of the first IF statement are met? Thank you in advance.
int startMagnetValue;
int Code[3] = {1, 2, 3};
int userCode[3] = {0, 0, 0};
int secondsLimit = 20;
int countDown;
int pMeter = 0;
int pMin = 14;
int pMax = 948;
unsigned long time_now = 0;
unsigned long time_then = 0;
unsigned long seconds = 0;
unsigned long secondsbefore = 1;
unsigned long interval = 1000;
void setup() {
initializeBreadboard();
startMagnetValue = analogRead(MAGNETSENSOR);
}
//turns off leds
void turnOffLEDS() {
digitalWrite(LED_RED, LOW);
digitalWrite(LED_BLUE, LOW);
digitalWrite(LED_YELLOW, LOW);
digitalWrite(LED_GREEN, LOW);
}
// returns true when the difference is bigger than 50, returns false when it's less than 50. True meaning door is open and False meaning it is closed
bool isOpen(int magnetValue) {
int magnetValueDifference = abs(startMagnetValue - magnetValue);
if (magnetValueDifference <= 50) {
return true;
} else {
return false;
}
}
//Toggles the alarm On or Off
void toggleOnOff() {
isAlarmOn = !isAlarmOn;
delay(200);
}
//registers if the alarm is on or off, also prints current magnet value to the oled
void loop() {
pMeter = analogRead(POTENTIOMETER);
int currentMagnetValue = analogRead(MAGNETSENSOR);
pMeter = map(pMeter, pMin, pMax, 0, 9); //translates pmeter to simple digits
OLED.printBottom(pMeter);
Serial.println(userCode[1]);
//On Off
if (digitalRead(BUTTON1)) {
toggleOnOff();
}
// Door Open, Alarm On
if(isOpen(currentMagnetValue) && isAlarmOn) {
turnOffLEDS();
digitalWrite(LED_RED, HIGH);
OLED.printTop(secondsLimit);
time_now = millis();
while((time_now - time_then) >= 1000)
{ secondsLimit--;
time_then = time_now;
Serial.println(secondsLimit);
}
if (userCode[1] = 0) {
if (digitalRead(BUTTON1)) {
userCode[1] = pMeter;
}
}
if (userCode[2] = 0) {
if (digitalRead(BUTTON1)) {
userCode[2] = pMeter;
}
}
if (userCode[3] = 0) {
if (digitalRead(BUTTON1)) {
userCode[3] = pMeter;
}
}
if (Code == userCode) {
isAlarmOn = false;
}
if (secondsLimit <= 0) {
digitalWrite(BUZZER, HIGH);
turnOffLEDS();
while (secondsLimit <= 0) {
OLED.print("INTRUDER");
turnOffLEDS();
digitalWrite(LED_RED, HIGH);
delay(100);
turnOffLEDS();
digitalWrite(LED_BLUE, HIGH);
delay(100);
turnOffLEDS();
digitalWrite(LED_YELLOW, HIGH);
delay(100);
turnOffLEDS();
digitalWrite(LED_GREEN, HIGH);
}
}
} else if (isOpen(currentMagnetValue) && !isAlarmOn) {
turnOffLEDS();
digitalWrite(LED_BLUE, HIGH);
//Door closed, Alarm On
} else if (!isOpen(currentMagnetValue) && isAlarmOn) {
turnOffLEDS();
digitalWrite(LED_GREEN, HIGH);
//Door closed, Alarm Off
} else if (!isOpen(currentMagnetValue) && !isAlarmOn) {
turnOffLEDS();
digitalWrite(LED_YELLOW, HIGH);
}
}```

How to read from two serial monitors at a time?

Here's my code:
const int AnalogInPin1 = A1;
const int AnalogInPin2 = A2;
int SerialPrint = 0;
int SerialMonitor = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
SerialPrint = analogRead(AnalogInPin1);
Serial.print("Sensor = ");
Serial.println(SerialPrint);
if (SerialPrint > 400) {
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
delay(500);
}
else{
digitalWrite(4, HIGH);
digitalWrite(3, LOW);
delay(500);
}
}
void setup1() {
Serial.begin();
}
void loop1() {
SerialMonitor = analogRead(AnalogInPin2);
Serial.print("Sensor = ");
Serial.println(SerialMonitor);
if (SerialMonitor > 400) {
digitalWrite(6, LOW);
digitalWrite(5, HIGH);
delay(500);
}
else{
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
delay(500);
}
}
I am trying to make a car guidance system like the ones found in underground parking lots. and I am trying to have two serial monitors read from 2 sensors.
f�������x怘�f�������x怘
The characters above are looping in the serial monitor whilst using 19200 baud but not in 9600 baud why is that?
If your question is about how to display two analogRead values in one SerialMonitor sketch, this should compile and work (if your SerialMonitor is set to 9600, of course):
const int AnalogInPin1 = A1;
const int AnalogInPin2 = A2;
int value1 = 0;
int value2 = 0;
void setup() {
Serial.begin(9600);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
}
void loop() {
value1 = analogRead(AnalogInPin1);
Serial.print("Sensor1 = ");
Serial.print(value1);
if (value1 > 400) {
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
}
else{
digitalWrite(4, HIGH);
digitalWrite(3, LOW);
}
value2 = analogRead(AnalogInPin2);
Serial.print("\t Sensor2 = ");
Serial.println(value2);
if (value2 > 400) {
digitalWrite(6, LOW);
digitalWrite(5, HIGH);
}
else{
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
}
delay(500);
}
I left most of your code as is, just made it compile and work. If this is not what you want, please edit your question.

Arduino Project (TRAFFIC LIGHTS) *need ideas*

This is the code I have so far and I need help making the button which is on pin2 of the Arduino have an interrupt where if the lights can cycle on there own and if the button is pressed then it must kick on the yellow LED for 3sec and give the red LED for 10sec for the pedestrian to walk across. The cycle I have set but I need the pushbutton to work and im having difficulties making this work, any suggestions?
CODE
int red = 10;
int yellow = 9;
int green = 8;
int button = 2; // switch is on pin 2
void setup(){
pinMode(red, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(green, OUTPUT);
pinMode(button, INPUT);
digitalWrite(green, HIGH);
}
void loop() {
if (digitalRead(button) == HIGH){
delay(15); // software debounce
if (digitalRead(button) == HIGH) {
// if the switch is LOW change the lights
changeLights();
delay(1000); // wait for 1 second
}
}
}
void changeLights(){
// RED ON for 5 sec
digitalWrite(red, HIGH);
digitalWrite(yellow, LOW);
digitalWrite(green, LOW);
delay(5000);
// GREEN ON for 5 sec
digitalWrite(red, LOW);
digitalWrite(yellow, LOW);
digitalWrite(green, HIGH);
delay(5000);
// YELOOW ON for 3 sec
digitalWrite(red, LOW);
digitalWrite(yellow, HIGH);
digitalWrite(green, LOW);
delay(3000);
}
This is Arduinos example code for external interrupts:
const byte ledPin = 13;
const byte interruptPin = 2;
volatile byte state = LOW;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);
}
void loop() {
digitalWrite(ledPin, state);
}
void blink() {
state = !state;
}
If the state of pinput pin interruptPin changes the microcontroller will service that interrupt calling blink. then it jumps back to into the application where it was interrupted.
So attach an interrupt service routine to your button pin and store in a variable the information that you should start the yellow red phase at the next chance.
as the yellow red phase usually cannot be interrupted you can use blocking code like delay to time it if your program is not controlling anything elss in the meantime.

Program for Blinking led quickly for some time and then slowly on Arduino UNO

I am new to Arduino,i want my led to blink 5 time quickly for time period 1s and then slowly for time period 4s, i tried like this,
void setup()
{
pinMode(13, OUTPUT);
}
void loop()
{
int n=1;
while (n<=5)
{
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
}
while (n<=10)
{
digitalWrite (13, HIGH);
delay (2000) ;
digitalWrite(13, LOW) ;
delay(2000) ;
}
But it's not working, please help me to fix it. Thanks
You are not updating 'n' inside your while loop
void setup()
{
pinMode(13, OUTPUT);
}
void loop()
{
int n=1;
while(n++ <= 5)
{
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
}
while(n++ <= 10)
{
digitalWrite (13, HIGH);
delay (2000) ;
digitalWrite(13, LOW) ;
delay(2000) ;
}
}
This should now work as intended.
Your code has infinite loops. Since you are just counting from 1 to 5 or 10, why not make them for loops?
void setup()
{
pinMode(13, OUTPUT);
}
void loop()
{
int n;
for (n = 1; n <= 5; n++)
{
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
}
for (n = 1; n <= 10; n++)
{
digitalWrite(13, HIGH);
delay(2000);
digitalWrite(13, LOW);
delay(2000);
}
}

Arduino motor speed and direction

I would like to make a program to control my Arduino Bluetooth car.
I'm having lots of trouble making it in a desired direction and at a desired speed. So far I tried this, but for some reason it is not working:
int motor1clock = 7, motor1clockc = 8, pwm1 = 3, pwm2 = 9, motor2clock = 10, motor2clocko = 16, speed;
String inputString = "", junk;
void setup()
{
pinMode(motor1clock, OUTPUT);
pinMode(motor1clockc, OUTPUT);
pinMode(pwm1, OUTPUT);
pinMode(motor2clocko, OUTPUT);
pinMode(motor2clock, OUTPUT);
pinMode(pwm2, OUTPUT);
Serial1.begin(9600);
}
void Forwards(int spee)
{
analogWrite(pwm2, spee);
analogWrite(pwm1, spee);
digitalWrite(motor1clock, LOW);
digitalWrite(motor1clockc, HIGH);
digitalWrite(motor2clocko, HIGH);
digitalWrite(motor2clock, LOW);
}
void Backwards(int spee)
{
analogWrite(pwm2, spee);
analogWrite(pwm1, spee);
digitalWrite(motor1clock, HIGH);
digitalWrite(motor1clockc, LOW);
digitalWrite(motor2clocko, LOW);
digitalWrite(motor2clock, HIGH);
}
void Steer_Left(int spee)
{
analogWrite(pwm2, spee - 20);
analogWrite(pwm1, spee);
digitalWrite(motor1clock, LOW);
digitalWrite(motor1clockc, HIGH);
digitalWrite(motor2clocko, LOW);
digitalWrite(motor2clock, HIGH);
}
void Steer_Right(int spee)
{
analogWrite(pwm2, spee);
analogWrite(pwm1, spee - 20);
digitalWrite(motor1clock, HIGH);
digitalWrite(motor1clockc, LOW);
digitalWrite(motor2clocko, HIGH);
digitalWrite(motor2clock, LOW);
}
void loop()
{
if (Serial1.available() > 0) {
String str = Serial1.readString();
char inChar[3];
str.toCharArray(inChar, 3);
while (Serial1.available() > 0) {
junk = Serial1.readString();
}
int num = atoi(&inChar[1]);
speed = num;
Serial1.println(speed);
if (inChar[0] == 'A') {
Serial1.println(inChar[0]);
Forwards(speed);
}
if (inChar[0] == 'B') {
Backwards(speed);
}
if (inChar[0] == 'C') {
Steer_Left(speed);
}
if (inChar[0] == 'D') {
Steer_Right(speed);
}
if (inChar[0] != 'A' || inChar[0] != 'B' || inChar[0] != 'C' || inChar[0] != 'D') {
digitalWrite(motor1clock, LOW);
digitalWrite(motor1clockc, LOW);
digitalWrite(motor2clocko, LOW);
digitalWrite(motor2clock, LOW);
}
inputString = "";
}
}
The input code format would be "command speed", for example: "A255".
There are more than 3 bytes in string "A255", so I recommend you to define inChar[6].
And, did "Serial1.println(speed);" print the desired speed?

Resources