//#include //SoftwareSerial serial (5, 1); #include int i; unsigned int x, y; float accum, fout, fval = 0.001; // these are variables for a simple low-pass (smoothing) filter - fval of 1 = no filter - .001 = max filter long threshold = 0; int main() { //serial.begin(9600); // sender PA7 // receiver PB2 // pot PB0 DDRA = 0b10000000; DDRB = 0b00000000; PORTA = 0b00000000; PORTB = 0b00000001; // Threshold calibration for (short i = 0; i < 10; i++) { long max; y = 0; x = 0; for (short i = 0; i < 4; i++) { // LOW-to-HIGH transition PORTA |= 0b10000000; // Sender HIGH while ((PINB & 0b0000100) != 0b00000100 ) { // loop while receiver is LOW x++; } delay(1); // HIGH-to-LOW transition PORTA &= 0b01111111; // Sender LOW while((PINB & 0b00000100) != 0b00000000 ) { // loop while receiver is HIGH y++; } delay(1); } max = (x > y ? x : y); threshold = (threshold > max ? threshold : max); } /* Serial.print("T: "); Serial.println(threshold); Serial.print('\n'); */ while (true) { y = 0; x = 0; for (short i = 0; i < 4; i++) { // LOW-to-HIGH transition PORTA |= 0b10000000; // Sender HIGH while ((PINB & 0b00000100) != 0b00000100 ) { // loop while receiver is LOW x++; } delay(1); // HIGH-to-LOW transition PORTA &= 0b01111111; // Sender LOW while((PINB & 0b00000100) != 0b00000000 ) { // loop while receiver is HIGH y++; } delay(1); fout = (fval * (float)x) + ((1-fval) * accum); // Easy smoothing filter "fval" determines amount of new data in fout accum = fout; } /* // Check on threshold if (x > threshold && y > threshold) { serial.println("A"); } else { serial.println("!A"); } serial.print(x); serial.print(" - "); serial.println(y); delay(1500); if ( (PINB & 0b00000001) != 0b00000001 ) { serial.println("P"); } else { serial.println("!P"); } } */ } }