Hi,
I'm gonna make a quick presentation of what I'm talking about and what's my problem.
Recently I made a self balancing robot and I have little problem with coding, my problem
is that i get data from a Gyro sensor (once axe) and with that I try to balance 2 motors
with a L298N H.B.
The code that I'm using and I found it really easy to use and not making mistakes is from
a person from youtube.
Code that I'm using: (That's a part of the code, for Gyro just use your imagination that I got a
single variable and a single axes)
#define ENA 5 //enable A on pin 5 (needs to be a pwm pin)
#define ENB 3 //enable B on pin 3 (needs to be a pwm pin)
#define IN1 2 //IN1 on pin 2 conrtols one side of bridge A
#define IN2 4 //IN2 on pin 4 controls other side of A
#define IN3 6 //IN3 on pin 6 conrtols one side of bridge B
#define IN4 7 //IN4 on pin 7 controls other side of B
void setup()
{
//set all of the outputs
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
}
void loop()
{
// (Imagine a Variable that I'm using is a single axes that I use to balance my robot)
// Let's say we have ax_y
Serial.println(ax_y,2); // We got values from n...-15 to 15...n "which represent angle in real life"
if (ax_y > 7){
motorA(0,0);
motorB(0,0);
delay(1);
motorA (1,70);
motorB (1,70);
}
else if (ax_y < -7){
motorA(0,0);
motorB(0,0);
delay(1);
motorA (2,70);
motorB (2,70);
}
else if (ax_y > -5 && ax_y < 5){
motorA(0,0);
motorB(0,0);
delay(1);
}
delay(147);
}
//****************** Motor A control *******************
void motorA(int mode, int percent)
{
//change the percentage range of 0 -> 100 into the PWM
//range of 0 -> 255 using the map function
int duty = map(percent, 0, 100, 0, 255);
switch(mode)
{
case 0: //disable/coast
digitalWrite(ENA, LOW); //set enable low to disable A
break;
case 1: //turn clockwise
//setting IN1 high connects motor lead 1 to +voltage
digitalWrite(IN1, HIGH);
//setting IN2 low connects motor lead 2 to ground
digitalWrite(IN2, LOW);
//use pwm to control motor speed through enable pin
analogWrite(ENA, duty);
break;
case 2: //turn counter-clockwise
//setting IN1 low connects motor lead 1 to ground
digitalWrite(IN1, LOW);
//setting IN2 high connects motor lead 2 to +voltage
digitalWrite(IN2, HIGH);
//use pwm to control motor speed through enable pin
analogWrite(ENA, duty);
break;
case 3: //brake motor
//setting IN1 low connects motor lead 1 to ground
digitalWrite(IN1, LOW);
//setting IN2 high connects motor lead 2 to ground
digitalWrite(IN2, LOW);
//use pwm to control motor braking power
//through enable pin
analogWrite(ENA, duty);
break;
}
}
//****************** Motor B control *******************
void motorB(int mode, int percent)
{
//change the percentage range of 0 -> 100 into the PWM
//range of 0 -> 255 using the map function
int duty = map(percent, 0, 100, 0, 255);
switch(mode)
{
case 0: //disable/coast
digitalWrite(ENB, LOW); //set enable low to disable B
break;
case 1: //turn clockwise
//setting IN3 high connects motor lead 1 to +voltage
digitalWrite(IN3, HIGH);
//setting IN4 low connects motor lead 2 to ground
digitalWrite(IN4, LOW);
//use pwm to control motor speed through enable pin
analogWrite(ENB, duty);
break;
case 2: //turn counter-clockwise
//setting IN3 low connects motor lead 1 to ground
digitalWrite(IN3, LOW);
//setting IN4 high connects motor lead 2 to +voltage
digitalWrite(IN4, HIGH);
//use pwm to control motor speed through enable pin
analogWrite(ENB, duty);
break;
case 3: //brake motor
//setting IN3 low connects motor lead 1 to ground
digitalWrite(IN3, LOW);
//setting IN4 high connects motor lead 2 to ground
digitalWrite(IN4, LOW);
//use pwm to control motor braking power
//through enable pin
analogWrite(ENB, duty);
break;
}
}
//**********************************************************
I tried many if, else if, else statments on my own but I always got a messed wheels they turn
not as they should. I even tried to make a 3rd switch to combine the data in a easier coding,
I tried any kid of "If" I could think.
I thought about using maping to turn the wheels from 0 to 100% speed while the angle rise
but never tried it, the problem is that the motors do not respond as exactly what i write in the code.
and do not talk about the wires or circuit everything is connected 100% fine.
they rotate in the same direction or same opposite direction, I done already tests
Any idea how else can I use the values from the "y" axis and combine it with the H.B motors?