Hello! All the best for 2022!
Skill level: Advanced starter, more creative thinking less logical thinking regarding problem solving. I copy paste parts of code and sometimes it works. Usually not.
I hope somebody can help me. I am trying to write a sketch for controlling an RC car with separate throttle and brake pedals via one PWM signal. My skills regarding feeding an all-in-one Arduino PPM stream into the trainer port of my radio is way beyond my knowledge. So for starters I'm going to feed the PWM signal into a PWM-to-PPM converter of which I have positive experiences with. (I hooked up a servo tester this way and BAM, I added an extra control knob to my radio)
I am using affordable $10-$15 Hall sensor pedals from Aliexpress. So far I received one, the rest is in de mail on a slow boat.
The problem: The servo goes in one direction for a bit, then sweeps all the other way when the Hall sensor is connected to A0. The same behavior applies for connecting the hall sensor to A1.
Is this because the Uno expects signals on both A0 and A1? As I have only one pedal atm, I switch between A0 and A1, hoping for correct behavior of the servo.
My code:
#include <Servo.h>
#define PwmIN1 9 // Arduino pin for the pwm output1
#define Hall_SensorT A0 // Arduino pin for the Throttle Hall sensor analog input
#define Hall_SensorB A1 // Arduino pin for the Brake Hall sensor analog input
int angle1 = 0;
int Val1 = 0;
int Val2 = 0;
Servo myservo;
void setup() {
myservo.attach(PwmIN1);
pinMode(Hall_SensorT, INPUT);
pinMode(Hall_SensorB, INPUT);
Serial.begin(9600);
}
void loop() {
Val1 = analogRead(Hall_SensorT); // read the analog Throttle input
Val2 = analogRead(Hall_SensorB); // read the analog Brake input
Serial.println(Val1); // My Hall sensors measure between 180 min and 850 max values, 515 being the middle
if ((Val1 >= 516) && (Val1 <= 850))
{
angle1 = map(Val1, 180, 850, 90, 170); // from Neutral to Max Brake
}
if ((Val2 >= 180) && (Val2 <= 515))
angle1 = map(Val2, 180, 850, 90, 10); // from Neutral to Max Throttle
//Tested with code for single Hall sensor 180, 850, 90, 10 from Neutral to Max Throttle
//Tested with code for single Hall sensor 180, 850, 90, 170 from Neutral to Max Brake
myservo.write(angle1);
delay(50); // wait 5 ms for the pwm to reach the position
}
Thank you for your time.