Stap 3: Arduino Sketch
Niet zullen we kijken naar de code die wordt uitgevoerd op de arduino. Ik heb zo veel mogelijk gereageerd, maar als je niet begrijpt iets laat het me weten in de commentaren.
Wat deze code doet:
Lees de IR ontvanger en stuur het via seriële verbinding met de PC.
Lees de seriële verbinding vanaf de PC en het wijzigen van de LED's.
uint8_t situation = 0; // 3 > 1 or 0 received | 2 > Repeat previous code uint8_t START = 0; // Detect if the transmission is started uint8_t x = 0; uint8_t BIT = 0; // Which bit is send (1/0) uint8_t Id = 0; // What's the remote ID uint8_t Id_inv = 0; uint8_t Data = 0; // Which key is pressed on the remote uint8_t Data_back = 0; // Backup of the key for repeat uint8_t Data_inv = 0; uint8_t Repeat = 0; // pass first repeat /* Variables for the pulse measurement */ uint16_t Time_old = 0; uint16_t Time = 0; uint16_t TimeDelta = 0; /* Data received from the computer */ /* 1 > Caps lock changed */ /* 2 > Num lock changed */ /* 3 > Scroll lock changed */ uint8_t incomingByte = 0; /* LED pins */ #define caps 24 #define numl 22 #define scrl 23 void setup(void) { /* Interrupt for reading the IR code */ attachInterrupt(0, IR_Read, FALLING); /* Serial connection to the computer software */ Serial.begin(9600); /* Led pins as output */ pinMode(caps, OUTPUT); pinMode(numl, OUTPUT); pinMode(scrl, OUTPUT); /* Led's out */ digitalWrite(caps, LOW); digitalWrite(numl, LOW); digitalWrite(scrl, LOW); } void loop(void) { /* If something from the computer is received */ if (Serial.available() > 0) { /* Read the incomming data */ incomingByte = Serial.read(); switch(incomingByte){ case 1: /* If 1 */ /* Caps Lock led on/off > off/on */ digitalWrite(caps, !digitalRead(caps)); break; case 2: /* If 2 */ /* Num Lock led on/off > off/on */ digitalWrite(numl, !digitalRead(numl)); break; case 3: /* If 3 */ /* Scroll Lock led on/off > off/on */ digitalWrite(scrl, !digitalRead(scrl)); break; default: break; } } } /* Function for reading the IR receiver using interrupts */ /* YOU DON'T NEED TO CHANGE ANYTHING HERE */ void IR_Read(void) { Time = micros(); if (Time_old != 0) { TimeDelta = Time - Time_old; if ((TimeDelta > 12000)&&(TimeDelta < 14000)) { START = 1; x = 0; situation = 1; Id = 0; Id_inv = 0; Data = 0; Data_inv = 0; } else if ((TimeDelta > 10000)&&(TimeDelta < 12000)) { situation = 2; // repeat } else if ((TimeDelta > 1500)&&(TimeDelta < 2500)) { situation = 3; //"1" BIT = 1; } else if ((TimeDelta > 1000)&&(TimeDelta < 1500)) { situation = 3; //"0" BIT = 0; } else situation = 5; if (situation == 3) { if (x < 8) { Id |= BIT; if (x < 7) Id <<= 1; x++; } else if (x < 16) { Id_inv |= BIT; if (x < 15) Id_inv <<= 1; x++; } else if (x < 24) { Data |= BIT; if (x < 23) Data <<= 1; x++; } else if (x < 32) { Data_inv |= BIT; if (x < 31) { Data_inv <<= 1; } else { Serial.println(Data); Data_back = Data; Repeat = 0; } x++; } } else if (situation == 2) { if(Repeat == 1) { /* Send received data to the computer */ Serial.println(Data_back); } else { Repeat = 1; } } } Time_old = Time; }