Lab03

Description

I used Arduino with three LEDs (red, blue and green) and 3 potentiometers. I edited the sketch code to control the red LED’s blinking speed and the brightness of green and blue LED with three different potentiometers .  The result, as shown in the image below, is quite as expected. The blinking speed of the red LED and the brightness of the green and blue LED is controlled by the plot sucessfully.

Components

  • 1 Arduino
  • 3 LED
  • 3 Resistor (220Ω)
  • 1 Breadboard
  • 3 Pots

Code

/*
 Analog Input
 Demonstrates analog input by reading an analog sensor on analog pin 0 and
 turning on and off a light emitting diode(LED) connected to digital pin 13.
 The amount of time the LED will be on and off depends on
 the value obtained by analogRead().

The circuit:
 * Potentiometer attached to analog input 0
 * center pin of the potentiometer to the analog pin
 * one side pin (either one) to ground
 * the other side pin to +5V
 * LED anode (long leg) attached to digital output 13
 * LED cathode (short leg) attached to ground

* Note: because most Arduinos have a built-in LED attached
 to pin 13 on the board, the LED is optional.

Created by David Cuartielles
 modified 20 Sep. 2016
 By Owen Hsiao

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/AnalogInput

*/

int sensorPin = A0; // select the input pin for the potentiometer
 //int bsensorPin = A1; // select the input pin for the potentiometer
 //int ledPin = 13; // select the pin for the LED
 int ledPin = 9;
 //int bledPin = 10;
 //int greenledPin = 11;
 int sensorValue = 0; // variable to store the value coming from the sensor
 //int bsensorValue = 0; // variable to store the value coming from the sensor

int potPin = A1; // Analog input pin that the potentiometer is attached to
 int potValue = 0; // value read from the pot

int potPin2 = A2; // Analog input pin that the potentiometer is attached to
 int pot2Value = 0; // value read from the pot
 int led = 10; // PWM pin that the LED is on. n.b. PWM 0 is on digital pin 10
 int led2 = 11; // PWM pin that the LED is on. n.b. PWM 0 is on digital pin 11

void setup() {
 // declare the ledPin as an OUTPUT:
 pinMode(ledPin, OUTPUT);
 // declare the bledPin as an OUTPUT:
 // pinMode(bledPin, OUTPUT);

// initialize serial communications at 9600 bps:
 Serial.begin(9600);
 // declare the led pin as an output:
 pinMode(led, OUTPUT);

// initialize serial communications at 9600 bps:
 Serial.begin(9600);
 // declare the led pin as an output:
 pinMode(led2, OUTPUT);
 }

void loop() {
 // read the value from the sensor:
 sensorValue = analogRead(sensorPin);
 // turn the ledPin on
 digitalWrite(ledPin, HIGH);
 // stop the program for <sensorValue> milliseconds:
 delay(sensorValue);
 // turn the ledPin off:
 digitalWrite(ledPin, LOW);
 // stop the program for for <sensorValue> milliseconds:
 delay(sensorValue);

potValue = analogRead(potPin); // read the pot value
 analogWrite(led, potValue/4); // PWM the LED with the pot value (divided by 4 to fit in a byte)
 Serial.println("hello"); // print the pot value back to the debugger pane
 delay(10); // wait 10 milliseconds before the next loop

pot2Value = analogRead(potPin2); // read the pot value
 analogWrite(led2, pot2Value/5); // PWM the LED with the pot value (divided by 4 to fit in a byte)
 Serial.println("hello"); // print the pot value back to the debugger pane
 delay(10); // wait 10 milliseconds before the next loop
 }

IMAG1134_BURST002 IMAG1135_BURST001

Leave a Reply