Control Arduino via RC Transmitter

I am trying to control a motor with a RadioLink A10 II RC remote hooked to an Arduino with the R12DS Reciever signal wire going to pin A3, I had it originally in pin 3. The speed control seems to be fine, however, after 5 seconds the motor automatically starts and will NOT shut off until I turn on the remote. Once I turn on the remote and open the serial monitor the motor shuts off and I can control the speed for forward and reverse great. It is only upon first boot/ or first time plugging in the Arduino usb cable to the Arduino Uno 3 and uploading the sketch.

I am struggling to find a way for the motors to only turn on when I tell them to via remotes joystick. Also, while the remote is off and the serial monitor is on, I get a very strange read-out like below.

If anyone has any idea on why this is happening and where 896 is randomly coming from with the remote not even being on would be greatly appreciated. Thanks. (I suspect it may have something to do with this line "if (ch1 < 1490){ But I still don't know why it is generating a random #")

Reverse
Previous Value: 0
New Mapped Value: 896
Reverse
Previous Value: 0
New Mapped Value: 896
Reverse
Previous Value: 0
New Mapped Value: 896
Reverse
Previous Value: 0
New Mapped Value: 896
Reverse
Previous Value: 0
New Mapped Value: 896
Reverse
Previous Value: 0
New Mapped Value: 896
Reverse
Previous Value: 0
New Mapped Value: 896
Reverse
Previous Value: 0
New Mapped Value: 896

// Arduino Uno single BTS7960 motor driver test with / remote


int R_EN=10; // digital
int R_PWM=9; // PWM
int L_EN=5; // PWM
int L_PWM=6; // PWM
int mappedValue = 0; // This variable is used to store the mapped value
int ch1;


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(A0, INPUT);

  
  
  pinMode(R_EN, OUTPUT);
  pinMode(R_PWM, OUTPUT); // direction 1
  pinMode(L_EN, OUTPUT);
  pinMode(L_PWM, OUTPUT);
  
  delay(5000);
  while(Serial.read() >= 0) ;                                             // flush the receive buffer
  digitalWrite(R_EN, HIGH);
  digitalWrite(L_EN, HIGH);
  

}

void loop() {
  // put your main code here, to run repeatedly:
  ch1= pulseIn(A0, HIGH, 25000);
  //Serial.println(ch1);                    // 1908 all the way forward  1086 all the way down
  if (ch1 >= 1490 && ch1 <= 1510) {
    Serial.println("Not moving ");
    delay(20);                                    
  }
  if (ch1 > 1510){    // 1495 - 1501 dead zone
    mappedValue = map(ch1 , 1074 , 1490 , 0 , 255); // You need to use this Mapped value according to your code
    // With Mapped value you will get value in range of 0-255
    
    analogWrite(R_PWM, mappedValue);
    analogWrite(L_PWM, 0);
    Serial.println("Forward");
    Serial.print("Previous Value: "); Serial.println(ch1);
    Serial.print("New Mapped Value: "); Serial.println(mappedValue);
    //Serial.println(ch1);
    delay(20);
  }

  if (ch1 < 1490){
    mappedValue = map(ch1 , 1490, 1066 , 0 , 255); // You need to use this Mapped value according to your code    
    // With Mapped value you will get value in range of 0-255
    analogWrite(R_PWM, 0);
    analogWrite(L_PWM, mappedValue);
    Serial.println("Reverse");
    Serial.print("Previous Value: "); Serial.println(ch1);
    Serial.print("New Mapped Value: "); Serial.println(mappedValue);
    //Serial.print("");
    //Serial.print(ch1);
    delay(20);

  
  }
  

}

Could you change the text "Previous Value: " into "Raw pulseIn Value: " ?
Lean back and take some distance from the sketch. Don't think in programming code.
Then look at the sketch and you notice that the 'ch1' is zero and there is a test for if (ch1 < 1490).

When looking at the sketch, my feeling tells me to read the reference of the pulseIn(): pulseIn() - Arduino Reference.
When there is no signal, it returns zero.

Your sketch accepts that zero, the value will be mapped, and it will be used.

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