Need help to adjust code

Hey, I watched a video explaining how to make an RC-controlled Arduino robot, in that video the person is using a flysky radio transmitter but I have a microzone mc6c transmitter I think the code needs to be changed for it to work with the transmitter I have.

int enA = 5;
int in1 = 2;
int in2 = 3;
//M2
int enB = 6;
int in3 = 7;
int in4 = 8;

int receiver_pins[] = {A0, A1, A2, A3, A4, A5};
int receiver_values[] = {0, 0, 0, 0, 0, 0};
int res_min = 1100;
int res_max = 1900;

int working_range = 255;// motor driver range

boolean prt = true;

int mode = 0;
//-1 - transmeter not connected or out of range
//0- trans connected and ready
//1 - low speed
//2 = high speed mode

void setup() {
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);

  pinMode(enA, OUTPUT);
  pinMode(enB,  OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);

  Serial.begin(115200);
  //Staring delay with LED
  setLED(1);
  delay(300);
  setLED(1);
  delay(300);
  setLED(2);
  delay(300);
  setLED(3);
  delay(300);
  setLED(0);


}

void loop() {
  receive();
  setModeLED();

  int m1 = 0;
  int m2 = 0;

  int rot = receiver_values[0];

  if (mode == 1) {
    m1 = receiver_values[1] / 2 + (rot );
    m2 = receiver_values[1] / 2 - (rot );

  } else if (mode == 2) {

    m1 = receiver_values[1] + rot / 2;
    m2 = receiver_values[1] - rot / 2
         ;
  }

  mpower(1,  m1);
  mpower(2,  m2);
}
int rp = 0;
void receive() {

  receiver_values[rp] = map(pulseIn (receiver_pins[rp], HIGH), res_min, res_max, -1 * working_range, working_range);
  rp++;
  if (rp == 6) {
    rp = 0;
  }
  boolean activevalues = true;
  for (int i = 0; i < 6; i++) {
    if (prt) {
      Serial.print("CH");
      Serial.print(i);
      Serial.print(" : ");
      Serial.print(receiver_values[i]);
      Serial.print(",\t");
    }
    if (receiver_values[i] < -500) {
      activevalues = false;
    }
  }
  mode = 0;
  if (!activevalues) {
    mode = -1;
  } else if (receiver_values[4] > -100) {
    mode = 2;
  } else if (receiver_values[5] > -100) {
    mode = 1;
  }
  if (prt) {
    Serial.println("");
  }

}

void setModeLED() {
  if (mode == -1) {
    setLED(-0);
  } else if (mode == 0) {
    setLED(1);
  } else if (mode == 1) {
    setLED(2);
  } else if (mode == 2) {
    setLED(3);
  }
}
void setLED(int led) {
  for (int i = 1; i < 4; i++) {
    if (led == i) {
      digitalWrite(10 + i, LOW);
    } else {
      digitalWrite(10 + i, HIGH);
    }
  }
}
void mpower(int motor,  int spd) {
  int rotation = 0;
  if (spd > 0) {
    rotation = 1;
  } else if (spd < 0) {
    rotation = -1;
    spd *= -1;
  }
  if (spd > 255) {
    spd = 255;
  }
  int pwm;
  int pA;
  int pB;
  if (motor == 1) {
    pwm = enA;
    pA = in1;
    pB = in2;
  } else if (motor == 2) {
    pwm = enB;
    pA = in3;
    pB = in4;
  } else {
    return;
  }

  if (rotation == 0) {
    digitalWrite(pA, LOW);
    digitalWrite(pB, LOW);
  } else if (rotation == 1) {
    digitalWrite(pA, HIGH);
    digitalWrite(pB, LOW);
  } else if (rotation == -1) {
    digitalWrite(pA, LOW);
    digitalWrite(pB, HIGH);
  }
  analogWrite(pwm, spd);

}

this is the code which was in the video

What makes you think the sketch has to be 'adjusted'? The pulse streams sent from the RC receiver to the servos is pretty standard. When you connect the servo outputs of the receiver to A0-A5, do they not work?

It looks like A0 is steering and A1 is throttle. A4 and A5 seem to be some mode switches.

nope it didn't work so I thought the sketch should be adjusted

Well there can be various hardware-things that can cause not working.
You have just talked about your microzone mc6c transmitter.

You must have a RC-Receiver too. The microcontroller itself is not able to receive the
2,4 GHz radio-signal.

How does your wiring look like?
Make a hand-drawn schematic how the RC-receiver is connected to your microcontroller.
Does the receiver work on 5V Does your microcontroller work on 5V too or is it a 3.3V-one?

As you can see from all these questions you have to provide much more detail-information
to enable that others can help you

Post a link to the video#
post pictures of your transmitter and your RC-Receiver

best regards Stefan

As already said it's not the transmitter that's important it's the RC receiver. So what receiver do you have and does it have standard servo outputs?

When you run the code what does it do?

Steve

Oh sorry. I know this shouldn't be used as an excuse but I am a beginner
link to the video

schematic diagram

I'm using the receiver which came with the transmitter it a Microzone MC6RE 6 channel receiver 2.4GHz
51V5Vb5wzOL.AC_SX425

pictures some the transmitter and receiver.

Thanks for all the support.

OK seems to be standard receiver creating standard servopulse-signal.

So upload a small testprogram that does nothing more than printing the values from each channel to the serial monitor to check if the wiring is OK.

Feeding 5V into Vin is not sufficient.
Do you have a digital multimeter?
Disconnect the L298-motor-driveborad from everything but your powersupply.

Measure the voltage between 5V and GND. Is this board able to create 5V from the 12V?
I highly doubt it.
If it does create 5V connecting 5V to the Vin-pin of the arduino is not sufficient
Vin expects 7,5V to 9V. With 5V connected to the Vin-Pin the arduino won't run.

If you have measured the voltage on the 5V terminal of the L298-board and there are 5V
you can connect it to the 5V-pin of the arduino.

If the 5V-terminal of the L298-board expects to get 5V feeded in you could connect the 12V to the socket where you can plug-in a typical power-supply-plug
And then connect the 5V-pin from the Arduino with the 5V terminal.

To clear all this please post a datasheet and usermanual of the L298-board.
or at least a link where you have bought the L298-board

In the microcontroller-world nothing is superstandardized like USB-devices
You have to take care of a lot more things than just "does the plug fit into the socket?"

And tinkering around trying this trying that without exactly knowing what you do can result in the "magic smoke" and damaged devices

best regards Stefan

hmm I dont know how to just yet, I'm going through some yt vids I might figure it out eventually.

no :neutral_face:

Well I bought it from a local store dont know if this helps

OK. from this picture
image
I can see there is just a pure L298 on the board. No additional voltage-regulator that creates 5V.

This means to supply the Arduino Uno connect the 12V battery with the supply-socket
See the green frame in the picture.
image

To avoid overheating / overloading the Arduino voltage-regulator you should use a 5V DC-DC-stepdown-voltage-regulator. With 1A current.

The RCreceiver wil pull quite some current and the L298 board a little bit too.
If you start tinkering with microcontrollers a digital multimeter is a must.
If you can afford to buy one this is a good investion. If not find somebody who can lend it to you.
I mean something like this
https://www.daraz.lk/products/lm2596-lm2596s-dc-dc-3-40v-adjustable-step-down-power-supply-module-voltage-regulator-3a-i102752572-s1009384809.html?spm=a2a0e.searchlistcategory.list.57.1e9a50fd1fS74x&search=1

To adjust the voltage to 5V you already need a digital multimeter
something very cheap like this will do

Or you can use a dc-dc-regulator with fixed voltage 5V.

If you can measure the current that the receiver is pulling you might be able to connect the receiver to the Arduino's 5V pin. But if the receiver pulls too much current this would overheat the Arduino-onboard-voltage-regulator.

If you are a real beginner in programming. Adapting this code of the receiver is quite ambitious.
You should learn the basics with much smaller programs.

You will get a lot of support in the arduino forum if the other users can see that you put own effort into learning it. The questions can be as easy and basic as ever possible as long as the other users see you put some own effort into really learning you will always get support.

So as a first step: have you already understood what the purpose of function setup and function loop is?

It doesn't matter if you know it or not. This is just for clarification what is your knowledge-level.
Learning will become easy for you if the explanations are adjusted to your knowledge-level.

best regards Stefan

OK, I ordered the multimeter and the 5V DC-DC-stepdown-voltage-regulator. I will work on the basics until these are delivered

Yes, my first project was the obstacle avoiding car then I made the Bluetooth controlled car I have worked with some LEDs before and now I'm trying to make this. So I guess this is a bit ambitious.

Well if you have managed to build these two cars. You have some experience.

Once the measuring of the pulse-length of the servo-signals is working the rest is similar to the two other cars.

Did you have manuals that showed all detail-steps including the wiring for the two other cars?

As described above the wiring has to be corrected. In the meantime you could learn how servosignals work.

https://learn.sparkfun.com/tutorials/hobby-servo-tutorial/all

EDIT: If you have a second microcontroller you could use the second one as a receiver-simulator
Arduino-A ----->------Servo-signal-creation-------->----------Arduino B servo-signal receiving / analysing

if you want to do that you must connect the GNDs of both microcontrollers

best regards Stefan

Yes, the video links were in the previous reply and the circuit diagrams->


Thanks, I'll check it out for sure, and Stefan any tips to learn coding?

Yes I can see all the wiring is sure through using a plug-in motor-shield and well documented wiring.

Me personally I think this tutorial is a very good starting point.

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

But people have different learning-styles

  • learning by doing / tinkering
  • write two or three lines of code asking somebody for some details get answers go on
  • watching a video
  • listening to a teacher
    etc.

So if you can tell what your preferred learning-style is I can suggest something that fits better to this style.

Another way is to start with a small demo-code and if something is unclear and a question arises to ask this question here in the forum. As long as the others can see that you put own effort into really learning it you will get support and answers.

best regards Stefan

1 Like

Hi,
I made the adjustments and it kinda worked, the left motors connected to output 1 and output 2 won't spin but the right motors connected to output 3 and 4 do, it was just driving in circles now so I just connected one of the left and right motors to output 3 and 4. Now it can forward and backward but it obviously can't turn,so will switching out the l298-board fix this?

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