How to use analog pin A0 to trigger servo movement?

Hi,

I have an arduino pro mini 5V.

I have the arduino outputs connected to some servos.

I can control the servos by typing keys on the computer keyboard and sending them over serial.

Everything works fine.

I have attached working code:

#include <Servo.h>                         

Servo servo_01;                           


void setup() {                             

  Serial.begin(9600);                 
    
  servo_01.attach(10);                      
  
  
  servo_01.write(68);  
}

void loop() {
  
  if (Serial.available() > 0) {            
    
    char input_character = Serial.read();  
    switch (input_character) {
    
// camera 1    
      
    case 'a':                              
      servo_01.write(68);
      break;
             
    case 'b':                              
      servo_01.write(58);
      break;
    
    case 'c':                              
      servo_01.write(57);
      break;
    
    case 'd':                              
      servo_01.write(118);
      break;
    
    case 'e':                              
      servo_01.write(119);
      break;
      

    }
  }
}

By typing letters on the keyboard I can rotate the servo to specified positions - very simple - works fine.

I have the arduino analog pin A0 hooked up to a wireless receiver unit with a relay - 5V signal (common to arduino) n/o relay.

With a wireless transmitter I can switch the relay high or low and set A0 high or low - I have a pulldown resistor on A0 too.

I can display the status of the A0 pin and everything works fine.

Here is working code to display the A0 state:

#include <Servo.h>                         

Servo servo_01;                           


void setup() {                             

  Serial.begin(9600);      

  pinMode(A0, INPUT);      // ADDED <<<<<<<<<<<<<  
    
  servo_01.attach(10);                      
  
  
  servo_01.write(68);  
}

void loop() {
  
  if (Serial.available() > 0) {            
    
    char input_character = Serial.read();  
    switch (input_character) {
    
// camera 1    
      
    case 'a':                              
      servo_01.write(68);
      break;
             
    case 'b':                              
      servo_01.write(58);
      break;
    
    case 'c':                              
      servo_01.write(57);
      break;
    
    case 'd':                              
      servo_01.write(118);
      break;
    
    case 'e':                              
      servo_01.write(119);
      break;
      

    }
    
  }
  
Serial.println(analogRead(A0));          // ADDED <<<<<<<<<<<<<
    delay(1000);                         // ADDED <<<<<<<<<<<<<
  
}

I only added three lines.

Output looks like:

0
0
0
0
0
0
1023
1023
1023
0
0
0
0

Everything works fine.

I want to be able to rotate the servo to a specific position when A0 is high and rotate it to to a dfferent position when A0 is low.

I also want to be able to still use the keyboard.

My attempts so far have only resulted in the servo not moving and my keyboard commands not working.

Any suggestions would be greatly appreciated.

Thanks.

P.s. the third attachment shows the code that doesn't work

#include <Servo.h>                         

Servo servo_01;                           

int button_status = 0;

void setup() {                             

  Serial.begin(9600);      

  pinMode(A0, INPUT);     
    
  servo_01.attach(10);                      
  
  
  servo_01.write(68);  
}

void loop() {
  
  if (Serial.available() > 0) {            
    
    char input_character = Serial.read();  
    switch (input_character) {
    
// camera 1    
      
    case 'a':                              
      servo_01.write(68);
      break;
             
    case 'b':                              
      servo_01.write(58);
      break;
    
    case 'c':                              
      servo_01.write(57);
      break;
    
    case 'd':                              
      servo_01.write(118);
      break;
    
    case 'e':                              
      servo_01.write(119);
      break;
      

    }
    
  }
  
Serial.println(analogRead(A0));          
    delay(1000);                         
    
button_status = analogRead(A0);

if (button_status == 0);
  servo_01.write(50);
if (button_status == 1023);
  servo_01.write(100);
  
  
}

arduino_program_mike_5_1_only.ino (893 Bytes)

arduino_program_mike_5_1_only_A0_1.ino (1.05 KB)

arduino_program_mike_5_1_only_A0_1.ino (1.05 KB)

If you are just looking for "high" and "low", there's no reason to use analogRead. Just use digitalRead().

As far as your if-statements:

if (button_status == 0);
  servo_01.write(50);
if (button_status == 1023);

You don't want a semicolon there. You almost certainly meant:

if (button_status == 0)
  servo_01.write(50);
if (button_status == 1023)
  servo_01.write(100);