Arduino Project (TRAFFIC LIGHTS) *need ideas* - debugging

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.

Related

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.

ESP32 unable to detect interrupt

The code below is suppose to detect a pulse from a rain gauge. It is working in esp8266 but not in esp32 nodemcu. The logic seems fine I suppose I did something wrong while define the pin?
const int interruptPin = 18;
volatile boolean interrupt = false;
const int led = 2 ;
void setup() {
Serial.begin(115200);
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), ISR, FALLING);
pinMode(led,OUTPUT);
digitalWrite(led, LOW);
}
void loop() {
if (interrupt == true) {
Serial.println("An interrupt occurred");
delay(500);
digitalWrite(led, LOW);
interrupt = false;
}
delay(1000);
}
IRAM_ATTR void ISR () {
digitalWrite(led, HIGH);
interrupt = true;
}

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);
}
}

convert analog read to range to time

I'm trying to blink a led with analog sensor between 8 and 40 times a minutes
I have try this code but I realize that I had to convert the valorSensor to time. How is the way to do it?
int led = 13;
int pinAnalogo = A0;
int analogo;
int valorSensor;
void setup() {
pinMode(led, OUTPUT);
}
void loop() {
analogo = analogRead(pinAnalogo);
valorSensor = map(analogo, 0, 1023, 4, 80);
digitalWrite(led, HIGH);
delay(valorSensor);
digitalWrite(led, LOW);
delay(valorSensor);
}
The problem here is not so much the code but biology. To see a blinking (and not just a flicker you will need times from 250ms and up) 24 frames/sec are perceived as motion so to get a "blink" you might start with 4 frames/sec (=250ms)
So my proposal is a blink without delay as function and a tuning parameter (blinkSpeedMultiplyer) for testing
/* Blink without Delay as function
Turns on and off a light emitting diode (LED) connected to a digital pin,
without using the delay() function. This means that other code can run at the
same time without being interrupted by the LED code.*/
// constants won't change. Used here to set a pin number:
const int blinkSpeedMultiplyer = 50; // very low
const int ledPin = 13;
const int pinAnalogo = A0;
// Variables will change:
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long startTime = millis(); // will store last time LED was updated
unsigned long blinkTime; // interval at which to blink (milliseconds)
int analogo;
int valorSensor;
int ledState = LOW; // ledState used to set the LED
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
analogo = analogRead(pinAnalogo);
valorSensor = map(analogo, 0, 1023, 4, 80);
blinkLed(valorSensor); // call the function
}
// Function to blink without blocking delay
void blinkLed(int inValue) {
blinkTime = blinkSpeedMultiplyer * inValue;
if (millis() - startTime >= blinkTime) {
// save the last time you blinked the LED
startTime = millis();
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}

Void loop not looping properly

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.

Resources