Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.7 kB
6
Indexable
Never
#include <IRremote.h>

const int RECV_PIN = 4;
IRrecv irrecv(RECV_PIN);
decode_results results;

#define IR_BUTTON_0 0xFF9867
#define IR_BUTTON_1 0xFFA25D
#define IR_BUTTON_2 0xFF629D
#define IR_BUTTON_3 0xFFE21D
#define IR_BUTTON_4 0xFF22DD
#define IR_BUTTON_5 0xFF02FD
#define IR_BUTTON_6 0xFFC23D
#define IR_BUTTON_7 0xFFE01F
#define IR_BUTTON_8 0xFFA857
#define IR_BUTTON_9 0xFF906F
#define IR_BUTTON_LEFT 0xFF10EF
#define IR_BUTTON_RIGHT 0xFF5AA5

#define EngineL1 6
#define EngineL2 7
#define EngineLE 9

const int accelerationTime = 100;
const int accelerationAmount = 20;
int targetEngineSpeed = 0;
int currentEngineSpeed = 0;
const int totalEngineSpeedLevel = 9;
int targetEngineSpeedLevel = 0;

int calculateEngineSpeed(int currentEngineSpeedLevel){
  return ((255 / totalEngineSpeedLevel) * currentEngineSpeedLevel);
}

void setup() {
  Serial.begin(9600);
  irrecv.enableIRIn();

  pinMode(EngineLE, OUTPUT);
  pinMode(EngineL1, OUTPUT);
  pinMode(EngineL2, OUTPUT);

  digitalWrite(EngineL1, LOW);
  digitalWrite(EngineL2, LOW);
  analogWrite(EngineLE, calculateEngineSpeed(currentEngineSpeedLevel));
}

void loop() {
  if (irrecv.decode(&results)){
    switch(results.value){
      case IR_BUTTON_0:
        targetEngineSpeedLevel = 0;
      break;

      case IR_BUTTON_1:
        targetEngineSpeedLevel = 1;
      break;

      case IR_BUTTON_2:
        targetEngineSpeedLevel = 2;
      break;

      case IR_BUTTON_3:
        targetEngineSpeedLevel = 3;
      break;

      case IR_BUTTON_4:
        targetEngineSpeedLevel = 4;
      break;

      case IR_BUTTON_5:
        targetEngineSpeedLevel = 5;
      break;

      case IR_BUTTON_6:
        targetEngineSpeedLevel = 6;
      break;

      case IR_BUTTON_7:
        targetEngineSpeedLevel = 7;
      break;

      case IR_BUTTON_8:
        targetEngineSpeedLevel = 8;
      break;

      case IR_BUTTON_9:
        targetEngineSpeedLevel = 9;
      break;

      case IR_BUTTON_LEFT:
        if(currentEngineSpeed == 0){
          digitalWrite(EngineL1, HIGH);
          digitalWrite(EngineL2, LOW);
        }
      break;

      case IR_BUTTON_RIGHT:
        if(currentEngineSpeed == 0){
          digitalWrite(EngineL1, LOW);
          digitalWrite(EngineL2, HIGH);
        }
      break;
    }

    targetEngineSpeed = calculateEngineSpeed(targetEngineSpeedLevel);
    irrecv.resume();
  }

  if(targetEngineSpeed > currentEngineSpeed){
  	currentEngineSpeed += accelerationAmount;
  }else{
  	currentEngineSpeed -= accelerationAmount;
  }
  
  if(currentEngineSpeed >= 255){
  	currentEngineSpeed = 255;
  }else if(currentEngineSpeed <= 0){
  	currentEngineSpeed = 0;
  }

  analogWrite(EngineLE, currentEngineSpeed);
  delay(accelerationTime);
}