Willkommen auf unserem Seminar-Blog

Immer auf dem aktuellen Stand bleiben

Dieser Seminar-Blog befindet sich noch im Aufbau und wird in den kommenden Tagen entsprechend verfeinert.

Member Login

Lost your password?

Registration is closed

Sorry, you are not allowed to register by yourself on this site!

You must either be invited by one of our team member or request an invitation by email at viad.info {at} zhdk {dot} ch.

Analog In, Datenglättung, Schwellenwert

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);

 }

}