Hi all,
I am a newbie and have been borrowing and tweaking this code to get to what I was hoping to get.
I have mostly managed to get my emulator to work but I am at a point where I am stuck and how and where I need to get additional code to do what I need in this case turn left and turn right. Forward is working without issue besides my cheapy remote getting confused at various stages ;-).
I have built a trolley using 36v hoverboard motors connected to two bike bldc controllers. The issue I am facing is that the dual linear pot on my trolley needs to be zeroed out before it will activate the motors again so will not work with the more traditional type of basic relay-based remote controls i.e. The pot cannot be preset to its speed and then started again after it is stopped without manually zeroing out the pot and defeating the objective of remote control.
So back to where I am, I have simulated the pot with Arduino Uno and this is working for what I want and I can get the trolley to go forwards, but now I would like to add steering left and right if possible to my code but I am not really sure what I need to do and where I need to insert the code.
My thinking is for the left turn I will slightly increase the speed of the right wheel and conversely for the right turn, I will slightly increase the speed of the left wheel.
This is my code thus far and hopefully, I have explained it enough with the remarks tags against most lines of code:
Thanks in advance for any help offered and again I am a newbie at this.
</>
//Version 1.0 Written by Charles Silberbauer with various inputs of borrowed code and with thanks to those that I borrowed bit and pieces to get to this point.
//Potentiometer motor control emulator for EBike controllers to control dual brushless DC motors with independent ESC's controlled with a 433MHz 4-channel button remote control
//HIGH value 255 = 5v ; Each +10 or -10 value = +0.2v or -0.2v
int RemotePin1=12; //Remote control channel 1 input
int RemotePin2=11; //Remote control channel 2 input
int RemotePin3=10; //Remote control channel 3 input
int RemotePin4=9; //Remote control channel 4 input
int buzzPin=2; //Buzzer to indicate if button is left latched HIGH or latched LOW
int PotFwd=7; //emulated dual potentiometer output for forward motion connecting to wiper
int PotLeft=6; //emulated single potentiometer output for right motor speed increase to turn left
int PotRight=5; //emulated single potentiometer output for left motor speed increase to turn right
int RemoteButtVal1; //Checks remote on/off value i.e. 1=on 0=off for remote control channel 1
int RemoteButtVal2; //Checks remote on/off value i.e. 1=on 0=off for remote control channel 2
int RemoteButtVal3; //Checks remote on/off value i.e. 1=on 0=off for remote control channel 3
int RemoteButtVal4; //Checks remote on/off value i.e. 1=on 0=off for remote control channel 4
int Speed=0; //Sets the initial speed at startup to stop or 0 value
int dt=200; //Sets value for serial monitor delay
void setup() {
// put your setup code here, to run once:
pinMode(RemotePin1,INPUT_PULLUP); //Set pin as an input pin
pinMode(RemotePin2,INPUT_PULLUP); //Set pin as an input pin
pinMode(RemotePin3,INPUT_PULLUP); //Set pin as an input pin
pinMode(RemotePin4,INPUT_PULLUP); //Set pin as an input pin
pinMode(PotFwd,OUTPUT); //Set pin as an output pin. This pin connects to both ESC wiper input
pinMode(PotLeft=5,OUTPUT); //Set pin as an output pin. This pin connects to the right ESC wiper input
pinMode(PotRight=4,OUTPUT); //Set pin as an output pin. This pin connects to the right ESC wiper input
pinMode(buzzPin,OUTPUT); //For Piezzo buzzer output
Serial.begin(9600); //Serial monitor connection baud rate
}
void loop() {
RemoteButtVal1=digitalRead(RemotePin1); //Reads remote control button 1 value for serial print output
RemoteButtVal2=digitalRead(RemotePin2); //Reads remote control button 2 value for serial print output
RemoteButtVal3=digitalRead(RemotePin2); //Reads remote control button 3 value for serial print output
RemoteButtVal4=digitalRead(RemotePin2); //Reads remote control button 4 value for serial print output
Serial.print("Remote Button 1 = ");
Serial.println(RemoteButtVal1); //Prints button 1 value as on/off i.e. 1 or 0 in serial monitor
Serial.print(", ");
Serial.print("Remote Button 2 = ");
Serial.println(RemoteButtVal2); //Prints button 2 value as on/off i.e. 1 or 0 in serial monitor
Serial.print("Remote Button 3 = ");
Serial.println(RemoteButtVal3); //Prints button 3 value as on/off i.e. 1 or 0 in serial monitor
Serial.print("Remote Button 4 = ");
Serial.println(RemoteButtVal4);//Prints button 4 value as on/off i.e. 1 or 0 in serial monitor
delay(dt);
if (RemoteButtVal1==0){
Speed=Speed+10; //Increments the current value by 10 which through testing is approximately equivalent to +0.2v
}
if (RemoteButtVal2==0){
Speed=Speed-10; //Decrements the current value by 10 which through testing is approximately equivalent to -0.2v
}
Serial.println(Speed);
if (Speed>255){
Speed=255;
digitalWrite(buzzPin,HIGH); //Buzzer to alert that HIGH button is latched i.e. > 255 maximum value
delay(dt);
digitalWrite(buzzPin,LOW);
Serial.println("Buzz HIGH"); //Print buzzer status HIGH if the maximum value of 255 is reached i.e. latched HIGH button
}
if (Speed<0){
Speed=0;
digitalWrite(buzzPin,HIGH);//Buzzer to alert that HIGH button is latched i.e. > 0 minimum value
delay(dt);
digitalWrite(buzzPin,LOW);
Serial.println("Buzz LOW"); //Print buzzer status LOW if the minimum value of 0 is reached i.e. latched LOW button
}
analogWrite(PotFwd, Speed);
}
</>