SHARE
TWEET

IR-Sketch

a guest Jul 29th, 2018 213 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <IRremote.h>
  2.  
  3. #define M1 7
  4. #define M2 8
  5. #define M3 9
  6. #define M4 2
  7. #define M5 4
  8. #define M6 3
  9.  
  10. int RECV_PIN = 12;
  11. int command = 4;
  12.  
  13. const int DEFAULT_VAL = 80;
  14. const int STOP_VAL = 0;
  15.  
  16. IRrecv irrecv(RECV_PIN);  //Objekt initialisieren für die IR Übertragung
  17. decode_results results;
  18.  
  19. void setup(){
  20.   irrecv.enableIRIn(); // Start the receiver
  21.   Serial.begin(9600); //Serielle kommunikation mit 9600 Baud beginnen.
  22.  
  23.   //output für die Motoren des Antriebs
  24.   pinMode(7, OUTPUT);
  25.   pinMode(8, OUTPUT);
  26.   pinMode(9, OUTPUT);
  27.   pinMode(2, OUTPUT);
  28.   pinMode(4, OUTPUT);
  29.   pinMode(3, OUTPUT);
  30.  
  31.   //input für die Fernbedienung
  32.   pinMode(RECV_PIN, INPUT);
  33. }
  34.  
  35. void loop() {
  36.   command = readCommand();
  37.  
  38.   //vorwärts fahren
  39.   if (command == 0){
  40.     Serial.println("vorwärts fahren");
  41.     moveVehicle(HIGH, LOW, LOW, HIGH);
  42.   }
  43.  
  44.   //rückwärts fahren
  45.   if (command == 1){
  46.     Serial.println("rückwärts fahren");
  47.     moveVehicle(LOW, HIGH, HIGH, LOW);
  48.   }
  49.  
  50.   //rechts fahren
  51.   if (command == 2){
  52.     Serial.println("rechts fahren");
  53.     moveVehicle(LOW, HIGH, LOW, HIGH);
  54.   }
  55.  
  56.   //links fahren
  57.   if (command == 3){
  58.     Serial.println("links fahren");
  59.     moveVehicle(HIGH, LOW, HIGH, LOW);
  60.   }
  61.  
  62.   //stop
  63.   if (command == 4){
  64.     Serial.println("stop!!!");
  65.     analogWrite(M3, STOP_VAL);
  66.     analogWrite(M6, STOP_VAL);
  67.   }
  68.  
  69.   delay(250);
  70. }
  71.  
  72. int readCommand(){
  73.  int command = 4;
  74.  if (irrecv.decode(&results)) {
  75.   //Serial.println(results.value, HEX);
  76.   //Serial.println(results.value, DEC);
  77.   switch (results.value){
  78.     case 16718055 : //vorwärts fahren
  79.       command = 0;
  80.     break;
  81.     case 16730805 : //rückwärts fahren
  82.       command = 1;
  83.     break;
  84.     case 16734885 : //rechts fahren
  85.       command = 2;
  86.     break;
  87.     case 16716015 : //links fahren
  88.       command = 3;
  89.     break;
  90.     case 16726215 : //stop!!!
  91.       command = 4;
  92.     break;
  93.     }
  94.  
  95.   irrecv.resume(); // Receive the next value
  96.  }
  97.  return command;
  98. }
  99.  
  100. void moveVehicle(int m1State, int m2State, int m4State, int m5State){
  101.   // rechte Kette
  102.   digitalWrite(M1, m1State);
  103.   digitalWrite(M2, m2State);
  104.   analogWrite(M3, DEFAULT_VAL);
  105.   // linke Kette
  106.   digitalWrite(M4, m4State);
  107.   digitalWrite(M5, m5State);
  108.   analogWrite(M6, DEFAULT_VAL);
  109. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top