/* Analog Input to LED and to Serial Demonstrates analog input by reading an analog sensor (LDR) 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 LDR_PIN = A2; // select the input pin for the LDR const int LED_PIN = 7; // select the pin for the LED const int BUTTON_PIN = 3; // the number of the pushbutton pin int sensorValue = 0; // variable to store the value coming from the sensor int ledState = LOW; // ledState used to set the LED int buttonPushCounter = 0; // counter for the number of button presses int buttonState = 0; // current state of the button int lastButtonState = 0; // previous state of the button int interval = 1000; // variable for delay to turn led on/off unsigned long previousMillis = 0; // will store last time LED was updated 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 LDR pin as an INPUT pinMode(LDR_PIN, INPUT); //configure pin 3 as an input and enable the internal pull-up resistor pinMode(BUTTON_PIN, INPUT_PULLUP); } void loop() { // read the state of the pushbutton value: buttonState = digitalRead(BUTTON_PIN); // compare the buttonState to its previous state if (buttonState != lastButtonState) { // if the state has changed, increment the counter if (buttonState == HIGH) { // if the current state is HIGH then the button went from off to on: // increase the button counter but stop at the max states of 2 buttonPushCounter = (buttonPushCounter + 1 ) % 2; Serial.print("number of button pushes: "); Serial.println(buttonPushCounter); // Give feedfack to the user via the LED digitalWrite(LED_PIN, LOW); delay(50); digitalWrite(LED_PIN, HIGH); delay(50); digitalWrite(LED_PIN, LOW); delay(50); digitalWrite(LED_PIN, ledState); } else { // if the current state is LOW then the button went from on to off: // Serial.println("off"); } } // save the current state as the last state, for next time through the loop lastButtonState = buttonState; // read the value from the sensor: sensorValue = analogRead(LDR_PIN); // map the value to a broader range to compensate for small difference in the LDR // sensorValue = map(sensorValue, 900, 1023, 0, 1023); // //Send the value over the Serial line to the computer // Serial.println(sensorValue); if(buttonPushCounter == 1){ //Store sensor value as interval for LED interval = sensorValue; }else{ // use default LED interval interval = 500; } // check to see if it's time to blink the LED; that is, if the difference // between the current time and last time you blinked the LED is bigger than // the interval at which you want to blink the LED. unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { // save the last time you blinked the LED previousMillis = currentMillis; // if the LED is off turn it on and vice-versa: if (ledState == LOW) { ledState = HIGH; } else { ledState = LOW; } // set the LED with the ledState of the variable: digitalWrite(LED_PIN, ledState); } }