Go Down

Topic: Arduino Mega 2560 led turns on and the light gradually gets weaker (Read 215 times) previous topic - next topic

ribkar

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:

Code: [Select]


#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
  }
}


pylon

Quote
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.
That's the only problem you have with it? The rest is working as expected?

Why do you think that the code is relevant in this regard? As the only problem you describe is a hardware problem, you should have posted a complete wiring diagram (including how you power it).

DKWatson

If you're talking about the LED that lights when power is applied to the board, you have no control over that (except with a soldering iron). Sounds to me like a leaky capacitor in the power regulation circuit. This happens when connected to the PC via USB? Can you try disconnect from PC and power via the barrel connector? Or try connecting a source to Vin and see what happens with the LED.
Live as if you were to die tomorrow. Learn as if you were to live forever. - Mahatma Gandhi

ribkar

Hi Pylon! I think the problem is with the code because I tried to use some example codes included in the Arduino Software and the LED turned on normally. And sorry but I don't have the wiring diagram anymore and don't have much time to do it again now... but thank you very much for answering!

Hey DKWatson! Thank you for answering too! This doesn't happen when connected to the PC via USB - in this case, the LED turns on normally. It happens when I connect a source in Vin.

demkat1

Some clones do not have connection between barrel connector and Vin on female header!!! They accept Vin only from barrel connector.
Give it a check...

PaulS

Code: [Select]
  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);

Print the data, then read the data. That order of operations has NEVER made sense to me.
The art of getting good answers lies in asking good questions.

ribkar

Why it doesn't make sense? Sorry I'm a begginer at this.

PaulS

Why it doesn't make sense? Sorry I'm a begginer at this.
Does THIS make sense?

Code: [Select]
   int a = 14;
   int b = 20;

   int sum = 0;

   Serial.print(a);
   Serial.print(" + ");
   Serial.print(b);
   Serial.print(" = ");
   Serial.println(sum);

   sum = a + b;


Well, that is what you are doing when you print the data first, and then get the data to print.
The art of getting good answers lies in asking good questions.

Go Up