const int sensorMin = 0; // sensor minimum, discovered through experiment
const int sensorMax = 255;
const int ledPin = 4; // the number of the LED pin
;
// Variables will change:
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated
long interval = 1000; // interval at which to blink (milliseconds)
// These constants won't change. They're used to give names
// to the pins used:
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)
//butttons constant
int buttonState = 0;
void setup() {
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(2, INPUT);
pinMode(3, INPUT);
}
void loop() {
//blink controls
//button one
buttonState = digitalRead(2);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
interval = interval - 10;
}
else {
}
//button two
buttonState = digitalRead(3);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
interval = interval + 10;
}
else {
}
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(ledPin, ledState);
}
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 255);
// change the analog out value:
analogWrite(analogOutPin, outputValue);
int sensorReading = analogRead(A0);
int range = map(sensorReading, sensorMin, sensorMax, 0, 3);
switch (range) {
case 0: // your hand is on the sensor
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
break;
case 1: // your hand is close to the sensor
digitalWrite(6, HIGH); // set the LED on
digitalWrite(7, LOW);
digitalWrite(8, LOW);
break;
case 2: // your hand is a few inches from the sensor
digitalWrite(6, HIGH); // set the LED on
digitalWrite(7, HIGH); // set the LED on
digitalWrite(8, LOW); // set the LED off
break;
case 3: // your hand is nowhere near the sensor
digitalWrite(6, HIGH); // set the LED on
digitalWrite(7, HIGH); // set the LED on
digitalWrite(8, HIGH); // set the LED on
break;
}}