hi,loldrup.there is my code for double mode.you can update the latest makeblock’s library( https://github.com/Makeblock-official/Makeblock-Library) on Github first.
/*************************************************************************
* File Name : Makeblock IR_ULTRASONIC_Controller.ino
* Author : Jasen
* Updated : Jasen
* Version : V1.0.0
* Date : 11/6/2013
* Description : Demo code for Makeblock Starter Robot kit,two motors
connect on the M1 and M2 port of baseshield, The IR receiver module
connect on port 6.
The four black button on the IR controller is used to control the direction
of robot, the number button on the IR controller is for changing the speed of the robot.
button 1 is for setting the speed to the slowest,button 9 is for setting the speed to fastest.
* License : CC-BY-SA 3.0
* Copyright (C) 2013 Maker Works Technology Co., Ltd. All right reserved.
* http://www.makeblock.cc/
**************************************************************************/
#include "Makeblock.h"
#include < Arduino.h>
#include < SoftwareSerial.h>
#include < Wire.h>
MeDCMotor MotorL(M1);
MeDCMotor MotorR(M2);
MeInfraredReceiver infraredReceiverDecode(PORT_6);
MeUltrasonicSensor UltrasonicSensor(PORT_3);
int moveSpeed = 190;
boolean rightflag, autoflag = false;
int distance = 0;
int minSpeed = 55;
int factor = 23;
int randnum = 0;
void setup()
{
infraredReceiverDecode.begin();
randomSeed(analogRead(0));
Serial.begin(9600);
Serial.println("start");
}
void loop()
{
if(infraredReceiverDecode.buttonState() == 1)
{
int ir = infraredReceiverDecode.read();
switch(ir)
{
case IR_BUTTON_PLUS:
Forward();
break;
case IR_BUTTON_MINUS:
Backward();
break;
case IR_BUTTON_NEXT:
TurnRight();
break;
case IR_BUTTON_PREVIOUS:
TurnLeft();
break;
case IR_BUTTON_9:
ChangeSpeed(factor*9+minSpeed);
break;
case IR_BUTTON_8:
ChangeSpeed(factor*8+minSpeed);
break;
case IR_BUTTON_7:
ChangeSpeed(factor*7+minSpeed);
break;
case IR_BUTTON_6:
ChangeSpeed(factor*6+minSpeed);
break;
case IR_BUTTON_5:
ChangeSpeed(factor*5+minSpeed);
break;
case IR_BUTTON_4:
ChangeSpeed(factor*4+minSpeed);
break;
case IR_BUTTON_3:
ChangeSpeed(factor*3+minSpeed);
break;
case IR_BUTTON_2:
ChangeSpeed(factor*2+minSpeed);
break;
case IR_BUTTON_1:
ChangeSpeed(factor*1+minSpeed);
break;
case IR_BUTTON_MENU:
AutoMode();
break;
default:
// Stop();
break;
}
}
else
{
Stop();
}
}
void Forward()
{
MotorL.run(moveSpeed);
MotorR.run(moveSpeed);
}
void Backward()
{
MotorL.run(-moveSpeed);
MotorR.run(-moveSpeed);
}
void TurnLeft()
{
MotorL.run(-moveSpeed);
MotorR.run(moveSpeed);
}
void TurnRight()
{
MotorL.run(moveSpeed);
MotorR.run(-moveSpeed);
}
void Stop()
{
MotorL.run(0);
MotorR.run(0);
}
void ChangeSpeed(int spd)
{
moveSpeed = spd;
}
void AutoMode()
{
while(infraredReceiverDecode.read()!=IR_BUTTON_MENU)
{
distance = UltrasonicSensor.distanceCm();
if(distance>2&&distance<40)
{
randnum=random(300);
if(randnum > 150 && !rightflag)
{
// leftflag=true;
TurnLeft();
}
else
{
rightflag=true;
TurnRight();
}
}
else
{
//leftflag=false;
rightflag=false;
Forward();
}
delay(150);
}
}