Arduino + servo + potentiometer + buttons NEED HELP PLEASE

Hello everyone ,

thanks in advance for reading this message .
I'm writing as a beginner user of Arduino .

My question is more for the code .

My project goal is the following :

For this project I have 3 buttons ,1 servo and 1 potentiometer .
1 button is used to set up the endstop position A
1 button for endstop position B
1 button to start or stop the motion which is to go from A to B and B to A endlessly .
the potentiometer is used at the beginning to move the servo to choose the position A and B .
And here I'm lost , I don't know how to set up the A and B position and then start the motion .
The first part which is to find the position with the poti is ok .

Please find attached the circuits and then the code below :

CODE :

#include <Servo.h> 
Servo myservo1;  

// Constants
const int buttonPinA = 3;               // digital pin Mem button1
const int buttonPinB = 4;  
const int buttonPinStart = 5;             // digital pin Act button1
const int servoPin = 2;                  // Servo1 PWM pin
const int potpin = A0;                     // analog pin used to connect the potentiometer

// Variables  
int val1 = 0;
int posPot1 = 0;
int pos = 0;
int posA =0;                        //Servo position A
int posB=0; 						 //Servo position B
int buttonStateA = LOW;                   // memory pushbutton1 status
int buttonStateB = LOW;
int buttonStateStart = LOW  ;                 // action pushbutton1 status

void setup() 
{
   Serial.begin(9600);
   myservo1.attach(servoPin);                     // attaches the servo on pin 9 (digital) to the servo object          
   pinMode(buttonPinA, INPUT);  
   pinMode(buttonPinB, INPUT);
}

void loop()
{
 //Reads analog input
val1 = analogRead(potpin);              // reads the value of the potentiometer (value between 0 and 1023)   
val1 = map(val1, 0, 1023, 0, 179);       // scale it to use it with the servo (value between 0 and 180)  
 //play servo with potentiometer    
myservo1.write(val1);

 //Reads digital input 
buttonStateA = digitalRead(buttonPinA);
buttonStateB = digitalRead(buttonPinB);
 if(buttonStateA == HIGH) 
 {			
posA = val1;
}
 if(buttonStateB == HIGH && posA > 0 ) 
 { 	  
posB = val1;
 }
if(buttonStateStart == HIGH && run>0 )
{
   
 for (pos = posA ; pos <= posB; pos += 1) 
{ // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo1.write(pos);              // tell servo to go to position in variable 'pos'
delay(15);                       // waits 15ms for the servo to reach the position
 }

 for (pos = posB; pos >= posA; pos -= 1) 
{ // goes from 180 degrees to 0 degrees
 myservo1.write(pos);              // tell servo to go to position in variable 'pos'
 delay(15);                       // waits 15ms for the servo to reach the position
 }
delay (10000);
}  
}

Thanks again for helping me .

Code tags find out about how to use them.

How about in setup the servo is first sent to position 90, then the servo is sent towards 0 degrees till the button is pressed, record that position in a global. then send the servo towards position 180 till the button is pressed, then store that position. Establishing the end points. After you have coded to detect the end points then post your code in code tags or if it doesn't work ask questions.

Please edit your first post and put your code in code tags.

the picture looks like a WOWKI-simulation.
Would you mind to post the link to the WOKWI-simulation

In the picture the middle-contact of the potentiometer un-connected

best regards Stefan

Hello , it's simulation from thinkcad circuits . Here is the link :

It's seems not connected because the wire is white . Sorry .

Hello Thanks for you answer .
The only point is , I cannot set with a good accuracy with that .

Regarding the post , I modified it according to your request .

Break your code into individual pieces and code each in a separate sketch. Get one thing working right then move on to the next and then finally combine them. Work iteratively and save regularly with “save as” to get new names. Coding everything together often results in a bit of a mess.

Use basic principles:
Avoid delay and blocking code
Think loop, remember code goes round and round fast so often you need state machines for the uC to know what it should be doing this loop.
Use very good names for sketches, variables and functions
Try to encapsulate your code so that it will work when combined with other code I.è keep global variables to a minimum
Use the arduino language reference to get exact syntax and examples
Use serial print to debug

I would start with creating some button code using millis timing, detecting state change for when the button gets pressed and including debouncing. Get the buttons to return a serial output eg button A Pressed, released etc

Welcome to the forum BTW

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