Arduino Code LED failure to alternate - arduino

Traffic light just stays on red rather than alternating.
Wanted it to stay on for 10s then off for 10s, continuing ad infinitum.
Dont want to use the delay function cos need to do other stuff while the LED continues to alternate.
Thanks
int red = 10; // red traffic light LED on pin 10
int redcounter;
// the setup routine runs once when you press reset:
void setup()
{
// initialize the digital pin as an output.
pinMode(red, OUTPUT);
digitalWrite(red, LOW);
redcounter = 0;
}
// the loop routine runs over and over again forever:
void loop()
{
redcounter = redcounter +1;
if(redcounter==1000)
{
redcounter=0;
if(digitalRead(red)==HIGH)
{
digitalWrite(red, LOW);
}
if(digitalRead(red)==LOW)
{
digitalWrite(red, HIGH);
}
}

You try to read a port which is configured as an OUTPUT. I don't know if this is supposed to work, but it would be more clear if you simply use another port as INPUT and feedback the signal you want to check in that port. I'm not sure however if it makes much sense to check the state of a signal you generate yourself (?). Moreover your redcounter is just "Active waiting", and arduino provides a delay function which does exactly that.
int red=10;
int signal=11;
void setup()
{
pinMode(red, OUTPUT);
pinMode(signal, INPUT);
digitalWrite(red, LOW);
}
void loop()
{
delay(1000);
if(digitalRead(signal)==HIGH)
{
digitalWrite(red, LOW);
}
if(digitalRead(signal)==LOW)
{
digitalWrite(red, HIGH);
}
}

Use elseif instead of if here:
if(digitalRead(red)==HIGH)
{
digitalWrite(red, LOW);
}
else if(digitalRead(red)==LOW)
{
digitalWrite(red, HIGH);
}
In your old solution every time red turned low, it was turned high a moment later.

Two issues in your code are that digitalread will not read an output pin and if you use an increment counter you won't be able to accurately denote time. Sorry if I missed a bracket or something I was doing this on the mobile app.
Use this:
int red = 10; // red traffic light LED on pin 10
int redcounter;
boolean pinState = false;
int delayTime = 10000;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(red, OUTPUT);
digitalWrite(red, LOW);
redcounter = millis();
}
// the loop routine runs over and over again forever:
void loop() {
if((millis() - red counter) > delayTime) {
redcounter=millis();
if(pinState) {
digitalWrite(red, LOW);
pinState = false;
}
else {
digitalWrite(red, HIGH);
pinState = true;
}
}
}

Related

Why is my single button LED light control only working sometimes?

I just started getting into Arduino and I put together a very simple led control with one button to change the on and off state. When I push the button the light will come on, but it will only stay on about 50% of the time (so I have to push it multiple times until it actually stays on), and The same thing happens when I try to turn it off.
Is there some problem with my code? Or is it likely to be a wiring issue?
//define variables
int ledPin = 5;
int btnOnPin = 9;
bool isItOn = false;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(btnOnPin, INPUT_PULLUP);
}
void loop() {
if(digitalRead(btnOnPin) == LOW)
{
if(isItOn == false)
{
digitalWrite(ledPin, HIGH);
isItOn = true;
}
else if (isItOn == true)
{
digitalWrite(ledPin, LOW);
isItOn = false;
}
}
}
The button is connected to ground and pin9, the led is connected to ground, and pin5 through a 220ohm resistor.
This should be super simple, but for some reason I can't get it to work properly.
Thanks for any help
try putting the arduiton to sleep for a few milli seconds after changing its val
void loop() {
if(digitalRead(btnOnPin) == LOW)
{
if(isItOn == false)
{
digitalWrite(ledPin, HIGH);
isItOn = true;
}
else if (isItOn == true)
{
digitalWrite(ledPin, LOW);
isItOn = false;
}
sleep(K);
}

Relay starting on but not going off

I write some very basic code to start on the relay and wait for 5 to 3 sec and then turn off it and exit the loop.
relay starting on but its not turning off and also relay taking the same time to start as I set the delay time (delay time = starting time) I don't know why.
void setup() {
pinMode(7, OUTPUT);
}
void loop() {
digitalWrite(7, HIGH);
delay(3000);
digitalWrite(7, LOW);
exit(0);
}
One possible reason is , loop running continuously with out exit. Which turns ON the relay immediately after OFF. Try code below
int count=0;
void setup()
{
pinMode(7, OUTPUT);
}
void loop()
{
if(count<1)
{
digitalWrite(7, HIGH);
delay(3000);
digitalWrite(7, LOW);
count++;
}
}
If you are facing the problem again, try swapping HIGH and LOW in above code,
it is active LOW for some boards.

How do I fix this arduino code ?(DETAILS BELOW)

I have an Arduino Mega 2560. I have an LED grounded and connected to pin 12 by breadboard and a switch connected to pin 7 and pin 2 by breadboard. I have written this program so that a button press changes the LED's state between on and off. The components all seem to be working so I believe it is a coding issue. Here's my code:
boolean running = false;
boolean ledon = true;
void statechange() {
if(running == false) {
running = true;
ledon = !ledon;
if(led on) {
digitalWrite(12, HIGH);
} else {
digitalWrite(12, LOW);
}
delay(1000);
running = false;
}
}
void setup() {
pinMode(12, OUTPUT);
pinMode(7, OUTPUT);
digitalWrite(7, HIGH);
}
void loop() {
attachInterrupt(0,statechange,CHANGE);
}
I don't receive any errors, it just doesn't work, the LED stays off no matter what.
Are you sure this is right?
if(ledon)
{
digitalWrite(12, HIGH);
}
else
{
digitalWrite(12, LOW);
}
It looks like "If the LED is ON, turn it ON, otherwise, If the LED is OFF, turn it OFF."
Shouldn't it be:
if(ledon)
{
digitalWrite(12, LOW);
ledon = false;
}
else
{
digitalWrite(12, HIGH);
ledon = true;
}
If you're using ledon to keep track of state, but change the state independently of your if statement, the two could potentially become out of sync. Especially if code somewhere else can change the state of ledon.
The other thing that bothers me is this line: if(running == false)
If that is true (e.g. your hardware? is not running), then what is the point of trying to change I/O states at that time?
boolean ledon = true;
void statechange()
{
ledon = !ledon;
digitalWrite(12, ledon ? HIGH : LOW);
}
void setup()
{
pinMode(12, OUTPUT);
pinMode(7, OUTPUT);
digitalWrite(7, HIGH);
}
void loop()
{
attachInterrupt(0,statechange,CHANGE);
}

Arduino Leonardo Endless Loop

My Code:
*int led = 13;
void setup(){
Serial.begin(9600);
pinMode(led, OUTPUT);
while (!Serial) {
//My code get stack here!
//it stay here looping on endlessly!
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay(500);
}
void loop() {
}*
So this is the problem. the simple program waiting for Serial and the while loop continue forever.
how to fix this. is it a known problem?
int led = 13;
void setup(){
Serial.begin(9600);
pinMode(led, OUTPUT);
while (!Serial1) {
//My code get stack here!
//it stay here looping on endlessly!
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay(500);
}
}
void loop() {
}
I think that is a missing bracket closing setup()
Your loop condition is wrong. If you want to wait for something to appear on the serial port then you need to change it to this:
while (Serial.available() == 0) {
Your code is fine, it looks like your serial port is not connecting. If I modify your code to watch a counter up to 5, and when the counter is 5 then myconnection is true, then !myconnection is false and the loop stops (watch it in Tools>Serial Monitor):
int led = 13;
int counter = 0;
boolean myconnection = false;
void setup(){
Serial.begin(9600);
pinMode(led, OUTPUT);
while (!myconnection) {
//My code get stack here!
//it stay here looping on endlessly!
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay(500);
//here's my debug stuff
counter++;
if (counter ==5 ){
myconnection = true;
}
Serial.println("counter: " + String(counter) + ", myconnection: " + String(myconnection));
}
}
void loop() {
}

How to turn on an LED on a certain distance with an Arduino

I'm making an Arduino program and I can't get it to work.
All the program does is turn an LED on whenever someones becomes 5 inches away from the sensor. The problem is whenever I start the program the LED stays off no matter what. Here's my program
const int pingPin =7 ;
const int ledPin = 13;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop() {
long duration, inches, cm;
pinMode(pingPin,OUTPUT);
digitalWrite(pingPin,LOW);
delayMicroseconds(2);
digitalWrite(pingPin,HIGH);
delayMicroseconds(5);
digitalWrite(pingPin,LOW);
pinMode(pingPin,INPUT);
duration =pulseIn(pingPin,HIGH);
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
while (true) {
if (inches <= 5) {
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin, LOW);
}
}
}
long microsecondsToInches(long microseconds)
{
return microseconds /74/2;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds /29/2;
}
How can I fix this so that the LED turns on and off at the correct distances?
Take a close look at this last bit of your code:
while (true) {
if (inches <= 5) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}
You see what it is doing? It is looping forever.
So... your var inches gets a value once and then for all and eternity (or until you unplug) that value will remain the same..
Get rid of the while() and you should see some responsiveness (assuming everything else is hooked up correctly).

Resources