Canada
Asked — Edited

Question With Controlling And Driving 4 Independent Dc Motors

Hello,

I'm using a JeenNode board and H-bridge L298 to drive 4 independent DC motors. The motors are connected to mecanum wheels. Also, I am using 4 pushbuttons to control the movement of my robot. I guess the problem is in my code which doesn't allow me to control the motors probably. When the all 4 pushbuttons are connected and one of them is pressed, the motors move as direction of the pressed pushbutton perfectly. However, the rest of the pushbuttons don't work as good as the pressed one. Also, I am reading from Analog pin a digital value using this function digiRead2() because I need 8 digital values to determine the direction for the motors and the jeenode has only 4 digital pins, could it be another problem? Could you please take a look at my code and help me to improve it. Thanks in advance

#include

Port port1 = Port(1); Port port2 = Port(2); Port port3 = Port(3); Port port4 = Port(4);

void setup() {

Serial.begin(115200);

port1.mode(OUTPUT); port2.mode(OUTPUT); port3.mode(OUTPUT); port4.mode(OUTPUT); }

void loop() {

int pushbuttonUP; int pushbuttonDWN; int pushbuttonLEFT; int pushbuttonRIGHT;

pushbuttonUP = port1.digiRead(); pushbuttonDWN = port1.digiRead2(); pushbuttonLEFT = port2.digiRead(); pushbuttonRIGHT = port2.digiRead2();

Serial.println(pushbuttonUP); Serial.println(pushbuttonDWN);

if ((pushbuttonUP == HIGH) && (pushbuttonLEFT && pushbuttonDWN && pushbuttonRIGHT)!= HIGH){ // drive the motor FWD port3.digiWrite(LOW); // L1 for front left motor port3.digiWrite2(HIGH); // L2 for front left motor port4.digiWrite(LOW); // L3 for front right motor port4.digiWrite2(HIGH); // L4 for front right motor }else{
port3.digiWrite(LOW); // Brake all motors port3.digiWrite2(LOW); port4.digiWrite(LOW); port4.digiWrite2(LOW); }

if ((pushbuttonDWN == HIGH) && (pushbuttonUP && pushbuttonDWN && pushbuttonRIGHT)!= HIGH){ // drive the motor BKW port3.digiWrite(HIGH); // L1 F-left motor port3.digiWrite2(LOW); // L2 F-left motor port4.digiWrite(HIGH); // L3 R-right motor port4.digiWrite2(LOW); // L4 R-right motor }else{ port3.digiWrite(LOW); // Brake All motors port3.digiWrite2(LOW); port4.digiWrite(LOW); port4.digiWrite2(LOW); }

if ((pushbuttonLEFT == HIGH) && (pushbuttonUP && pushbuttonDWN && pushbuttonRIGHT) !=HIGH){ // drive the motor LEFT port3.digiWrite(LOW); // L1 F-left motor port3.digiWrite2(HIGH); // L2 F-left motor port4.digiWrite(HIGH); // L3 R-right motor port4.digiWrite2(LOW); // L4 R-right motor }else{ port3.digiWrite(LOW); // Brake All motors port3.digiWrite2(LOW); port4.digiWrite(LOW); port4.digiWrite2(LOW); }

}


ARC Pro

Upgrade to ARC Pro

ARC Pro will give you immediate updates and new features needed to unleash your robot's potential!