Go Down

Topic: How to use EEPROM on Arduino to remember the current state of the solenoid... (Read 355 times) previous topic - next topic


Hi i wanted some advice to add the eeprom on my code I'm controling the the solenoid using HC-05 module. And reason why I wanted to know how to use the EEPROM on my arduino is "if the arduino is switch off it will recover the current state he left the solenoid.In the circuit diagram its not connected on pin 13 but conencted on pin 12(Sorry for bad english)

Code: [Select]
String inputString = "";
String command = "";
String value = "";
String password = "1234"; // this is the password for opening and closing your door
                            // you can set any password you like using digit and symbols
boolean stringComplete = false;
const byte solenoidPin = 12; // pin for solenoid transistor
int Speed = 100;
void setup(){
  //start serial connection
  Serial.begin(9600);  // baud rate is 9600 must match with bluetooth
  //The String reserve() function allows you to allocate a buffer in memory for manipulating strings.
  inputString.reserve(50);  // reserve 50 bytes in memory to save for string manipulation
  boolean stringOK = false;
  pinMode(solenoidPin, OUTPUT);
void loop(){
  // if arduino receive a string termination character like \n stringComplete will set to true
  if (stringComplete) {
    // identified the posiion of '=' in string and set its index to pos variable
    int pos = inputString.indexOf('=');
    // value of pos variable > or = 0 means '=' present in received string.
    if (pos > -1) {
      // substring(start, stop) function cut a specific portion of string from start to stop
      // here command will be the portion of received string till '='
      // let received string is open=test123
      // then command is 'open'
        command = inputString.substring(0, pos);
      // value will be from after = to newline command
      // for the above example value is test123
      // we just ignoreing the '=' taking first parameter of substring as 'pos+1'
      // we are using '=' as a separator between command and vale
      // without '=' any other character can be used
      // we are using = menas our command or password must not contains any '=', otherwise it will cause error
        value = inputString.substring(pos+1, inputString.length()-1);  // extract command up to \n exluded
       // password.compareTo(value) compare between password and value string,if match return 0
    if(!password.compareTo(value) && (command == "OPEN")){
          // if password matched and command is 'OPEN' than door should open
           openDoor(); // call openDoor() function
           Serial.println(" OPEN"); // sent open feedback to phone
    else if(!password.compareTo(value) && (command == "CLOSE")){
          // if password matched and command is 'CLOSE' than door should close
           Serial.println(" CLOSE"); // sent " CLOSE" string to the phone
    else if(password.compareTo(value)){
          // if password not matched than sent wrong feedback to phone
           Serial.println(" WRONG");
       // clear the string for next iteration
       inputString = "";
       stringComplete = false;
void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline or a carriage return, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n' || inChar == '\r') {
      stringComplete = true;
void openDoor(){
   digitalWrite(solenoidPin, LOW);
void closeDoor(){
   digitalWrite(solenoidPin, HIGH);


Have you looked at the EEPROM examples in the IDE ?

You would need to save the state of the solenoid each time it changed.  How often is it likely to change bearing in mind that the EEPROM is subject to a limitation when it comes to write cycles.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up