| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

analog_vu_metr_blink_w_timer_final

Page history last edited by tilotilo 12 years, 1 month ago

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;

 

 

}}

 

Comments (3)

tilotilo said

at 7:45 pm on Feb 29, 2012

Blink led-timer pin set with const int ledPin, at top. I used AnalogInOutSerial (without serial code), BlinkWithoutDelay, Button, SwitchCase (for the VU Meter). Please test and ask any questions, I'll try my best to help

Sean Lueder said

at 3:59 pm on Mar 1, 2012

Hey tilo, im useing the blink with no delay with a button from your code; however, my button is only causing the led to stop blinking. Have you had this problem with your code?

tilotilo said

at 5:27 pm on Mar 1, 2012

if you wait youll see it seems to go from 1 second to 2-3 minutes (maybe its reading hundreds of HIGH states in the fraction of a second?) I'ml working on getting that to behave better

You don't have permission to comment on this page.