الكود البرمجي
الكود البرمجي للمرسل يتم رفعه على الأردوينو الذي سيرسل البيانات
int ledPin = 13; void setup() { pinMode(ledPin, OUTPUT); Serial.begin(9600); } void loop() { byte input; while (Serial.available() > 0) { input = Serial.read(); digitalWrite(ledPin, HIGH); delay(10); digitalWrite(ledPin, LOW); for (int i = 0; i < 8; i++) { digitalWrite(ledPin, (input & (1 << i)) >> i); delay(100); } digitalWrite(ledPin, LOW); delay(10); } }
الكود البرمجي للمستقبل يتم رفعه على الأردوينو الذي يستقبل بيانات
#define NUM_SAMPLES 10 int sensorPin = A0; // select the input pin for the potentiometer int sensorValue = 0; // variable to store the value coming from the sensor double average; #include <LiquidCrystal.h> //Load Liquid Crystal Library LiquidCrystal lcd(12, 11, 5, 6, 7, 8); void setup() { lcd.begin(16,2); Serial.begin(9600); sensorValue = analogRead(sensorPin); average = sensorValue; } void loop() { // read the value from the sensor: sensorValue = analogRead(sensorPin); boolean high = false; high = isSignalHigh(average, sensorValue); if (high) { byte incoming = 0; delay(10); for (int i = 0; i < 8; i++) { incoming |= ( isSignalHigh(average, analogRead(sensorPin)) << i); delay(100); } lcd.print((char)incoming); //Print measured distance Serial.print((char)incoming); } average = approxRollingAverage(average, sensorValue); } double approxRollingAverage(double avg, double new_sample) { avg -= avg / NUM_SAMPLES; avg += new_sample / NUM_SAMPLES; return avg; } boolean isSignalHigh(double average, double sample) { if (sample - average > 10) return true; return false; }