PWM output affecting unrelated digital outputs

Hello all,

I'm creating an ag robot for help in a wineyard, its basically a fancy cart that you can control with RC controller atm.

The robot has a solenoid for steering and two driver motors in the back, I created a simple electronic differential so one wheel turns slower while you're steering on either side. Now I'm moving on to enabling additional features which will be turned on with a simple relay board. Relays switch on when the output is LOW.

The problem I'm having is this - when you increase the two motor drivers to full power (PWM outputs) the two unrelated digital outputs get turned on as well. As far as I can see in the code the outputs are not interchangeable at all so I have no clue where the values might get mixed up or is this a hardware issue?

Here is the code:

#define RCPinin 13 //gas
#define Feedback A3 //feedback
#define RCPinout1 2 //output 1
#define RCPinout2 3 //output 2

#define RCPinr13 45 //rele 1 in
#define RCPinr13out 36 //rele 1 out

#define RCPinr2 47 //rele 1 in
#define RCPinr2out 37 //rele 1 out
#define RCPinr3out 34 //rele 1 out


int input;
float output1;
float output2;
int inputr13;
int fba;
int fbacalc;
float fbakontravolan;
int voznja;
int rikverc;

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

  pinMode(RCPinr13, INPUT_PULLUP);
  pinMode(RCPinr13out, OUTPUT);
  
  pinMode(RCPinr2, INPUT_PULLUP);
  pinMode(RCPinr3out, OUTPUT);
  pinMode(RCPinr2out, OUTPUT);
  }

void loop() {
fba = analogRead(Feedback);
fbacalc = map(fba, 130, 740, 0, 100);
input = pulseIn(RCPinin, HIGH);

voznja = digitalRead(RCPinr13);
digitalWrite(RCPinr13out, voznja);


  rikverc = digitalRead(RCPinr2);
  if(rikverc == 1)
  {
    digitalWrite(RCPinr2out, LOW);
    digitalWrite(RCPinr3out, LOW);
  }else
  {
    digitalWrite(RCPinr2out, HIGH);
    digitalWrite(RCPinr3out, HIGH);
  }

    output1 = map(input, 812, 2169, 0, 100);
    output2 = map(input, 812, 2169, 0, 100);

    analogWrite(RCPinout1, output1);
    analogWrite(RCPinout2, output2);

      if(output1 >= 10 && fbacalc <= 45 && fbacalc >= 0)
      {
        fbakontravolan = map(fbacalc, 45, 0, 3, 50);
        output2 = output2 - (output2 * (fbakontravolan / 100.0));
        analogWrite(RCPinout1, output1);
        analogWrite(RCPinout2, output2);
      }

      if(output1 >= 10 && fbacalc >= 55 && fbacalc <= 100)
      {
        fbakontravolan = map(fbacalc, 55, 100, 3, 50);
        output1 = output1 - (output1 * (fbakontravolan / 100.0));
        analogWrite(RCPinout1, output1);
        analogWrite(RCPinout2, output2);
      }

Serial.print("Feedback:");
Serial.print(fbacalc);
Serial.print(",");
Serial.print("Output_1:");
Serial.println(output1);
Serial.print(",");
Serial.print("Output_2:");
Serial.println(output2);
Serial.print(",");
Serial.print("Feedback_kontra:");
Serial.println(fbakontravolan);
Serial.print("Voznja:");
Serial.println(voznja);
Serial.print(",");
Serial.print("Rikverc:");
Serial.println(rikverc);
Serial.print(",");
}

Don’t know , but you can’t output a float to an analog output - whether that messes things up I don’t know .

Help us decide by posting a schematic, hand drawn is best and easiest.

Be sure to show all components and all connections. Show all sources of power and how the things that need power are connected to them.

a7

Which robot wrote this sketch?

Pin 2 on a Uno/Nano etc. is not a PWM pin and cannot be used for analogWrite().

1 Like

Hi,

I'm using an Arduino Mega, the outputs work fine. Just cant figure out how the PWM outputs affect the 2 simple if statements at the beginning.

Hi,

The float value isn't used as an output exactly, by storing it in int space it should be transformed to int right?
Either way, the output works fine, the issue is that it affects other outputs at maximum output.

Hi,
I've uploaded the schematic, I hope its somewhat readable and understandable. Basically everything is powered by a 36V battery, that goes through one big and then smaller transformer to get appropriate voltage for a reciever (frsky archer r10 pro), jrk is for the solenoid steering - the exact controller we're using is JRK G21v3. From the reciever signals go to Arduino and then from arduino to a PWM to Voltage module and then to a BLDC motor, there are 2 modules and 2 motors. Additionally Arduino is connected to a Relay board (Low triggers the relay) which gets triggered when PWM signal to the motors reaches 100%. Otherwise everything works as intended the only issue is that signal at 100% somehow affects the relay board signal.

THX. I'm looking through the small window from the beach here, so anything substantial I can say will jave to wait.

I have read your code a bit, and there is just one thing I want to make sure you know…

The map() function will extrapolate, that is to say it will happily provide outputs outside the output range specified if you supply inputs that are outside the input range.

Please check your code and the inputs you are using to the map() function to ensure this is not a problem.

Often the use of map() will be combined with the use of the function constrain(), which will, you guessed it, constrain the value of a variable to specified limits.

The map() function has a few other quirks. If you are confident it is doing what you think, and what you want, then don't go deep into making sure it is behaving. map() is straight ahead Algebra, you can find and read the source code and may decide to do your own version of it. I have been disappointed, or worse burned, by map() too many times; I simply do not use it.

As always, YMMV.

I do see some serial printing going on. Hike up the baud rate and sprinkle some printing closer to where you might be able to observe the values of key variables and ensure that the flow of your program that they inform is correct.

a7

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