Arduino with 2 servo and 2 hall sensor with push button

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.

void loop() {
  bool hall1 = digitalRead(CW);
  bool hall2 = digitalRead(CCW);

  if (hall1 == LOW && hall2 == HIGH) {
    while ( digitalRead(buttonPin));
    val = analogRead(potpin);
    val = map(val, 0, 1023, 0, 180);
    myservo1.write(val);
    myservo2.write(180 - val);
    delay(50);
  } else if (hall1 == HIGH && hall2 == LOW) {
    while ( digitalRead(buttonPin));
    val = analogRead(potpin);
    val = map(val, 0, 1023, 0, 180);
    myservo1.write(180 - val);
    myservo2.write(val);
    delay(50);
  }
}

You don't say which Hall sensors you have, but most have open collector NPN outputs which need pullups and switch LOW when activated. So you might try:

 pinMode(CW, INPUT_PULLUP);
 pinMode(CCW, INPUT_PULLUP);

And reverse the input logic:

if (initialHall1 == HIGH && initialHall2 == HIGH){
  val = analogRead(potpin);
  val = map(val, 0, 1023, 0, 180);
  myservo1.write(val);
  myservo2.write(180 - val);
} 

one issue is the code with the button doesn't do anything if the button is not pressed.

a common problem with things like this where sensors detect the end of travel, is that in order to restart things, the mechanism used to stop them must be defeated.

void loop() {
    ...

    if (hall1 == LOW && hall2 == HIGH) {
        if (buttonState == LOW) {
            val = analogRead(potpin);
            val = map(val, 0, 1023, 0, 180);
            myservo1.write(val);
            myservo2.write(180 - val);
        }
    }
    ...
}

Sorry for my late reply, i appreciate all the replay from you guys and will be my reference.

What i see when i touch this mini project again it turn's out that the servo is not a 360 degree servo's, it's just a modified standard servo, so it cannot detect how many degree the turning is. Basically it's just a normal motor with some torque screwed into it. I think i will search some real 360 servo online.

Thankyou.

i think common RC servos may travel 270 not 360 deg.

i know there are robotics servos that can be programmed for continuous or multi-turn operation

1 Like

Oh wow, i see the robotics servos that you linked and it's pricey. I mean more price = more feature and i like that. I think i will take my time to just searching some reference and learning.

Thankyou.

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