Thanks for the quick response.
I guess I missed these examples because I was looking for something with the word ‘motor’ in it. I’ve tried all of these examples and written the program below in mBlock, but still have no track motion. When I run the examples above and look at the serial monitor I can see the encoders are working if I move the tracks manually. I’ve added Serial.println() statements into the loop so I can see that my serial input it triggering the correct case statements in the switch statement.
##Summary of what is working and what isn’t:
###Working:
- Robot works correctly when I load the firmware and control from iPhone.
- Can write programs and successfully load them on mBot Ranger to control the LEDs
- Receive encoder information in serial monitor when I run the example code
###Not Working:
- Any motor operation from programs I’ve written and loaded directly in Arduino IDE or mBlock.
#include <Arduino.h>
#include <Wire.h>
#include <SoftwareSerial.h>
#include <MeAuriga.h>
//Encoder Motor
MeEncoderOnBoard Encoder_1(SLOT1);
MeEncoderOnBoard Encoder_2(SLOT2);
void isr_process_encoder1(void)
{
if(digitalRead(Encoder_1.getPortB()) == 0){
Encoder_1.pulsePosMinus();
}else{
Encoder_1.pulsePosPlus();
}
}
void isr_process_encoder2(void)
{
if(digitalRead(Encoder_2.getPortB()) == 0){
Encoder_2.pulsePosMinus();
}else{
Encoder_2.pulsePosPlus();
}
}
void move(int direction, int speed)
{
int leftSpeed = 0;
int rightSpeed = 0;
if(direction == 1){
leftSpeed = -speed;
rightSpeed = speed;
}else if(direction == 2){
leftSpeed = speed;
rightSpeed = -speed;
}else if(direction == 3){
leftSpeed = -speed;
rightSpeed = -speed;
}else if(direction == 4){
leftSpeed = speed;
rightSpeed = speed;
}
Encoder_1.setTarPWM(leftSpeed);
Encoder_2.setTarPWM(rightSpeed);
}
void moveDegrees(int direction,long degrees, int speed_temp)
{
speed_temp = abs(speed_temp);
if(direction == 1)
{
Encoder_1.move(-degrees,(float)speed_temp);
Encoder_2.move(degrees,(float)speed_temp);
}
else if(direction == 2)
{
Encoder_1.move(degrees,(float)speed_temp);
Encoder_2.move(-degrees,(float)speed_temp);
}
else if(direction == 3)
{
Encoder_1.move(-degrees,(float)speed_temp);
Encoder_2.move(-degrees,(float)speed_temp);
}
else if(direction == 4)
{
Encoder_1.move(degrees,(float)speed_temp);
Encoder_2.move(degrees,(float)speed_temp);
}
}
double angle_rad = PI/180.0;
double angle_deg = 180.0/PI;
MeRGBLed rgbled_0(0, 12);
void setup(){
//Set Pwm 8KHz
TCCR1A = _BV(WGM10);
TCCR1B = _BV(CS11) | _BV(WGM12);
TCCR2A = _BV(WGM21) | _BV(WGM20);
TCCR2B = _BV(CS21);
rgbled_0.setpin(44);
attachInterrupt(Encoder_2.getIntNum(), isr_process_encoder2, RISING);
Encoder_2.setPulse(9);
Encoder_2.setRatio(39.267);
Encoder_2.setPosPid(1.8,0,1.2);
Encoder_2.setSpeedPid(0.18,0,0);
rgbled_0.setColor(0,0,0,0);
rgbled_0.show();
}
void loop(){
rgbled_0.setColor(0,0,20,0);
rgbled_0.show();
// Expect the motor to run here. The LEDs have turned to green.
Encoder_2.runSpeed(100);
_delay(1);
rgbled_0.setColor(0,20,0,0);
rgbled_0.show();
// Expect the motor to stop here. The LEDs have turned to red.
Encoder_2.runSpeed(0);
_delay(1);
_loop();
}
void _delay(float seconds){
long endTime = millis() + seconds * 1000;
while(millis() < endTime)_loop();
}
void _loop(){
Encoder_2.loop();
}