71 lines
1.7 KiB
Arduino
71 lines
1.7 KiB
Arduino
|
/*
|
||
|
AnalogReadSerial
|
||
|
|
||
|
Reads an analog input on pin 0, prints the result to the Serial Monitor.
|
||
|
Graphical representation is available using Serial Plotter (Tools > Serial Plotter menu).
|
||
|
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
|
||
|
|
||
|
This example code is in the public domain.
|
||
|
|
||
|
http://www.arduino.cc/en/Tutorial/AnalogReadSerial
|
||
|
*/
|
||
|
|
||
|
|
||
|
|
||
|
// the setup routine runs once when you press reset:
|
||
|
void setup() {
|
||
|
// initialize serial communication at 9600 bits per second:
|
||
|
Serial.begin(115200);
|
||
|
pinMode(A0, INPUT);
|
||
|
pinMode(A1, INPUT);
|
||
|
pinMode(A2, INPUT);
|
||
|
}
|
||
|
|
||
|
// the loop routine runs over and over again forever:
|
||
|
byte val = 0;
|
||
|
void loop() {
|
||
|
val=map(measure(A0), 0, 1023, 0, 200);
|
||
|
Serial.write(201);
|
||
|
Serial.write(val);
|
||
|
val=map(measure(A1), 0, 1023, 0, 200);
|
||
|
Serial.write(202);
|
||
|
Serial.write(val);
|
||
|
val=map(measure(A2), 0, 1023, 0, 200);
|
||
|
Serial.write(203);
|
||
|
Serial.write(val);
|
||
|
|
||
|
// Serial.print(analogRead(A0));
|
||
|
// Serial.print(",");
|
||
|
// Serial.print(analogRead(A1));
|
||
|
// Serial.print(",");
|
||
|
// Serial.print(analogRead(A2));
|
||
|
// Serial.print(",");
|
||
|
// Serial.print(analogRead(A3));
|
||
|
// Serial.print(",");
|
||
|
// Serial.print(analogRead(A4));
|
||
|
// Serial.print(",\n");
|
||
|
// delay(100); // delay in between reads for stability
|
||
|
}
|
||
|
|
||
|
int sensorValue = 0;
|
||
|
int distance = 110;
|
||
|
|
||
|
int measure(int sensor)
|
||
|
{
|
||
|
sensorValue = average(sensor);
|
||
|
return sensorValue;
|
||
|
}
|
||
|
|
||
|
#define N 10
|
||
|
|
||
|
int average(int sensor)
|
||
|
{
|
||
|
int value = 0;
|
||
|
for (int i=0; i<N; i++)
|
||
|
{
|
||
|
value = value + analogRead(sensor);
|
||
|
delay(1);
|
||
|
}
|
||
|
return value/N;
|
||
|
}
|