Joystick button, EEPROM, servo motor

Hello to all,
I am doing a project with Arduino Uno with two servo motors and one Joystick. I want to try after clicking the joystick button how can set a new direction? In here I have already made the last state, which means after pressing reset or power off the last state will be moved but how can hold or give new values from the Joystick button. if anyone has an idea it's grateful to me.

Welcome to the forum

Please post your current sketch using code tags when you do to make the sketch easier to examine and copy

Hallo to all,
Yestday I have post my question without code, my problem is I want to save in EEPROM 5 position which will given by joystick and after the reset button or power off it will be save and after sometime later by clicking joystick button I want to give new 5 position and repeat this process. How can solve this problems, if you have any idea it will be great to me. Thank you

#include <EEPROM.h>
#include <Servo.h>

Servo boden;// using two servo
Servo turm;
int joyX= A0; // connect analog pin with joystick
int joyY= A1;
int pos_x;
int pos_y;
int servoboden = 5 ; // connecting pin with servos
int servoturm = 6 ;
int initial_position_boden = 90;
int initial_position_turm = 90;
#define mosfet 3
#define button 4 // joystick button
int pos1_x ; // initalization of five different position
int pos1_y ;
int pos2_x;
int pos2_y;
int pos3_x;
int pos3_y;
int pos4_x;
int pos4_y;
int pos5_x;
int pos5_y;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
boden.attach (servoboden);
turm.attach (servoturm);
boden.write (initial_position_boden);
turm.write (initial_position_turm);
pinMode (joyX,INPUT);
pinMode (joyY,INPUT);
pinMode (mosfet,OUTPUT);
pinMode (button, INPUT);
digitalWrite(button, HIGH);

}
void loop() {
// put your main code here, to run repeatedly:

//Laser

Reset();

laservoll();
//lesen von daten von Analogeingängen von Joystick

pos_x = analogRead(joyX);

pos_y = analogRead(joyY);

int btn = digitalRead (button);
// if the joystick button is pressed
if(btn == LOW){

if(pos1_x == NULL){
pos1_x = Backpos1_x();
pos1_y = Backpos1_y();
EEPROM.write(1,pos1_x) ;
EEPROM.write(2,pos1_y) ;
delay(1000);
}else{ if(pos2_x == NULL){
pos2_x = Backpos2_x();
pos2_y = Backpos2_y();
EEPROM.write(3,pos2_x) ;
EEPROM.write(4,pos2_y);
delay(1000);
}else{if(pos3_x == NULL){

pos3_x = Backpos3_x();
pos3_y = Backpos3_y();
EEPROM.write(5,pos3_x) ;
EEPROM.write(6,pos3_y);
delay(1000);
}else{
if(pos4_x == NULL){
pos4_x = Backpos4_x();
pos4_y = Backpos4_y();
EEPROM.write(7,pos4_x) ;
EEPROM.write(8,pos4_y);
delay(1000);
}else{if(pos5_x == NULL){
pos5_x = Backpos5_x();
pos5_y = Backpos5_y();

EEPROM.write(9,pos5_x) ;
EEPROM.write(10,pos5_y);
delay(1000);
}else{ boden.write (initial_position_boden);
turm.write (initial_position_turm);
} }}}}}

/*if(Switchcounter <=5){
Reset();
}else{

Set();

}*/

//boden
if (pos_x < 300)
{
if (initial_position_boden <2 )
{ }
else{ initial_position_boden = initial_position_boden - 2; boden.write ( initial_position_boden ) ; delay (10) ; } }
if (pos_x > 700){
if (initial_position_boden > 180)
{ }
else
{
initial_position_boden = initial_position_boden +2 ;
boden.write ( initial_position_boden ) ;
delay (10) ;
}}

//turm
if (pos_y < 300)
{
if (initial_position_turm < 2)
{ }
else{ initial_position_turm = initial_position_turm - 2; turm.write ( initial_position_turm) ; delay (10) ; } }
if (pos_y > 700){
if (initial_position_turm > 180)
{ }
else
{
initial_position_turm = initial_position_turm + 2;
turm.write ( initial_position_turm ) ;
delay (10) ;
}}}

// given five position which will be save in EEPROM
void Reset(){
if(pos5_x != NULL){

  boden.write(EEPROM.read(1) );
  turm.write(EEPROM.read(2));
  delay(500);
 
  boden.write(EEPROM.read(3) );
  turm.write(EEPROM.read(4));
  delay(500);
 
  boden.write(EEPROM.read(5) );
  turm.write(EEPROM.read(6));
  delay(500);
 
  boden.write(EEPROM.read(7) );
  turm.write(EEPROM.read(8));
  delay(500);
 
  boden.write(EEPROM.read(9) );
  turm.write(EEPROM.read(10));
  delay(500);
   }}

// save after clicking reset button

void Set(){

if(digitalRead(button) == LOW ){
  
  
  boden.write(EEPROM.read(1) );
  turm.write(EEPROM.read(2));
   delay(500);
 
  boden.write(EEPROM.read(3) );
  turm.write(EEPROM.read(4));
  delay(500);
 
  boden.write(EEPROM.read(5) );
  turm.write(EEPROM.read(6));
  delay(500);
 
  boden.write(EEPROM.read(7) );
  turm.write(EEPROM.read(8));
  delay(500);
 
  boden.write(EEPROM.read(9) );
  turm.write(EEPROM.read(10));
  delay(500);

}else{
Reset();
}

}
void laservoll(){
analogWrite(mosfet, 255);
}
void laserhalb(){
analogWrite(mosfet,128);
}
void laseraus(){
analogWrite(mosfet,0);
}

int Backpos1_x()
{
return boden.read();
}

int Backpos1_y()
{
return turm.read();
}

int Backpos2_x()
{
return boden.read();
}

int Backpos2_y()
{
return turm.read();
}

int Backpos3_x()
{
return boden.read();

}
int Backpos3_y()
{
return turm.read();
}
int Backpos4_x()
{
return boden.read();
}

int Backpos4_y()
{
return turm.read();
}
int Backpos5_x()
{
return boden.read();
}
int Backpos5_y()
{

return turm.read();

}

Topics merged

Did you miss the request to use code tags when posting your code ?

quote="UKHeliBob, post:2, topic:971272"]
post your current sketch using code tags when you do
[/quote]

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

[/quote]

Hallo UKHEILBOb,
how can I use a code tag in this form I am bit new .

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.