4. November 2010
Der Abstand der Hand zum Distanzmesser bestimmt welches LED leuchtet.#define ANALOGIN A0 #define ANALOGOUT 10 //LED #define OUT1 3 #define OUT2 4 #define OUT3 5 int analogData = 0; int dataArray[] = {0,0,0,0,0}; int newData = 0; int counter = 0; void setup() { pinMode(ANALOGIN, INPUT); pinMode(ANALOGOUT, OUTPUT); pinMode(OUT1, OUTPUT); pinMode(OUT2, OUTPUT); pinMode(OUT3, OUTPUT); Serial.begin(9600); } void loop() { analogData = analogRead(ANALOGIN); makeNewData(); checkState(); analogWrite(ANALOGOUT, newData / 4); Serial.println(analogData); delay(50); } void makeNewData() { dataArray[counter] = analogData; if (counter <= 4) { counter++; } else { counter = 0; } newData = (dataArray[0] + //Datenglättung dataArray[1] + dataArray[2] + dataArray[3] + dataArray[4]) / 5; } void checkState() { //Schwellwert wird ausgelesen if (newData < 50) { digitalWrite(OUT1, HIGH); digitalWrite(OUT2, LOW); digitalWrite(OUT3, LOW); } else if (newData > 50 && newData < 300) { digitalWrite(OUT1, LOW); digitalWrite(OUT2, HIGH); digitalWrite(OUT3, LOW); } else { digitalWrite(OUT1, LOW); digitalWrite(OUT2, LOW); digitalWrite(OUT3, HIGH); } }