/* * Task 3 - ADC * * Read an analog voltage from a potentiometer and use the result to flash an ADC. * * Code adapted from Analog Input and Read Analog Voltage examples. * */ // LED connection #define LED_PIN 2 // ADC connection #define ADC_PIN A0 // Voltage reference value #define MAX_VOLTAGE 5.0 // the setup routine runs once when you press reset: void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); // Set up the ADC reference voltage // DEFAULT uses the USB 5V power as a reference // The other possibility for a Nano is INTERNAL which is a 1.1V bandgap reference analogReference(DEFAULT); // Don't actually neeed this line to specify default // Pin to control the LED pinMode(LED_PIN, OUTPUT); digitalWrite(LED_PIN, LOW); // Start with LED off } // the loop routine runs over and over again forever: void loop() { // read the input on analog pin 0 (range: 0-1023) int sensorValue = analogRead(A0); // Convert this to a voltage, scaled by Vmax float voltage = sensorValue * (MAX_VOLTAGE/1023.0); // Print this value and conversion to Volts to the Serial port Serial.print(sensorValue); Serial.print(" -> "); Serial.print(voltage); Serial.println(" V"); // Now have some fun. Light up our LED based on the ADC reading digitalWrite(LED_PIN, HIGH); delay(sensorValue/8); digitalWrite(LED_PIN, LOW); delay(sensorValue/8); }