/* Analog Input to LED and to Serial Demonstrates analog input by reading an analog sensor (PHOTOTRANSISTOR) on analog pin 0, sending its value over Software Serial and turning on and off a light emitting diode(LED) connected to digital pin 7. The amount of time the LED will be on and off depends on the value obtained by analogRead(). This code is optimised for usage with the ATTiny44A created by David Cuartielles modified 30 Aug 2011 By Tom Igoe modified 12 Mar 2019 by Joey van der Bie This example code is in the public domain. http://www.arduino.cc/en/Tutorial/AnalogInput */ #include #define RX 1 #define TX 0 const int PHOTOTRANSISTOR_PIN = A2; // select the input pin for the PHOTOTRANSISTOR const int LED_PIN = 7; // select the pin for the LED int sensorValue = 0; // variable to store the value coming from the sensor SoftwareSerial Serial(RX, TX); void setup() { //Start serial Serial.begin(9600); Serial.println("Initializing..."); // declare the LED pin as an OUTPUT: pinMode(LED_PIN, OUTPUT); // declare the PHOTOTRANSISTOR pin as an INPUT pinMode(PHOTOTRANSISTOR_PIN, INPUT); } void loop() { // read the value from the sensor: sensorValue = analogRead(PHOTOTRANSISTOR_PIN); // map the value to a broader range to compensate for small difference in the PHOTOTRANSISTOR // sensorValue = map(sensorValue, 900, 1023, 0, 1023); //Send the value over the Serial line to the computer Serial.println(sensorValue); // turn the ledPin on digitalWrite(LED_PIN, HIGH); // stop the program for milliseconds: delay(sensorValue); // turn the ledPin off: digitalWrite(LED_PIN, LOW); // stop the program for for milliseconds: delay(sensorValue); }