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 .
