231 lines
6.5 KiB
C++
231 lines
6.5 KiB
C++
#include <Arduino.h> /* Just in case of using the vscode ide for arduino programming, not needed if using the commom arduino ide */
|
|
#include <IRremote.hpp> // library for the IR receiver
|
|
#define RPWM 5
|
|
#define LPWM 6
|
|
|
|
const int SPEED_SLOW = 60;
|
|
const int SPEED_MED = 90;
|
|
const int SPEED_MAX = 140;
|
|
|
|
// IR receive parameters
|
|
const int IR = 9;
|
|
IRrecv IrReceiv(IR);
|
|
decode_results result;
|
|
|
|
/*
|
|
Pins connected to aduino( in english )
|
|
|
|
IBT-2 pin 1 (RPWM) to Arduino pin 5(PWM)
|
|
IBT-2 pin 2 (LPWM) to Arduino pin 6(PWM)
|
|
IBT-2 pins 3 (R_EN), 4 (L_EN), 7 (VCC) to Arduino 5V
|
|
IBT-2 pin 8 (GND) to Arduino's GND
|
|
IBT-2 pins 5 (R_IS) and 6 (L_IS) not connected
|
|
*/
|
|
|
|
// IR codes
|
|
const int code1 = 0xAA1;
|
|
const int code2 = 0xAA2;
|
|
const int code3 = 0xAA3;
|
|
const int code4 = 0xAA4;
|
|
const int code5 = 0xAA5;
|
|
const int code6 = 0xAA6;
|
|
|
|
int code = 0;//0xAA1;
|
|
|
|
bool cond = true;
|
|
int i = 0;
|
|
long start;
|
|
|
|
void rotateMotor(int vel) /* function to set the sense of rotation of the motor */
|
|
{
|
|
analogWrite(LPWM, 0);
|
|
analogWrite(RPWM, vel);
|
|
}
|
|
|
|
void setup()
|
|
{
|
|
Serial.begin(9600);
|
|
IrReceiv.enableIRIn();
|
|
}
|
|
|
|
int code_ant = 0;
|
|
|
|
void loop()
|
|
{
|
|
/*if (IrReceiv.decode(&result))
|
|
{
|
|
Serial.print(result.bits);
|
|
Serial.print(": ");
|
|
Serial.println(result.value, HEX);
|
|
IrReceiv.resume();
|
|
if ((result.value==code1)||(result.value==code2)||(result.value==code3)||(result.value==code4)||(result.value==code5)||(result.value==code6))
|
|
{
|
|
code = result.value;
|
|
if (code==code_ant) code = 0;
|
|
else code_ant=code;
|
|
}
|
|
}*/
|
|
|
|
switch(code)
|
|
{
|
|
case code1: // stopped -> slow - check
|
|
for(unsigned long start = millis(); (millis() - start) <= 1000;) // responsible for maintaining no velocity
|
|
{
|
|
rotateMotor(0);
|
|
Serial.print("Motor status: stopped || ");
|
|
Serial.print("Time: ");
|
|
Serial.print((millis()-start)/1000);
|
|
Serial.print(" || Speed: ");
|
|
Serial.println(0);
|
|
delay(392);
|
|
}
|
|
|
|
start = millis();
|
|
|
|
for (int i = 0; i < (SPEED_SLOW); i+=4) // responsible for increasing the speed
|
|
{
|
|
rotateMotor(i);
|
|
Serial.print("Motor status: stopped -> slow || ");
|
|
Serial.print("Time: ");
|
|
Serial.print((millis()-start)/1000);
|
|
Serial.print(" || Speed: ");
|
|
Serial.println(i);
|
|
delay(392);
|
|
}
|
|
break;
|
|
case code2: // slow -> medium - check
|
|
|
|
start = millis();
|
|
|
|
for(unsigned long start = millis(); (millis() - start) <= 1000;) // responsible for maintaning the constant velocity
|
|
{
|
|
rotateMotor(SPEED_SLOW);
|
|
Serial.print("Motor status: slow || ");
|
|
Serial.print("Time: ");
|
|
Serial.print((millis()-start)/1000);
|
|
Serial.print(" || Speed: ");
|
|
Serial.println(SPEED_SLOW);
|
|
delay(392);
|
|
}
|
|
|
|
start = millis();
|
|
|
|
for (int i = SPEED_SLOW; i < (SPEED_MED); i+=1) // responsible for increasing the speed
|
|
{
|
|
rotateMotor(i);
|
|
Serial.print("Motor status: slow -> medium || ");
|
|
Serial.print("Time: ");
|
|
Serial.print((millis()-start)/1000);
|
|
Serial.print(" || Speed: ");
|
|
Serial.println(i);
|
|
delay(392);
|
|
}
|
|
break;
|
|
case code3: // medium -> fast - check
|
|
|
|
for(unsigned long start = millis(); (millis() - start) <= 1000;) // responsible for maintaning the constant velocity
|
|
{
|
|
rotateMotor(SPEED_MED);
|
|
Serial.print("Motor status: medium || ");
|
|
Serial.print("Time: ");
|
|
Serial.print((millis()-start)/1000);
|
|
Serial.print(" || Speed: ");
|
|
Serial.println(SPEED_MED);
|
|
delay(392);
|
|
}
|
|
|
|
start = millis();
|
|
|
|
for (int i = SPEED_MED; i < (SPEED_MAX); i+=1) // responsible for increasing the speed
|
|
{
|
|
rotateMotor(i);
|
|
Serial.print("Motor status: medium -> fast || ");
|
|
Serial.print("Time: ");
|
|
Serial.print((millis()-start)/1000);
|
|
Serial.print(" || Speed: ");
|
|
Serial.println(i);
|
|
delay(392);
|
|
}
|
|
break;
|
|
case code4: // fast -> medium - check
|
|
|
|
for(unsigned long start = millis(); (millis() - start) <= 1000;) // responsible for maintaning the constant velocity
|
|
{
|
|
rotateMotor(SPEED_MAX);
|
|
Serial.print("Motor status: fast || ");
|
|
Serial.print("Time: ");
|
|
Serial.print((millis()-start)/1000);
|
|
Serial.print(" || Speed: ");
|
|
Serial.println(SPEED_MAX);
|
|
delay(392);
|
|
}
|
|
|
|
start = millis();
|
|
|
|
for (int i = SPEED_MAX; i > (SPEED_MED); i-=3) // responsible for decreasing the speed
|
|
{
|
|
rotateMotor(i);
|
|
Serial.print("Motor status: fast -> medium || ");
|
|
Serial.print("Time: ");
|
|
Serial.print((millis()-start)/1000);
|
|
Serial.print(" || Speed: ");
|
|
Serial.println(i);
|
|
delay(392);
|
|
}
|
|
break;
|
|
case code5: // medium -> slow - check
|
|
for(unsigned long start = millis(); (millis() - start) <= 1000;) // responsible for maintaning the constant velocity
|
|
{
|
|
rotateMotor(SPEED_MED);
|
|
Serial.print("Motor status: medium || ");
|
|
Serial.print("Time: ");
|
|
Serial.print((millis()-start)/1000);
|
|
Serial.print(" || Speed: ");
|
|
Serial.println(SPEED_MED);
|
|
delay(392);
|
|
}
|
|
|
|
start = millis();
|
|
|
|
for (int i = SPEED_MED; i > (SPEED_SLOW); i-=3) // responsible for decreasing the speed
|
|
{
|
|
rotateMotor(i);
|
|
Serial.print("Motor status: medium -> slow || ");
|
|
Serial.print("Time: ");
|
|
Serial.print((millis()-start)/1000);
|
|
Serial.print(" || Speed: ");
|
|
Serial.println(i);
|
|
delay(392);
|
|
}
|
|
break;
|
|
case code6: // slow -> stopped - check
|
|
for(unsigned long start = millis(); (millis() - start) <= 1000;) // responsible for maintaning the constant velocity
|
|
{
|
|
rotateMotor(SPEED_SLOW);
|
|
Serial.print("Motor status: slow || ");
|
|
Serial.print("Time: ");
|
|
Serial.print((millis()-start)/1000);
|
|
Serial.print(" || Speed: ");
|
|
Serial.println(SPEED_SLOW);
|
|
delay(392);
|
|
}
|
|
|
|
start = millis();
|
|
|
|
for (int i = SPEED_SLOW; i > (0); i-=3) // responsible for decreasing the speed
|
|
{
|
|
rotateMotor(i);
|
|
Serial.print("Motor status: slow -> stopped || ");
|
|
Serial.print("Time: ");
|
|
Serial.print((millis()-start)/1000);
|
|
Serial.print(" || Speed: ");
|
|
Serial.println(i);
|
|
delay(392);
|
|
}
|
|
break;
|
|
}
|
|
//code = 0;
|
|
if (code<0xAA7) code++;
|
|
|
|
}
|