Go Down

Topic: EEPROM storage of an array (Read 394 times) previous topic - next topic

Thomas1995

So I am moderately new to Arduino.
I have been experimenting with the HC-06 bluetooth shield and am working on a project to control 4 different stepper motors.
Each stepper motor has a slider attached to it.
The sliders are in an app i made on mit app inventor.
the app sends 4 whole integer slider values between 1 and 30 to the hc-06.
i am able to read the values in one at a time, index them into an array, and manipulate them.
however, once i begin comparing the current slider value with the previous slider value which is stored in EEPROM, issues begin.


i have attached my code as well as a serial monitor output.

for the output, the results are all correct until the THIRD to last Previous slider value. in the app, i sent the FOUR slider values, unchanged, twice. the program reads them in and displays them correctly.

however, i changed SLIDER 1 to 29 with the last send. the program correctly identifies the difference as 14.

The problem occurs next.
Slider 2 was left unchanged
The program correctly reads the current position of Slider 2,
however when trying to compare to the previous position of Slider 2, which should be 14, the program

MISTAKENLY uses the current position of Slider 1 instead of the previous position of Slider 2.


any tips would be appreciated


I also attached the .apk file of the app

Thanks

ieee488

Learn to use code tags to show your code.

sterretje

eeprom.read() and eeprom.update() work with bytes; your slider values are integers.

If your values range from 0 to 255, change the type for slider and prevvalue to byte. Else you must read / write two bytes individually (using e.g. lowByte and highByte); possibly easier to use for integers are eeprom.get() and eeprom.put()
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Mantu

Hi,
 Here is my code for 3 axis robot, position can be saved through pot value, but after replug it was loosing that variable value, so i wrote this code to save that value in EEPROM but its not working.






#include <Servo.h> //add servo library. this library is standard library
#include <LiquidCrystal.h>
#include <EEPROM.h>
#define pragma_ROM
//define variable for saved position of the servos
int servo1PosSave[]={1,1,1,1,1};
int servo2PosSave[]={1,1,1,1,1};
int servo3PosSave[]={1,1,1,1,1};
int SVM1[]={1,1,1,1,1};
int SVM2[]={1,1,1,1,1};
int SVM3[]={1,1,1,1,1};
//define the servos
Servo servo1;
Servo servo2;
Servo servo3;

//define the buttons
const int button3 = 7;
const int button1 = 8;
const int button2 = 13;
const int rs=12, en=11, d4=5, d5=4, d6=3, d7=2;
LiquidCrystal lcd(rs,en,d4,d5,d6,d7);
//define variable for values of the button
int button1Pressed = 0;
boolean button2Pressed = false;
int button3Pressed = 0;
//define potentiometers
const int pot1 = A0;
const int pot2 = A1;
const int pot3 = A2;

//define variable for values of the potentiometers
int pot1Val;
int pot2Val;
int pot3Val;

//define variable for angles of the potentiometer
int pot1Angle;
int pot2Angle;
int pot3Angle;



void setup() {
  //define attached pins of the servos
  servo1.attach(6);
  servo2.attach(9);
  servo3.attach(10);

  //define buttons as input units
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
lcd.begin(16,2);
  Serial.begin(9600);
       
}

void loop() {
  //read the potentiometer values and define the servo angle to
  //the potentiometer value with the map function
 //lcd.setCursor(16, 1);

//lcd.scrollDisplayLeft();

lcd.setCursor(16, 0);
lcd.print("Tissue Dispensing Robot");
//lcd.autoscroll();
lcd.scrollDisplayLeft();
    // wait a bit:
    delay(150);
if(digitalRead(button3) == HIGH){
 
   lcd.setCursor(16, 1);
    //lcd.clear();
  lcd.print("Jog mode selected");
//lcd.scrollDisplayLeft();
    // wait a bit:
    delay(150);
//lcd.clear();
    button3==HIGH;
  pot1Val = analogRead(pot1);
  pot1Angle = map (pot1Val, 0, 1023, 0, 179);
  pot2Val = analogRead(pot2);
  pot2Angle = map (pot2Val, 0, 1023, 0, 179);
  pot3Val = analogRead(pot3);
  pot3Angle = map (pot3Val, 0, 1023, 0, 90);

  //servos move to mapped angles
  servo1.write(pot1Angle);
  servo2.write(pot2Angle);
  servo3.write(pot3Angle);
 
   }
   
 //  servo1.write(servo1PosSave[0]);
  //    servo2.write(servo2PosSave[0]);
  //    servo3.write(servo3PosSave[0]);

  //if button1 is pressed (HIGH), save the potentiometers position
  //as long as button1 is pressed
  if(digitalRead(button1) == HIGH){
   // lcd.clear();
 //lcd.print("Record Button pressed");
    button1Pressed++;
    switch(button1Pressed){
      case 1:
       lcd.setCursor(16, 1);
      //lcd.clear();
       EEPROM.put (SVM1[0],pot1Angle);
        Serial.print('EEPROM VALUE');
       EEPROM.put (SVM2[0], pot2Angle);
        Serial.print('EEPROM VALUE');
       EEPROM.put (SVM3[0],pot3Angle);
        Serial.print('EEPROM VALUE');
lcd.print("Position #1 Saved");
lcd.scrollDisplayLeft();
    // wait a bit:
    delay(150);
       break;
       case 2:
        lcd.setCursor(16, 1);
       //lcd.clear();
      EEPROM.put (SVM1[1],pot1Angle);
       Serial.println('EEPROM VALUE:');
       EEPROM.put (SVM2[1],pot2Angle);
       Serial.println('EEPROM VALUE:');
      EEPROM.put (SVM3[1],pot3Angle);
       Serial.println('EEPROM VALUE:');
        lcd.print("Position #2 Saved");
        lcd.scrollDisplayLeft();
    // wait a bit:
    delay(150);
       break;
       case 3:
        lcd.setCursor(16, 1);
       //lcd.clear();
      EEPROM.put(SVM1[2],pot1Angle);
       Serial.println('EEPROM VALUE:');
       EEPROM.put (SVM2[2],pot2Angle);
       Serial.println('EEPROM VALUE:');
       EEPROM.put (SVM3[2],pot3Angle);
       Serial.println('EEPROM VALUE:');
 lcd.print("Position #3 Saved");
 lcd.scrollDisplayLeft();
    // wait a bit:
    delay(150);
       break;
       
       case 4:
        lcd.setCursor(16, 1);
       //lcd.clear();
   EEPROM.put (SVM1[3],pot1Angle);
   Serial.println('EEPROM VALUE:');
  EEPROM.put (SVM2[3],pot2Angle);
   Serial.println('EEPROM VALUE:');
  EEPROM.put (SVM3[3],pot3Angle);
   Serial.println('EEPROM VALUE:');
lcd.print("Position #4 Saved");
lcd.scrollDisplayLeft();
    // wait a bit:
    delay(150);
       break;
       case 5:
        lcd.setCursor(16, 1);
      // lcd.clear();
      EEPROM.put (SVM1[4],pot1Angle);
      Serial.println('EEPROM VALUE:');
      EEPROM.put (SVM2[4],pot2Angle);
      Serial.println('EEPROM VALUE:');
      EEPROM.put (SVM3[4],pot3Angle);
      Serial.println('EEPROM VALUE:');
lcd.print("Position #5 Saved");
lcd.scrollDisplayLeft();
    // wait a bit:
    delay(150);
       break;
    }
  }
  //if button2 pressed (HIGH), the servos move saved position
  if(digitalRead(button2) == HIGH){
     
    button2Pressed = true;
   
  }
  if(button2Pressed){
    lcd.setCursor(16, 1);
    //lcd.clear();
 lcd.print(" Tissue Request");
 lcd.scrollDisplayLeft();
    // wait a bit:
    delay(150);
    for(int i=0,j=0; j,5,i<5; i++,j++){
      EEPROM.get(SVM1[j],servo1PosSave);
      Serial.println('SVM1[j]:');
      EEPROM.get(SVM2[j],servo2PosSave);
      Serial.println('SVM2[j]:');
      EEPROM.get(SVM3[j],servo3PosSave);
      Serial.println('SVM3[j]:');
     servo1.write(servo1PosSave);
      servo2.write(servo2PosSave);
      servo3.write(servo3PosSave);
      delay(2000);
     }
  //  servo1.write(servo1PosSave[0]);
    //  servo2.write(servo2PosSave[0]);
      //servo3.write(servo3PosSave[0]);
  button2Pressed = false;
   lcd.setCursor(16, 1);
  lcd.clear();
  }
  delay(500);
 
   
 }



Go Up