Hallo,
Ik heb 2 arduino's met elk een IR transmitter en receiver. De receivers zijn verschillend, 38kHz en 56kHz (dit heeft niks met het probleem te maken!)
Als ik op arduino 1 hem op transmitten zet, ontvangt arduino 2 keurig de waardes. Andersom ook. Nu wil ik tegelijk kunnen zenden én ontvangen, maar dat gaat fout. Ik zie wel wat er fout gaat maar de oplossing heb ik niet. De fout is namelijk, als de arduino aan het verzenden is, en de ander ook, dan moet het programma éérst het verzenden afhandelen en daarna pas het ontvangen. Om dit op te lossen heb je volgens mij een interrupt nodig.
Kan iemand mij helpen hoe ik dit het beste kan aanpakken? De code zit erbij, al is deze niet duidelijk en weinig gecomment.
Bedankt!
Mathero11
/*
Name: timertest.ino
Created: 12/4/2017 12:06:25 PM
Author: Asus-Laptop
*/
volatile int count;
volatile int bitje;
const byte interruptPin = 3;
byte aan;
byte binary[4];
volatile int count2;
volatile int eersteTeller;
volatile int tweedeTeller;
volatile int tellerbegin;
volatile int tellereind;
volatile int count3;
uint8_t bytelengte;
byte variable;
double decimal;
ISR(TIMER1_COMPA_vect) {
count++;
PORTB ^= (1 << PORTB3);
count3++;
}
void blink() {
count2++;
//irontvanger;
}
int main(void) {
init();
PORTD |= (1 << PORTD2); /* pullup */
Serial.begin(9600);
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);
DDRB |= (1 << PORTB3);
PORTB |= (1 << PORTB3);
cli(); // stop interrupts
TCCR1A = 0; // set entire TCCR1A register to 0
TCCR1B = 0; // same for TCCR1B
TCNT1 = 0; // initialize counter value to 0
OCR1A = 142; // compare A register value (210 * clock speed) 209 en 142
TCCR1B |= (1 << WGM12);
TCCR1B |= (0 << CS12) | (0 << CS11) | (1 << CS10);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
sei(); // allow interrupts
while (1) {
//sendNibble(2);
irontvanger();
}
}
void sendBit(uint8_t sendBit) {
bitje = sendBit;
stuurLed();
aan = 1;
}
void stuurLed() {
while (aan) {
while (bitje == 0) {
if (count>1000 && count<2000) {
DDRB &= ~(1 << PORTB3);
}
if (count >= 2000) {
DDRB |= (1 << PORTB3);
count = 0;
aan = 0;
count2 = 0;
break;
}
}
while (bitje == 1) {
if (count>1000 && count<2500) {
DDRB &= ~(1 << PORTB3);
}
if (count >= 2500) {
DDRB |= (1 << PORTB3);
count = 0;
aan = 0;
count2 = 0;
break;
}
}
while (bitje == 2) {
if (count>1000 && count<3000) {
DDRB &= ~(1 << PORTB3);
}
if (count >= 3000) {
DDRB |= (1 << PORTB3);
count = 0;
aan = 0;
count2 = 0;
break;
}
}
while (bitje == 3) {
if (count>1000 && count<3500) {
DDRB &= ~(1 << PORTB3);
}
if (count >= 3500) {
DDRB |= (1 << PORTB3);
count = 0;
aan = 0;
count2 = 0;
break;
}
}
} //end while 'aan'
}
void ontvangen() {
//Serial.println(tellereind);
if (bytelengte < 5) { //max 5 bits
if (eenofnul(tellereind) == 3) {
Serial.println("\nStop bit\n");
bytelengte = 0;
variable = 0;
printdecimal();
}
else if (eenofnul(tellereind) == 2) {
Serial.println("Start bit");
}
else {
Serial.print(eenofnul(tellereind));
binary[bytelengte] = eenofnul(tellereind);
bytelengte++;
}
}
}
void printdecimal() { //functie om binary om te zetten naar decimal
decimal = 0;
int ooh = 3;
for (int i = 0; i <= 3; i++) {
decimal += ((pow(2, ooh)) * binary[i]);
ooh--;
}
Serial.print("Decimaal: ");
Serial.println(decimal);
}
int eenofnul(int result) { //38KHZ versie 142 timer (ontvangen) zenden: 209
if (result > 2600 && result < 3300) {
return 2;
}
else if (result > 3300) {
return 3;
}
if (result > 1900 && result < 2600) {
return 1;
}
else if (result < 2000) {
return 0;
}
}
void irontvanger() {
while (count2 > 0 && count2 < 4) {
if (count2 == 1) {
tellerbegin = count3;
}
if (count2 == 3) {
tellereind = count3;
tellereind -= tellerbegin;
count3 = 0;
//Serial.println(tellereind);
if (eenofnul(tellereind) == 2) { //start bit
variable = 1;
}
if (variable) {
ontvangen();
}
count2 = 1;
}
if (count2 > 3) {
count2 = 1;
}
}
}
void sendNibble(char decimaltosend){
int j;
byte nibbletosend[3];
for (j = 3; j >= 0; j--) {
if (decimaltosend & (1 << j)) {
nibbletosend[j] = 1;
}
else {
nibbletosend[j] = 0;
}
}
sendBit(2); //start bit
for (int i =3; i >= 0; i--) {
//Serial.print(nibbletosend[i]);
sendBit(nibbletosend[i]);
}
sendBit(3); //stop bit
}
38khz 142.c (3.97 KB)