Arduino Mega code for Analog Read Pushbuttons

Greetings.

I am working on an Arduino Rotator Controller based on K3NG code in the GitHub repository.

I have almost everything operational. ALMOST.

The problem is that the code was orignally written around using OPEN / CLOSED switches and not Analog values. I am using this display with buttons.

Instead of the buttons going to the Digital pins, it connects to a resistor ladder voltage divider network. When you press different buttons, it goes out A0 and has varying voltages.

The code is expecting either to select a Digital pin, or an Analog one where the voltage is either 0VDC or +5vdc.

The keypad buttons on the display output:

RIGHT = 50
UP = 250
DOWN = 450
LEFT = 650
SELECT = 850

This is what K3NG originally had for button input:

/ K3NG:

if (button_cw) {
    pinModeEnhanced(button_cw, INPUT);
    digitalWriteEnhanced(button_cw, HIGH);
  }
  if (button_ccw) {
    pinModeEnhanced(button_ccw, INPUT);
    digitalWriteEnhanced(button_ccw, HIGH);
  }

This is the part of the K3NG code that is affected:

/* azimuth pins --------------------- (use just the azimuth pins for an azimuth-only rotator) */

#define rotate_cw 6              // goes high to activate rotator R (CW) rotation - pin 1 on Yaesu connector
#define rotate_ccw 7             // goes high to activate rotator L (CCW) rotation - pin 2 on Yaesu connector
#define rotate_cw_ccw  9         // goes high for both CW and CCW rotation
#define rotate_cw_pwm 0          // optional - PWM CW output - set to 0 to disable (must be PWM capable pin)
#define rotate_ccw_pwm 0         // optional - PWM CCW output - set to 0 to disable (must be PWM capable pin)
#define rotate_cw_ccw_pwm 0      // optional - PWM on CW and CCW output - set to 0 to disable (must be PWM capable pin)
#define rotate_cw_freq 0         // optional - CW variable frequency output
#define rotate_ccw_freq 0        // optional - CCW variable frequency output
**#define button_cw 2              // normally open button to ground for manual CW rotation (schematic pin: A2)**
**#define button_ccw 3             // normally open button to ground for manual CCW rotation (schematic pin: A3)**
#define serial_led 0             // LED blinks when command is received on serial port (set to 0 to disable)
#define rotator_analog_az A5     // reads analog azimuth voltage from rotator - pin 4 on Yaesu connector
#define azimuth_speed_voltage 0  // optional - PWM output for speed control voltage feed into rotator (on continually unlike rotate_cw_pwm and rotate_ccw_pwm)
#define overlap_led 0            // line goes active when azimuth rotator is in overlap (> 360 rotators)
#define brake_az 8               // goes high to disengage azimuth brake (set to 0 to disable)
#define az_speed_pot 0           // connect to wiper of 1K to 10K potentiometer for speed control (set to 0 to disable)
#define az_preset_pot 0          // connect to wiper of 1K to 10K potentiometer for preset control (set to 0 to disable)
#define preset_start_button 0    // connect to momentary switch (ground on button press) for preset start (set to 0 to disable or for preset automatic start)
#define button_stop 0            // connect to momentary switch (ground on button press) for preset stop (set to 0 to disable or for preset automatic start)
#define rotation_indication_pin 0
#define blink_led 13
#define az_stepper_motor_pulse 0
#define az_rotation_stall_detected 0

I have tried modifying the example code from the manufacturer booklet for the LCD, but I keep getting errors.

void setup() {
  pinMode(4, OUTPUT) //make sure you have set your pins as inputs or outputs
  pinMode(A0, INPUT)
}

void loop() {
  if(analogRead(A0) > 512) { //if the analog value of A0 is greater than 512 then
    digitalWrite(4, HIGH); //write digital pin 4 high
  }
  else {
    digitalWrite(4, LOW); //if it isn't higher than 512, make pin 4 low
  }
}

All I need for now is the RIGHT / CW and LEFT / CCW commands to work.

Thanks in Advance.

Post a link to that booklet.

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