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);*_
}