Hi there.
I am currently making some code here:
#include <Servo.h>
Servo myservo1;
Servo myservo2;
int CW = 8; // HALL SENSOR 1
int CCW = 10; // HALL SENSOR 2
int potpin = A0;
int val;
void setup() {
pinMode(CW, INPUT);
pinMode(CCW, INPUT);
myservo1.attach(5);
myservo2.attach(6);
int initialHall1 = digitalRead(CW);
int initialHall2 = digitalRead(CCW);
if (initialHall1 == LOW && initialHall2 == LOW) {
val = analogRead(potpin);
val = map(val, 0, 1023, 0, 180);
myservo1.write(val);
myservo2.write(180 - val);
}
else if (initialHall1 == HIGH && initialHall2 == HIGH) {
val = analogRead(potpin);
val = map(val, 0, 1023, 0, 180);
myservo1.write(180 - val);
myservo2.write(val);
}
}
void loop() {
int hall1 = digitalRead(CW);
int hall2 = digitalRead(CCW);
if (hall1 == LOW && hall2 == HIGH) {
val = analogRead(potpin);
val = map(val, 0, 1023, 0, 180);
myservo1.write(val);
myservo2.write(180 - val);
} else if (hall1 == HIGH && hall2 == LOW) {
val = analogRead(potpin);
val = map(val, 0, 1023, 0, 180);
myservo1.write(180 - val);
myservo2.write(val);
}
delay(50);
}
I want to add button to make it turning opposite when the hall sensor 1 and 2 is met the conditions. What i want is like when the hall1 is HIGH and hall2 is LOW the 2 servo's STOP turning and i need to press the button to make the servo's turning to opposite until the met the hall1 is LOW and hall2 is HIGH and make the servo's stop again until i press the button again.
i make these code:
#include <Servo.h>
Servo myservo1;
Servo myservo2;
int CW = 8; // HALL SENSOR 1
int CCW = 10; // HALL SENSOR 2
int potpin = A0;
int buttonPin = 4;
int val;
int buttonState = 0;
void setup() {
pinMode(CW, INPUT);
pinMode(CCW, INPUT);
myservo1.attach(5);
myservo2.attach(6);
pinMode(buttonPin, INPUT_PULLUP);
int initialHall1 = digitalRead(CW);
int initialHall2 = digitalRead(CCW);
if (initialHall1 == LOW && initialHall2 == LOW) {
val = analogRead(potpin);
val = map(val, 0, 1023, 0, 180);
myservo1.write(val);
myservo2.write(180 - val);
}
else if (initialHall1 == HIGH && initialHall2 == HIGH) {
val = analogRead(potpin);
val = map(val, 0, 1023, 0, 180);
myservo1.write(180 - val);
myservo2.write(val);
}
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
int hall1 = digitalRead(CW);
int hall2 = digitalRead(CCW);
if (hall1 == LOW && hall2 == HIGH) {
val = analogRead(potpin);
val = map(val, 0, 1023, 0, 180);
myservo1.write(val);
myservo2.write(180 - val);
} else if (hall1 == HIGH && hall2 == LOW) {
val = analogRead(potpin);
val = map(val, 0, 1023, 0, 180);
myservo1.write(180 - val);
myservo2.write(val);
}
delay(50);
}
}
The code above is not working as i want. The servo's turning and when the hall1 is LOW and hall2 is HIGH the servo not stop and if i press the button in this hall's contidion the servo's turning the opposite but not stop when hall1 is HIGH and hall2 is LOW and i need to press the button again.
Can you guys help me to make this thing working as i want?
Thankyou.