Restarting Arduino and keeping the variables - arduino

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.

Related

This code error - too few arguments to function 'void digitalWrite(uint8_t, uint8_t)'

Hello I'm new to coding & made the code but there's this error saying:
too few arguments to function 'void digitalWrite(uint8_t, uint8_t)'. Did I do it wrong or is it something else?
#include <Servo.h>
Servo Servo1;
Servo Servo2;
void setup() {
Servo2.attach(9);
Servo1.attach(10);
pinMode(5, INPUT); //IR 1
pinMode(4, INPUT); //IR 2
pinMode(6, OUTPUT); //Standby LED#1
pinMode(7, OUTPUT); //Standby LED#2
}
void loop() {
if (digitalWrite(4) == HIGH) {
Servo1.write(60);
delay(1000);
} else {
digitalWrite(6, HIGH);
}
}
void loop() {
if (digitalWrite(5) == HIGH) {
Servo2.write(60);
delay(1000);
} else {
digitalWrite(7, HIGH);
}
}
There are several issues with your code.
Let's begin with the most obvious. You cannot have multiple loop functions.
Arduino will call setup() and then call loop() over and over in an infinite loop.
The compiler should raise an error for redefining loop.
The second issue is the one you're asking about.
digitalWrite can be used to
set the logic state of a pin, if the pin is configured as OUTPUT
enable/disable the internal pullup resistor if the pin is configured as INPUT
You did the following:
if(digitalWrite(4)== HIGH)
So you
you provided to few arguments to digitalWrite. It expects a pin number and a value (HIGH or LOW)
you use it in an if statement although it does not return any value
As pin 4 is configured as input you probably wanted to use digitalRead to read its state.
use if (digitalRead(4) == HIGH) or simply if(digitalRead(4))
Please read https://www.arduino.cc/en/Tutorial/Foundations/DigitalPins and the rest of the Arduino manual
The digitalWrite argument requires both the pin to write to and the value to set the pin to. Your statement,
if (digitalWrite(4) == HIGH)
should probably be
if (digitalRead(4) == HIGH)
It looks like you should be using digitalRead instead of digitalWrite when reading from a GPIO. See https://arduino.stackexchange.com/q/35965 for more info

Problems with simplest Arduino program

Today I started what is supposed to become a great Arduino career, but I'm already stumped. I may be going crazy, but shouldn't this code blink the LED on the Mega 2560?
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
unsigned int count = 0;
void loop() {
if(count%2) digitalWrite(LED_BUILTIN, LOW);
else digitalWrite(LED_BUILTIN, HIGH);
delay(1);
count++;
}
I know this is not elegant for a blinking LED, but this is a stripped down example for something else, where I need a counter and modulo operations on it. The 'Blink' program works, but this here doesn't.
delay()'s argument is measured in milliseconds (not seconds), so you probably want 1000 rather than 1 to observe the blinking!
delay(1000);
Official Documentation

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.

Relay pin acts differently with load

I have the following code.
void setup(){
pinMode(14, OUTPUT);
digitalWrite(14, HIGH); //Relay
}
void loop(){
if (!digitalRead(14)){
digitalWrite(10,HIGH); //LED
digitalWrite(11,LOW); //LED
}else{
digitalWrite(10, LOW);
digitalWrite(11, HIGH);
}
}
In another portion of the code the relay pin changes states and I'm monitoring that with the digitalRead portion in the loop in hopes of changing which led is on based on the state.
Now the hard part. All of that works, except when I wire the relay to a magnet. This is all for a very intricate door control system with maglocks. And for some reason with the maglock hooked up to the relay the Arduino behaves very differently. It slows to a crawl once the relay is changed. Up until then all is fine, but as soon as the relay is activated, something causes it to slow way down.
What I can't figure out is why all is fine and relay triggers without side affects, until a load is attached to it.
Any ideas? Or a better way of monitoring a relay state? (Without storing its pseudo value in a variable)
You set the pin 14 as OUTPUT, but you're trying to read from it with digitalRead.
What you want to know is the value of the register that stores the value of the port.
You could go the easy way and use an auxiliar variable that stores the pin state like this:
bool state = true;
void setup(){
pinMode(14, OUTPUT);
digitalWrite(14, state); //Relay
}
void loop(){
if (!state){
digitalWrite(10,HIGH); //LED
digitalWrite(11,LOW); //LED
}else{
digitalWrite(10, LOW);
digitalWrite(11, HIGH);
}
}
And edit the rest of the code accordingly so state changes accordingly.
The 'Hardest to understand' solution, is to read the register value. since you're using pin 14 (the same as pin A0) you have to look into the port C According to the Arduino Reference on port manipulation (Link at the end of my answer).
So you can just do this:
void setup(){
pinMode(14, OUTPUT);
digitalWrite(14, HIGH); //Relay
}
void loop(){
if (!BitRead(PORTC,0)){ //Reads bit 0 of the register of PORTC (wich is the state of pin14)
digitalWrite(10,HIGH); //LED
digitalWrite(11,LOW); //LED
}else{
digitalWrite(10, LOW);
digitalWrite(11, HIGH);
}
}
This solution is more elegant and is exactly what you need. It might be harder to come up with so if you don't remember this in the future you could always use the "state" variable method.
Reference Bit Read Operation and Arduino Reference on port manipulation for more information.

How do I program digital pins on arduino uno?

I'm trying to make a microcontroller with an arduino. I am supplying with +5volt from the arduino, sending it to an NC button (so i can manually decide when to output a certain timed pulse). After the button it goes to a pin that I have set as an inPin (pin8). Then I want the program to make pin 7 HIGH(with a delay), and then it goes to a transistor.
This is the code I tried making (I know almost nothing about coding):
int ledPin = 7;
int inPin = 8;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(inPin, INPUT);
}
void loop()
{
if (inPin=HIGH) {
digitalWrite(ledPin, HIGH);
}
delay (500);
digitalWrite(ledPin, LOW);
}
For some reason the outPin is HIGH all the time. I remembered to hook up a resistor to GND so the digital pin would stay LOW when supposed to be LOW.
Thanks in advance!
if(inPin=HIGH) is a mistake, first of all use "==" instead of "=". ALso you need to READ input pin state: int invalue = digitalRead(inPin);
Also, all pins by default coonfigured as inputs, so you don't need use pinMode(inPin, INPUT);
After those changes your code will look like:
int ledPin = 7;
int inPin = 8;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop()
{
if (digitalRead(inPin)==HIGH) digitalWrite(ledPin, HIGH);
delay (500);
digitalWrite(ledPin, LOW);
}

Resources