I am having the exact same problem as this forum post: Mbot Ranger Motors work in mblock but not Arduino IDE environment
For example, I take a small generated program in mBlock that turns on the ring LED for 1 second and then runs the motors for 5 seconds (see below).
It works perfectly fine if I compile and run with mBlock (that is, I copy the Arduino code it generates and then copy it to the mBlock Arduino tab and compile/upload).
But if I copy this to the Arduino IDE the lights work but not the motors.
Before you ask let me answer some questions:
-
Yes I am using the Github library from this link: https://github.com/Makeblock-official/Makeblock-Libraries
-
Yes the power is on. If I compile and upload with the mBlock program motors work perfect.
-
Yes the batteries are fine
// generated by mBlock5 for mBot Ranger
// codes make you happy#include <Arduino.h>
#include <Wire.h>
#include <SoftwareSerial.h>
#include <MeAuriga.h>MeRGBLed rgbled_0(0, 12);
MeEncoderOnBoard Encoder_1(SLOT1);
MeEncoderOnBoard Encoder_2(SLOT2);
MeLightSensor lightsensor_12(12);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 _delay(float seconds) {
if(seconds < 0.0){
seconds = 0.0;
}
long endTime = millis() + seconds * 1000;
while(millis() < endTime) _loop();
}void setup() {
rgbled_0.setpin(44);
rgbled_0.fillPixelsBak(0, 2, 1);
TCCR1A = _BV(WGM10);
TCCR1B = _BV(CS11) | _BV(WGM12);
TCCR2A = _BV(WGM21) | _BV(WGM20);
TCCR2B = _BV(CS21);
attachInterrupt(Encoder_1.getIntNum(), isr_process_encoder1, RISING);
attachInterrupt(Encoder_2.getIntNum(), isr_process_encoder2, RISING);
randomSeed((unsigned long)(lightsensor_12.read() * 123456));rgbled_0.setColor(0,255,0,0);
rgbled_0.show();
_delay(1);
rgbled_0.setColor(0,0,0,0);
rgbled_0.show();move(1, 50 / 100.0 * 255);
_delay(5);
move(1, 0);}
void _loop() {
Encoder_1.loop();
Encoder_2.loop();
}void loop() {
_loop();
}