Relay starting on but not going off - arduino

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.

Related

Restarting Arduino and keeping the variables

I would like to restart the arduino board but keeping values of some variable. My solution would be calling setup() whenever I would like to restart. Something like this:
int led = 13;
int led2 = 50;
boolean restart = false;
void setup() {
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
if(!restart){
digitalWrite(led, LOW); // will only happen once
delay(3000);
}
digitalWrite(led, HIGH); // turn the LED on (and will be always on even after reset)
restart = true;
delay(3000);
digitalWrite(led2, HIGH); // indicate restart is called
delay(1000);
digitalWrite(led2, LOW);
setup();}
void loop() { }
I was thinking if this will cause any heavy usage in RAM. Or is there any better methods?
Thank you.
Use the EEPROM library. Have a button with an interrupt that saves the variable then read the variable in the setup() routine.
If you are getting or changing information slow enough you could constantly write the value but beware EEPROM on this chip is only certified to 100,000 writes per byte.

Arduino Code LED failure to alternate

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

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() {
}

Receiving SMS using GSM and controlling LED using Arduino

Has someone come up with a solution with the above stated problem?
We are using Arduino Duemilanove and SIM 900 GSM module (http://robokits.co.in/shop/index.php?main_page=product_info&products_id=303)
We've tried to work on the similar problem of lightning LEDs from port 9-12 when we send an sms #aibicidi, where i = 0 or 1, 0 =off, 1=on. E.g. #a1b1c1d1 will switch on all the LEDs.
When we upload the code and run it through serial monitor and enter the #a1b1c1d1 in the serial monitor, we can see all the LEDs lighten up. But if we send the sms with having content "#a1b1c1d1", we don't see any function of LEDs.
It would be great if anyone can give some guidance about the same.
char inchar; //Will hold the incoming character from the Serial Port.
int led1 = 9;
int led2 = 10;
int led3 = 11;
int led4 = 12;
void setup()
{
// prepare the digital output pins
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
//Initialize GSM module serial port for communication.
Serial.begin(9600);
delay(3000); // give time for GSM module to register on network etc.
Serial.println("AT+CMGF=1"); // set SMS mode to text
delay(200);
Serial.println("AT+CNMI=3,3,0,0"); // set module to send SMS data to serial out upon receipt
delay(200);
}
void loop()
{
//If #a1b1c1d1 comes as sms, all LEDs should light up.
if(Serial.available() >0)
{
inchar=Serial.read();
if (inchar=='#')
{
delay(10);
inchar=Serial.read();
//first led
if (inchar=='a')
{
delay(10);
inchar=Serial.read();
if (inchar=='0')
{
digitalWrite(led1, LOW);
}
else if (inchar=='1')
{
digitalWrite(led1, HIGH);
}
delay(10);
//Second led
inchar=Serial.read();
if (inchar=='b')
{
inchar=Serial.read();
if (inchar=='0')
{
digitalWrite(led2, LOW);
}
else if (inchar=='1')
{
digitalWrite(led2, HIGH);
}
delay(10);
// Third led
inchar=Serial.read();
if (inchar=='c')
{
inchar=Serial.read();
if (inchar=='0')
{
digitalWrite(led3, LOW);
}
else if (inchar=='1')
{
digitalWrite(led3, HIGH);
}
delay(10);
//Fourth led
inchar=Serial.read();
if (inchar=='d')
{
delay(10);
inchar=Serial.read();
if (inchar=='0')
{
digitalWrite(led4, LOW);
}
else if (inchar=='1')
{
digitalWrite(led4, HIGH);
}
delay(10);
}
}
Serial.println("AT+CMGD=1,4"); // delete all SMS
}
}
}
}
}
First do not use delay
Serial.begin(9600);
delay(3000); // give time for GSM module to register on network etc.
This is neither necessary nor reliable. Instead of waiting some random time, you can check the network status with AT+CFUN and/or AT+COPS. If the GSM module is already attached to a network when you open the serial connection, it is a waste of time waiting like that. And if is not attached you should wait explicitly for that to happen (polling CFUN/COPS or enabling AT+CREG), otherwise you risk waiting too short time. See the 27.007 specification for more information for those commands.
Second do not use delay
Serial.println("AT+CMGF=1"); // set SMS mode to text
delay(200);
Please do not write code like this. See this answer on why using delay is such a bad idea, and this answer for suggestion to how to handle properly.

Resources