Storing the Charge State with Arduino

Storing the Charge State with Arduino © GPL3+

You'll learn how to store the last command used for actuate or desactuate a charge/device.

  • 538 views
  • 0 comments
  • 4 respects

Components and supplies

About this project

Introduction

Several projects need to store information and data indefinitely, even if the device is turned off. Among some types of projects, we can mention: storage of the temperature setpoint for triggering the load, storage of the current time and storage of the last command performed by the equipment operator, for example.

With the last example, I'll explain how to develop a system to storage the last command performed by the user. This project can be used case occur lack of energy in electrical network. And when energy is established again, the microcontroller obtain the last command to energyse ou desenergyse the equipment based on the last command of the user. To solve these problems, we can use the EEPROM memory.

This project is relatively simple and now I'll explain the electrical circuit mounted.

Electrical Circuit

Based on the circuit in the Figure 1, all project was created.Basically, was used the button with pull-down configuration connected on the digital pin 10.

When the button is pressed, the LED connected on the digital pin 13 the led will be activated and when button is pressed again (second time), the LED will be deactivated. The physical circuit is presented in the Figure 2.

In the Figure 2 was used a power supply for the circuit. Now, Will be presented the code developed.

Firmware

Based on the problem, was created a solution. Now, will be presented the work logical for the project.

For solve the problem, we use the code presented below. Firstly, we include the EEPROM library, because we use the EEPROM Memory for store the state of the command used for activate ou deactivate the charge. The #define directive was used to define names for digital pin numbers and memory position so that we could use them in code.

The position of number 120 was used to store the state value of the charge command. In the setup function, the pin 10 (pino) was configurated as Input and the digital pin 13 was configurated as Output.

#include <EEPROM.h> //Declaracao da biblioteca EEPROM
#define pino 10 //Botao para salvar os dados conectado no pino digital 10
#define LED 13 //Pino digital onde o LED/CARGA esta conectado(a)
#define PosiMem 120 //Posicao da memoria que sera utilizada para salvar os valores
int botao = 0; //Variavel que armazena o estado do pino 10 - botao
int estado = 0; //Variavel que armazenara o estado do botao para sabermos quando o botao foi solto/pressionado
int cont = 0; //
void setup() 
{
   pinMode(pino, INPUT);
   pinMode(LED, OUTPUT);
   cont = EEPROM.read(PosiMem);
}
void loop() 
{
   botao = digitalRead(pino);
   if(botao == 0 && estado == 1)
   {  
       estado = 0;
   }
   if(botao == 1 && estado == 0)
   {  
       cont++;
       if(cont > 1)
       {
       cont = 0;  
       }
       estado = 1; 
   }
   if(cont == 0)
   {
       digitalWrite(LED, LOW);
       EEPROM.write(PosiMem, cont);
   }
   if(cont == 1)
   {
       digitalWrite(LED, HIGH);
       EEPROM.write(PosiMem, cont);
   }
}

Now, note that there is something very important for you to understand the working logic of the project. On the following line:

cont = EEPROM.read(PosiMem);

Whenever the code starts executing, the first action it takes is to read the last command made to turn the load on or off, which was stored in position 120 (PosiMem) of the EEPROM memory.

If the value stored in the cont variable is 0 and store the value on the EEPROM Memory, the charge will be deactivated or if the value is 1, the charge will be activated and store the value on the EEPROM Memory, according the code presented below:

 if(cont == 0)
   {
       digitalWrite(LED, LOW);
       EEPROM.write(PosiMem, cont);
   }
   if(cont == 1)
   {
       digitalWrite(LED, HIGH);
       EEPROM.write(PosiMem, cont);
   }

After this, in the loop the button is monitored to know if the button is pressed or unpressed. When the button is pressed, is executed the following code:

   if(botao == 1 && estado == 0)
   {  
       cont++;
       if(cont > 1)
       {
       cont = 0;  
       }
       estado = 1; 
   }

When the condition is true, the cont variable is incremented and its value is verified if is more than 1. If the comparation result was true, the value will be 0. But, if your value is false, don't will be 0 and the estado variable will be 1. The estado variable is used to control the logic of input in the condition for increment the cont variable.

When the button is unpressed, the botao variable will be equal at 0 and the estado variable with 0 value and, therefore, will be executed the following condition:

if(botao == 0 && estado == 1)
   {  
       estado = 0;
   }

When the condition is true, the estado variable will be with the 0 value. This allows the execution to input in the condition for increment the cont variable.

When the button is pressed again, the condition (if(botao == 1 && estado == 0)) will be executed and the cont variable will be incremented. Its value will be 2, but in the condition (if(cont > 1)) the result will be true. In the moment, the cont variable is zero, because cont value is more than one.

After this, the LED will be deactivate and the zero value will be stored in the position 120 of the EEPROM Memory.

Therefore, using this logic is possible to apply in various projects that needs store permanent datas on the Microcontroller.

Acknowledgments

The Silícios Lab thanks you for reading and following the published content. If you want to contact us to suggest improvements and new contents, please contact us. Check back often!

Comments

Similar projects you might like

Standalone Arduino Applied in Projects

Project tutorial by PCBWay and Silícios Lab

  • 5,760 views
  • 8 comments
  • 37 respects

Control Arduino Uno Using ESP8266 WiFi Module and Blynk App

Project tutorial by Adithya TG

  • 5,254 views
  • 10 comments
  • 18 respects

Ultrasonic Sensor with Alarm, LCD and Temperature

Project tutorial by MichDragstar

  • 4,697 views
  • 7 comments
  • 19 respects

ARDUINO UNO or TRINKET PRO 5V 6 Chasing LEDS with POT and PB

Project tutorial by MichDragstar

  • 1,888 views
  • 2 comments
  • 3 respects

Controlling Arduino State Machine via Web

Project tutorial by phpoc_man

  • 1,791 views
  • 4 comments
  • 22 respects
Add projectSign up / Login