64 lines
1.6 KiB
Arduino
64 lines
1.6 KiB
Arduino
|
/*
|
||
|
Analog input, analog output, serial output
|
||
|
|
||
|
Reads an analog input pin, maps the result to a range from 0 to 255
|
||
|
and uses the result to set the pulsewidth modulation (PWM) of an output pin.
|
||
|
Also prints the results to the serial monitor.
|
||
|
|
||
|
The circuit:
|
||
|
* potentiometer connected to analog pin 0.
|
||
|
Center pin of the potentiometer goes to the analog pin.
|
||
|
side pins of the potentiometer go to +5V and ground
|
||
|
* LED connected from digital pin 9 to ground
|
||
|
|
||
|
created 29 Dec. 2008
|
||
|
modified 9 Apr 2012
|
||
|
by Tom Igoe
|
||
|
|
||
|
This example code is in the public domain.
|
||
|
|
||
|
*/
|
||
|
#define TIME 50
|
||
|
#define NUM 8
|
||
|
// These constants won't change. They're used to give names
|
||
|
// to the pins used:
|
||
|
const int analogInPin[8] = {A0, A1, A2, A3, A7, A6, A5, A4}; // Analog input pin that the potentiometer is attached to
|
||
|
const int dig_control = 13; // Analog output pin that the LED is attached to
|
||
|
int sensorValue[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
|
||
|
|
||
|
void setup() {
|
||
|
Serial.begin(115200);
|
||
|
pinMode(dig_control, OUTPUT);
|
||
|
delay(200);
|
||
|
}
|
||
|
|
||
|
void loop() {
|
||
|
start_sensor();
|
||
|
printall();
|
||
|
delay(NUM*TIME);
|
||
|
}
|
||
|
|
||
|
void start_sensor(){
|
||
|
digitalWrite(dig_control,HIGH);
|
||
|
delay(1);
|
||
|
digitalWrite(dig_control,LOW);
|
||
|
}
|
||
|
|
||
|
void printall(){
|
||
|
Serial.print((int)(NUM*TIME/10));
|
||
|
for (int i=0; i<NUM; i++)
|
||
|
{
|
||
|
//delay(TIME);
|
||
|
sensorValue[i] = analogRead(analogInPin[i])*1.27;
|
||
|
if (sensorValue[i]<10) Serial.print("000");
|
||
|
else if (sensorValue[i]<100) Serial.print("00");
|
||
|
else if (sensorValue[i]<1000) Serial.print("0");
|
||
|
Serial.print(sensorValue[i]);
|
||
|
//Serial.print(',');
|
||
|
//delay(10);
|
||
|
}
|
||
|
Serial.println();
|
||
|
}
|
||
|
|
||
|
|