Mittwoch, 1. Mai 2013

Servo mit Fernbedienung - Quellcode

Hier der Quellcode zur Ansteuerung des Servos mit der AIWA-Fernbedienung:
#define LED_unten 7
#define LED_oben 6
#define IR36Khz 5 // TSOP 31236
#define laenge 48
#define time 100000

#include <Servo.h>
Servo meinServo; 
int muster[laenge];
int i = 0;
int Servofaktor = 1;
int taktPin = 8;     // SH_CP
int speicherPin = 11; // ST_CP
int datenPin = 10;   // DS

void setup() {
  pinMode(taktPin, OUTPUT);
  pinMode(speicherPin, OUTPUT);
  pinMode(datenPin, OUTPUT);
  pinMode(LED_unten, OUTPUT);
  pinMode(LED_oben, OUTPUT);
  pinMode(IR36Khz, INPUT); 
  digitalWrite(LED_unten, LOW);
  digitalWrite(LED_oben, LOW); 
  meinServo.attach(9); // Objekt mit Pin 9 verbinden
 // Serial.begin(9600);
  setLEDs(B00000001);
}

void loop() {
  readRemote(pulseIn(IR36Khz, HIGH, time));   
}

void readRemote(int pulse1) {
    i = 0; 
    if (pulse1 > 4000) {  
    
    boolean pressed = true;
    int highpulse = pulseIn(IR36Khz, HIGH, time);
    while ((highpulse < 4000) &&  (highpulse > 100)){
      if (highpulse > 1000) {
        muster[i++] = 1;
      } else {
        muster[i++] = 0;
      }
      highpulse = pulseIn(IR36Khz, HIGH, time);  
    }
    
    for (i; i < laenge; i++) muster[i] = -1;
    
    while (pressed){
      getButton();
      pressed = (highpulse > 20000) & (pulseIn(IR36Khz, HIGH, time) > 4000);
    }
    
  digitalWrite(LED_unten, LOW);
  digitalWrite(LED_oben, LOW); 
  }  
}

void getButton(){
  unsigned int a = 0;
  unsigned int b = 0;
  unsigned int c = 0;
  
  for (int i = 0; i < 16; i++) {
    if (muster[i] == 1) a = a + (1 << i);
    if (muster[i+16] == 1) b = b + (1 << i);
    if (muster[i+32] == 1) c = c + (1 << i);
  }
  
  if (a == 57464 && b == 19440 && c == 948) { // Pfeil unten
    downArrow();
  } else if (a == 57464 && b == 18416 && c == 952) { // Pfeil oben
    upArrow();
  } else if (a == 49273 && b == 17392 && c == 956) { // Down Button
    downButton(); 
  } else if (a == 49273 && b == 16368 && c == 960) { // Up Button
    upButton(); 
  } else {
 //   Serial.println(a);
  //  Serial.println(b);
  //  Serial.println(c);
  }
}

void upButton() 
{
  if (Servofaktor < 15) Servofaktor ++;  
  digitalWrite(LED_oben, HIGH);
  aktualisiereLED();
}

void downButton() 
{
  if (Servofaktor > 2) Servofaktor --; 
  digitalWrite(LED_unten, HIGH); 
  aktualisiereLED();
}

void aktualisiereLED()
{
  switch (Servofaktor) 
  {
    case 1: setLEDs(B00000001); break;
    case 2: setLEDs(B00000001); break;
    case 3: setLEDs(B00000011); break;
    case 4: setLEDs(B00000011); break;
    case 5: setLEDs(B00000111); break;
    case 6: setLEDs(B00000111); break;
    case 7: setLEDs(B00001111); break;
    case 8: setLEDs(B00001111); break;
    case 9: setLEDs(B00011111); break;
    case 10: setLEDs(B00011111); break;
    case 11: setLEDs(B00111111); break;
    case 12: setLEDs(B00111111); break;
    case 13: setLEDs(B01111111); break;
    case 14: setLEDs(B01111111); break;
    case 15: setLEDs(B11111111); break;
    case 16: setLEDs(B11111111); break;
  }
}

void setLEDs(byte bitmuster) {
  digitalWrite(speicherPin, LOW);
  shiftOut(datenPin, taktPin, LSBFIRST, bitmuster);
  digitalWrite(speicherPin, HIGH);
}

void upArrow() 
{
  if (Servoposition > 2) 
  {
    Servoposition = Servoposition - Servofaktor;
    if (Servoposition < 1) Servoposition = 1;
    meinServo.write(Servoposition);
    //delay(20);
  }  
  digitalWrite(LED_oben, HIGH);
}

void downArrow() 
{
  if (Servoposition < 180) 
  {
    Servoposition = Servoposition + Servofaktor;
    if (Servoposition > 180) Servoposition = 180;
    meinServo.write(Servoposition);
   // delay(20);
  }  
  digitalWrite(LED_unten, HIGH);
}
Hier der Quellcode:

Keine Kommentare:

Kommentar veröffentlichen