Go Down

Topic: MKR1010 CHRG LED keep blinking and not stopping (Read 184 times) previous topic - next topic

vitsensei

When I use an external power source (power supply, 7V, 2.5A), the CHRG LED kept blinking red continuously (not stopping), this does not happen when I use USB and power it from the laptop.

The thing is, I wouldn't care anyway, but I was trying to read encoder data from 3 different DC motors (using interrupt pins), and the code only works correctly when a USB cable is plugged in. So I'm trying to debug the system and see where it went wrong.

I'm guessing CHRG stands for Charge, and it means the Arduino is trying to charge the battery (but there's no battery!). Would doing this somehow interfere with the code (for example, disable interrupt, not running the sketch at all, reset the Arduino...)?

Here's my code base:

Code: [Select]

///////////////////////////// Library /////////////////////////////
#include <Encoder.h>
#include <PID_v1.h>

///////////////////////////// DC motors /////////////////////////////
const int int1 = A0; // motor 0
const int int2 = A1;
const int enA = 2;

const int int3 = A2; // motor 1
const int int4 = A3;
const int enB = 3;

const int int5 = A4; // motor 2
const int int6 = A5;
const int enC = 4;

/////////////////////////////
Encoder enc0(0, 1);
Encoder enc1(6, 7);
Encoder enc2(8, 9);

float countPerRotation = 2797.0;

/////////////////////////// PID ////////////////////////////
struct motorPID_parameter {
  double Setpoint;
  double Input;
  double Output;
};

float Kp = 22.0;
float Ki = 70.0;
float Kd = 0.0;

struct motorPID_parameter motor0;
struct motorPID_parameter motor1;
struct motorPID_parameter motor2;

float pwm0 = 0;
float pwm1 = 0;
float pwm2 = 0;

PID motorPID0(&motor0.Input, &motor0.Output, &motor0.Setpoint, Kp, Ki, Kd, DIRECT);
PID motorPID1(&motor1.Input, &motor1.Output, &motor1.Setpoint, Kp, Ki, Kd, DIRECT);
PID motorPID2(&motor2.Input, &motor2.Output, &motor2.Setpoint, Kp, Ki, Kd, DIRECT);

////////////////// For keeping track of time ////////////////
unsigned long start0 = 0;
unsigned int elapsedTime0 = 0;

int time_frame0 = 10;

///////////////// For calculate displacement ///////////////
long int displacement0 = 0;
long int posPrevious0 = 0;

long int displacement1 = 0;
long int posPrevious1 = 0;

long int displacement2 = 0;
long int posPrevious2 = 0;

float speed0 = 0;
float speed1 = 0;
float speed2 = 0;

void setup() {
  // put your setup code here, to run once:
  Serial1.begin(9600);

  pinMode(int1, OUTPUT);
  pinMode(int2, OUTPUT);
  pinMode(enA, OUTPUT);

  pinMode(int3, OUTPUT);
  pinMode(int4, OUTPUT);
  pinMode(enB, OUTPUT);

  pinMode(int5, OUTPUT);
  pinMode(int6, OUTPUT);
  pinMode(enC, OUTPUT);

  analogWrite(enA, 0);
  analogWrite(enB, 0);
  analogWrite(enC, 0);
 
  // PID1 initialize
  motorPID0.SetMode(AUTOMATIC);
  motorPID0.SetTunings(Kp, Ki, Kd);
  motorPID0.SetOutputLimits(-255,255);
  motorPID0.SetSampleTime(time_frame0);
 
  motorPID1.SetMode(AUTOMATIC);
  motorPID1.SetTunings(Kp, Ki, Kd);
  motorPID1.SetOutputLimits(-255,255);
  motorPID1.SetSampleTime(time_frame0);

  motorPID2.SetMode(AUTOMATIC);
  motorPID2.SetTunings(Kp, Ki, Kd);
  motorPID2.SetOutputLimits(-255,255);
  motorPID2.SetSampleTime(time_frame0);

  motor0.Setpoint = 30;
  motor1.Setpoint = -30;
  motor2.Setpoint = 60;

  start0 = micros();
}

void loop() {
  // put your main code here, to run repeatedly:
  elapsedTime0 = micros() - start0;
 
  if (elapsedTime0 > time_frame0*1000) {
  start0 = micros();
    Serial1.write(enc0.read());

    displacement0 = (enc0.read() - posPrevious0);
   
    posPrevious0 = enc0.read();
    speed0 = (displacement0 / countPerRotation) / (elapsedTime0 / 1000000.0) * 60; // rpm
   
  displacement1 = (enc1.read() - posPrevious1);
  posPrevious1 = enc1.read();
  speed1 = (displacement1 / countPerRotation) / (elapsedTime0 / 1000000.0) * 60; // rpm

    displacement2 = (enc2.read() - posPrevious2);
    posPrevious2 = enc2.read();
    speed2 = (displacement2 / countPerRotation) / (elapsedTime0 / 1000000.0) * 60; // rpm
 
    motor0.Input = speed0;
    motorPID0.Compute();
    pwm0 = motor0.Output;

  motor1.Input = speed1;
    motorPID1.Compute();
    pwm1 = motor1.Output;

    motor2.Input = speed2;
    motorPID2.Compute();
    pwm2 = motor2.Output;

    if (pwm0 > 0)  {
        digitalWrite(int1, 1);
        digitalWrite(int2, 0);
        analogWrite(enA, pwm0);

      } else {
        digitalWrite(int1, 0);
        digitalWrite(int2, 1);
        analogWrite(enA, -pwm0);
    }

  if (pwm1 > 0) {
        digitalWrite(int3, 1);
        digitalWrite(int4, 0);
        analogWrite(enB, pwm1);

      } else {
        digitalWrite(int3, 0);
        digitalWrite(int4, 1);
        analogWrite(enB, -pwm1);
    }

    if (pwm2 > 0)  {
        digitalWrite(int5, 1);
        digitalWrite(int6, 0);
        analogWrite(enC, pwm2);

      } else {
        digitalWrite(int5, 0);
        digitalWrite(int6, 1);
        analogWrite(enC, -pwm2);
    }
   
  }
}

Go Up