250 lines
7.3 KiB
Arduino
250 lines
7.3 KiB
Arduino
|
#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 unsigned long TIME_STATIC=1000;
|
||
|
|
||
|
const int SPEED_SLOW = 60;
|
||
|
const int SPEED_MED = 140;
|
||
|
const int SPEED_MAX = 255;
|
||
|
|
||
|
const float INCREASE_1 = 3; //0 -> low
|
||
|
const float INCREASE_2 = 0.1; //low -> medium
|
||
|
const float INCREASE_3 = 0.1; //medium -> fast
|
||
|
const float DECREASE = 0.1;
|
||
|
|
||
|
// 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;
|
||
|
|
||
|
void rotateMotor(int vel) /* function to set the sense of rotation of the motor */
|
||
|
{
|
||
|
analogWrite(LPWM, vel);
|
||
|
analogWrite(RPWM, 0);
|
||
|
}
|
||
|
|
||
|
unsigned long ini_time;
|
||
|
|
||
|
void setup()
|
||
|
{
|
||
|
Serial.begin(9600);
|
||
|
IrReceiv.enableIRIn();
|
||
|
rotateMotor(60);
|
||
|
delay(500);
|
||
|
rotateMotor(0);
|
||
|
delay(30000);
|
||
|
ini_time = millis();
|
||
|
}
|
||
|
|
||
|
int code_ant = 0;
|
||
|
unsigned long start = millis();
|
||
|
|
||
|
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
|
||
|
start = millis();
|
||
|
while((millis() - start) <= TIME_STATIC) // responsible for maintaining no velocity
|
||
|
{
|
||
|
rotateMotor(0);
|
||
|
Serial.print("Motor status: stopped || ");
|
||
|
Serial.print("Time: ");
|
||
|
Serial.print((millis()-ini_time)/1000);
|
||
|
Serial.print(" || Time_start: ");
|
||
|
Serial.print((millis()-start)/1000);
|
||
|
Serial.print(" || Speed: ");
|
||
|
Serial.println(0);
|
||
|
delay(392);
|
||
|
}
|
||
|
|
||
|
for (float i = 0; i < (SPEED_SLOW); i+=INCREASE_1) // responsible for increasing the speed
|
||
|
{
|
||
|
rotateMotor(i);
|
||
|
Serial.print("Motor status: stopped -> slow || ");
|
||
|
Serial.print("Time: ");
|
||
|
Serial.print((millis()-ini_time)/1000);
|
||
|
Serial.print(" || Speed: ");
|
||
|
Serial.println(i);
|
||
|
delay(392);
|
||
|
}
|
||
|
break;
|
||
|
case code2: // slow -> medium - check
|
||
|
start = millis();
|
||
|
while((millis() - start) <= 30*TIME_STATIC) // responsible for maintaning the constant velocity
|
||
|
{
|
||
|
rotateMotor(SPEED_SLOW);
|
||
|
Serial.print("Motor status: slow || ");
|
||
|
Serial.print("Time: ");
|
||
|
Serial.print((millis()-ini_time)/1000);
|
||
|
Serial.print(" || Time_start: ");
|
||
|
Serial.print((millis()-start)/1000);
|
||
|
Serial.print(" || Speed: ");
|
||
|
Serial.println(SPEED_SLOW);
|
||
|
delay(392);
|
||
|
}
|
||
|
for (float i = SPEED_SLOW; i < (SPEED_MED); i+=INCREASE_2) // responsible for increasing the speed
|
||
|
{
|
||
|
rotateMotor(i);
|
||
|
Serial.print("Motor status: slow -> medium || ");
|
||
|
Serial.print("Time: ");
|
||
|
Serial.print((millis()-ini_time)/1000);
|
||
|
Serial.print(" || Speed: ");
|
||
|
Serial.println(i);
|
||
|
delay(392);
|
||
|
}
|
||
|
break;
|
||
|
case code3: // medium -> fast - check
|
||
|
start = millis();
|
||
|
while((millis() - start) <= 30*TIME_STATIC) // responsible for maintaning the constant velocity
|
||
|
{
|
||
|
rotateMotor(SPEED_MED);
|
||
|
Serial.print("Motor status: medium || ");
|
||
|
Serial.print("Time: ");
|
||
|
Serial.print((millis()-ini_time)/1000);
|
||
|
Serial.print(" || Time_start: ");
|
||
|
Serial.print((millis()-start)/1000);
|
||
|
Serial.print(" || Speed: ");
|
||
|
Serial.println(SPEED_MED);
|
||
|
delay(392);
|
||
|
}
|
||
|
for (float i = SPEED_MED; i < (SPEED_MAX); i+=INCREASE_3) // responsible for increasing the speed
|
||
|
{
|
||
|
rotateMotor(i);
|
||
|
Serial.print("Motor status: medium -> fast || ");
|
||
|
Serial.print("Time: ");
|
||
|
Serial.print((millis()-ini_time)/1000);
|
||
|
Serial.print(" || Speed: ");
|
||
|
Serial.println(i);
|
||
|
delay(392);
|
||
|
}
|
||
|
break;
|
||
|
case code4: // fast -> medium - check
|
||
|
start = millis();
|
||
|
while((millis() - start) <= 180*TIME_STATIC) // responsible for maintaning the constant velocity
|
||
|
{
|
||
|
rotateMotor(SPEED_MAX);
|
||
|
Serial.print("Motor status: fast || ");
|
||
|
Serial.print("Time: ");
|
||
|
Serial.print((millis()-ini_time)/1000);
|
||
|
Serial.print(" || Time_start: ");
|
||
|
Serial.print((millis()-start)/1000);
|
||
|
Serial.print(" || Speed: ");
|
||
|
Serial.println(SPEED_MAX);
|
||
|
delay(392);
|
||
|
}
|
||
|
|
||
|
for (float i = SPEED_MAX; i > (SPEED_MED); i-=DECREASE) // responsible for decreasing the speed
|
||
|
{
|
||
|
rotateMotor(i);
|
||
|
Serial.print("Motor status: fast -> medium || ");
|
||
|
Serial.print("Time: ");
|
||
|
Serial.print((millis()-ini_time)/1000);
|
||
|
Serial.print(" || Speed: ");
|
||
|
Serial.println(i);
|
||
|
delay(392);
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
case code5: // medium -> slow - check
|
||
|
start = millis();
|
||
|
while((millis() - start) <= TIME_STATIC) // responsible for maintaning the constant velocity
|
||
|
{
|
||
|
rotateMotor(SPEED_MED);
|
||
|
Serial.print("Motor status: medium || ");
|
||
|
Serial.print("Time: ");
|
||
|
Serial.print((millis()-ini_time)/1000);
|
||
|
Serial.print(" || Time_start: ");
|
||
|
Serial.print((millis()-start)/1000);
|
||
|
Serial.print(" || Speed: ");
|
||
|
Serial.println(SPEED_MED);
|
||
|
delay(392);
|
||
|
}
|
||
|
for (float i = SPEED_MED; i > (SPEED_SLOW); i-=DECREASE) // responsible for decreasing the speed
|
||
|
{
|
||
|
rotateMotor(i);
|
||
|
Serial.print("Motor status: medium -> slow || ");
|
||
|
Serial.print("Time: ");
|
||
|
Serial.print((millis()-ini_time)/1000);
|
||
|
Serial.print(" || Speed: ");
|
||
|
Serial.println(i);
|
||
|
delay(392);
|
||
|
}
|
||
|
break;
|
||
|
case code6: // slow -> stopped - check
|
||
|
start = millis();
|
||
|
while((millis() - start) <= TIME_STATIC) // responsible for maintaning the constant velocity
|
||
|
{
|
||
|
rotateMotor(SPEED_SLOW);
|
||
|
Serial.print("Motor status: slow || ");
|
||
|
Serial.print("Time: ");
|
||
|
Serial.print((millis()-ini_time)/1000);
|
||
|
Serial.print(" || Time_start: ");
|
||
|
Serial.print((millis()-start)/1000);
|
||
|
Serial.print(" || Speed: ");
|
||
|
Serial.println(SPEED_SLOW);
|
||
|
delay(392);
|
||
|
}
|
||
|
for (float i = SPEED_SLOW; i > (0); i-=DECREASE) // responsible for decreasing the speed
|
||
|
{
|
||
|
rotateMotor(i);
|
||
|
Serial.print("Motor status: slow -> stopped || ");
|
||
|
Serial.print("Time: ");
|
||
|
Serial.print((millis()-ini_time)/1000);
|
||
|
Serial.print(" || Speed: ");
|
||
|
Serial.println(i);
|
||
|
delay(392);
|
||
|
}
|
||
|
break;
|
||
|
}
|
||
|
//code = 0;
|
||
|
if (code<0xAA7) code++;
|
||
|
else
|
||
|
{
|
||
|
code = code1;
|
||
|
Serial.print("System stopped!!");
|
||
|
delay(120000);
|
||
|
ini_time = millis();
|
||
|
}
|
||
|
|
||
|
}
|