Assignment 3: Potentiometers

Assignment: Sensing: Potentiometers

Collaborators:

Description

Use two potentiometers to control blinking speed and brightness. For the optional part, I made one pot control blinking speed and one to control a cycle of colors.

Components Used

  • 3  LEDs
  • 3 220 Resistors
  • Diffuser (opaque white bottle stuffed with tissue)
  • 2 Potentiometers

Arduino Code

For the first part, I used the code for 2 pots on the course website. For the optional portion, I used this code:

/* * Modification of the blinking and fading program from class. * Here, one pot controls blinking, while the other cycles * through colors. */ int colorPot = 0; int blinkPot = 1; int colorPotVal = 0; int blinkPotVal = 0; int redPin = 9; int greenPin = 10; int bluePin = 11; int redVal = 255; int greenVal = 1; int blueVal = 1; void setup() { pinMode(redPin, OUTPUT); pinMode(greenPin, OUTPUT); pinMode(bluePin, OUTPUT); } void loop() { colorPotVal = analogRead(colorPot); // read the value from pot 1, between 0 - 1024, for dimming blinkPotVal = analogRead(blinkPot); // read the value from pot 2, between 0 - 1024, for blinking displayColor(colorPotVal); delay(blinkPotVal); // stop the program for some time, meaning, LED is on for this time blinkOff(); delay(blinkPotVal); // stop the program for some time, meaning, LED is OFF for this time } void displayColor(int colorPotVal) { if (colorPotVal < 256) { redVal = 255 - colorPotVal; greenVal = colorPotVal; analogWrite(redPin, redVal); analogWrite(greenPin, greenVal); } else if (colorPotVal < 512) { greenVal = 511 - colorPotVal; blueVal = colorPotVal - 256; analogWrite(greenPin, greenVal); analogWrite(bluePin, blueVal); } else if (colorPotVal < 768) { blueVal = 767 - colorPotVal; redVal = colorPotVal - 512; analogWrite(bluePin, blueVal); analogWrite(redPin, redVal); } else if (colorPotVal < 1024) { greenVal = 1023 - colorPotVal; blueVal = colorPotVal - 768; analogWrite(redPin, 255); analogWrite(greenPin, greenVal); analogWrite(bluePin, blueVal); } } // turns off all the LEDs void blinkOff() { analogWrite(redPin, 0); analogWrite(greenPin, 0); analogWrite(bluePin, 0); }

Item

Video showing one pot controlling color cycle: