Good afternoon,
I've been having a problem with the code that I'm using for my autonomous guiding vehicle.When I turn on the Arduino, the LED that indicates that it's on gets weaker until the light gets so low that you almost can't see that it's on. The code that I'm using is this one:
#define md 9 // Right motor
#define me 10 // Left motor
#define pe A0 // Left motor potenciometer
#define pd A1 // Right motor potenciometer
#define s1 A2 // Left sensor
#define s2 A3 // Central left sensor
#define s3 A4 // Central right sensor
#define s4 A5 // Right sensor
#define pwm_e90 255 // PWM value to do 90 degree curve to the right
#define pwm_d90 255 // PWM value to do 90 degree curve to the left
#define cte 4 // Constant to divide the PWM by to slow the speed of the motor
byte pwm_d = 0, pwm_e = 0; // PWM variables
unsigned short int per = 0, pdr = 0, s1r = 0, s2r = 0, s3r = 0, s4r = 0; // Variables of the sensors and potenciometers
void setup() {
TCCR2B = TCCR2B & 0b11111000 | 0x01; // Timer configuration so the PWM doesn't make noise, changes the frequency to 31kHz
pinMode(me, OUTPUT);
pinMode(md, OUTPUT);
pinMode(pe, INPUT);
pinMode(pd, INPUT);
pinMode(s1, INPUT);
pinMode(s2, INPUT);
pinMode(s3, INPUT);
pinMode(s4, INPUT);
digitalWrite(me, LOW);
digitalWrite(md, LOW);
Serial.begin(9600); // Initializes serial communication
}
void loop() {
Serial.println(s1r); // Prints the s1 value
Serial.println(s2r); // s2 value
Serial.println(s3r); // s3 value
Serial.println(s4r); // s4 value
per = analogRead(pe);
pdr = analogRead(pd);
s1r = analogRead(s1);
s2r = analogRead(s2);
s3r = analogRead(s3);
s4r = analogRead(s4);
pwm_e = map(per, 0, 1023, 0, 255);
pwm_d = map(pdr, 0, 1023, 0, 255);
if (s1r < 512 and s2r > 512 and s3r > 512 and s4r < 512) {
analogWrite(me, pwm_e);
analogWrite(md, pwm_d);
// To the front
}
else if (s1r < 512 and s2r > 512 and s3r < 512 and s4r < 512) {
analogWrite(me, 0);
analogWrite(md, pwm_d);
// Left
}
else if (s1r < 512 and s2r < 512 and s3r > 512 and s4r < 512) {
analogWrite(me, pwm_e);
analogWrite(md, 0);
// Right
}
else if (s1r > 512 and s2r < 512 and s3r < 512 and s4r > 512) {
analogWrite(me, pwm_e / cte);
analogWrite(md, pwm_d / cte);
// To the front slowly
}
else if (s1r > 512 and s2r < 512 and s3r < 512 and s4r < 512) {
analogWrite(me, pwm_e / cte);
analogWrite(md, pwm_d);
// Correct direction to the left
}
else if (s1r < 512 and s2r < 512 and s3r < 512 and s4r > 512) {
analogWrite(me, pwm_e);
analogWrite(md, pwm_d / cte);
// Correct direction to the right
}
else if (s1r < 512 and s2r > 512 and s3r > 512 and s4r > 512) {
analogWrite(me, pwm_e90);
analogWrite(md, 0);
// 90 degree to the right
}
else if (s1r > 512 and s2r > 512 and s3r > 512 and s4r < 512) {
analogWrite(me, 0);
analogWrite(md, pwm_d90);
// 90 degree to the left
}
else if (s1r < 512 and s2r < 512 and s3r > 512 and s4r > 512) {
analogWrite(me, pwm_e);
analogWrite(md, 0);
// Right
}
else if (s1r > 512 and s2r > 512 and s3r < 512 and s4r < 512) {
analogWrite(me, 0);
analogWrite(md, pwm_d);
// Left
}
}