Lab 3

carlos.sandoval's picture

Turns on/off and adjusts brigthness of 3 LEDs

 

/*

 * AnalogInput

 * by DojoDave <http://www.0j0.org>

 *

 * Turns on/off and adjusts brigthness of 3 LEDs connected to digital 

 * pins 9,10, and 11. The amount of time the LED will be on/off and the brigthness depends 

 * on the value obtained by analogRead(). Pots are connected to analog pins 1 and 2.

 *

 */

// select input pin for potentiometers

int potPin = 2;    // select the input pin for the potentiometer controlling blinking

int potPin1 = 1;    // select the input pin for the potentiometer controlling brightness

 

// select pin for LEDs

int ledPin = 11;   // select the pin for the LED1

int ledPin2 = 10;   // select the pin for the LED2

int ledPin3 = 9;   // select the pin for the LED3

 

int val = 0;       // variable to store the value coming from the sensor 

int valA = 0;      // variable to store the brightness value coming from the sensor

 

void setup() {

  pinMode(ledPin, OUTPUT);  // declare the ledPin as an OUTPUT

  pinMode(ledPin2, OUTPUT);  // declare the ledPin2 as an OUTPUT

  pinMode(ledPin3, OUTPUT);  // declare the ledPin3 as an OUTPUT

  Serial.begin(9600);

 

}

void loop() {

  val = analogRead(potPin);    // read the value from the sensor

  digitalWrite(ledPin, HIGH);  // turn the ledPin on

  delay(val);                  // stop the program for some time

  digitalWrite(ledPin, LOW);   // turn the ledPin off

  delay(val);    // stop the program for some time

  valA = analogRead(potPin1);    // read the value from the sensor, between 0 - 1024

  Serial.println(valA);

  analogWrite(ledPin, valA/4); // analogWrite can be between 0-255

 

  digitalWrite(ledPin2, HIGH);  // turn the ledPin on

  delay(val);                  // stop the program for some time

  digitalWrite(ledPin2, LOW);   // turn the ledPin off

  delay(val);                  // stop the program for some time

  valA = analogRead(potPin1);    // read the value from the sensor, between 0 - 1024

  Serial.println(valA);

  analogWrite(ledPin2, valA/4); // analogWrite can be between 0-255

 

  digitalWrite(ledPin3, HIGH);  // turn the ledPin on

  delay(val);                  // stop the program for some time

  digitalWrite(ledPin3, LOW);   // turn the ledPin off

  delay(val);                  // stop the program for some time

  valA = analogRead(potPin1);    // read the value from the sensor, between 0 - 1024

  Serial.println(valA);

  analogWrite(ledPin3, valA/4); // analogWrite can be between 0-255

}

1.jpg
0
Your rating: None