Description
For the normal lab requirement, I had one rotating control for brightness and one rotating control for blinking rate. I mapped these controls using soldered potentiometers.
For extra credit, I wanted to improve upon my previous piano/forte Lab 2. I mapped one pot to a variable called dynamic, which when rotated would switch from ‘p’ mode to ‘mp mode’ to ‘f’ mode. The other pot mapped to the blinking rate.
Materials
Code (Lab Requirement)
int sensorPin = A0;
int sensorPin2 = A1; // select the input pin for the potentiometer
int ledPin = 11; // select the pin for the LED
int ledPin2 = 10;
int ledPin3 = 9;
int sensorValue = 0; // variable to store the value coming from the sensor
int sensorValue2 = 0;
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(sensorPin, INPUT);
pinMode(sensorPin2, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
Serial.begin(9600);
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
sensorValue2 = analogRead(sensorPin2);
Serial.print(sensorValue);
Serial.print(",");
Serial.print(sensorValue2);
Serial.println("");
// turn the ledPin on
analogWrite(ledPin, (sensorValue2 / 4));
analogWrite(ledPin2, (sensorValue2 / 4));
analogWrite(ledPin3, (sensorValue2 / 4));
// stop the program for milliseconds:
delay(sensorValue);
analogWrite(ledPin, 0);
analogWrite(ledPin2, 0);
analogWrite(ledPin3, 0);
// stop the program for milliseconds:
delay(sensorValue);
}
Code (Extra Credit)
// Output
int sensorPin = A0;
int sensorPin2 = A1; // select the input pin for the potentiometer
int greenPin = 9; // Red LED, connected to digital pin 9
int bluePin = 10; // Green LED, connected to digital pin 10
int redPin = 11; // Blue LED, connected to digital pin 11
int dynamic = 0;
// Program variables
int blueVal = 0; // Variables to store the values to send to the pins
int greenVal = 0 ; // Initial values are Red full, Green and Blue off
int redVal = 0;
int repeat = 0;
int i = 0; // Loop counter
int wait = 25; // 50ms (.05 second) delay; shorten for faster fades
//int DEBUG = 0; // DEBUG counter; if set to 1, will write values back via serial
void setup()
{
pinMode(sensorPin, INPUT);
pinMode(sensorPin2, INPUT);
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
Serial.println("rotate right or left to go more p or more f");
}
// Main program
void loop()
{
dynamic = analogRead(sensorPin);
wait = analogRead(sensorPin2);
if (dynamic < 300) {
redVal = 0;
greenVal = 0;
blueVal = 0;
i = 0;
for (; i <50; i++) {
Serial.print("dynamic ");
Serial.print(dynamic);
Serial.print(" wait ");
Serial.print(wait/8);
Serial.println();
blueVal += 1 ;
Serial.println(dynamic);
analogWrite(redPin, redVal);
analogWrite(greenPin, greenVal);
analogWrite(bluePin, blueVal);
delay(wait/8);
}
for (; i <100;i++) {
blueVal -= 1;
analogWrite(redPin, redVal);
analogWrite(greenPin, greenVal);
analogWrite(bluePin, blueVal);
delay(wait/8);
}
}
else if (dynamic < 700) {
redVal = 0;
greenVal = 0;
blueVal = 0;
i = 0;
for (; i <50; i++) {
redVal += 2;
greenVal += 1;
blueVal = 1;
Serial.print("dynamic ");
Serial.print(dynamic);
Serial.print(" wait ");
Serial.print(wait/8);
Serial.println();
analogWrite(redPin, redVal);
analogWrite(greenPin, greenVal);
analogWrite(bluePin, blueVal);
delay(wait/8);
}
for (; i <100;i++) {
greenVal -= 1;
redVal -= 2;
analogWrite(redPin, redVal);
analogWrite(greenPin, greenVal);
analogWrite(bluePin, blueVal);
delay(wait/8);
}
redVal = 0;
}
else if (dynamic < 1050) {
redVal = 255;
greenVal = 0;
blueVal = 0;
i = 0;
for (; i <50; i++) {
Serial.print("dynamic ");
Serial.print(dynamic);
Serial.print(" wait ");
Serial.print(wait/8);
Serial.println();
redVal -= 3;
greenVal = greenVal;
blueVal += 1;
Serial.println(dynamic);
analogWrite(redPin, redVal);
analogWrite(greenPin, greenVal);
analogWrite(bluePin, blueVal);
delay(wait/8);
}
for (; i <100;i++) {
redVal = redVal;
greenVal += 2;
blueVal = blueVal;
analogWrite(redPin, redVal);
analogWrite(greenPin, greenVal);
analogWrite(bluePin, blueVal);
delay(wait/8);
}
for (; i <150;i++) {
redVal += 3;
greenVal -= 1;
blueVal -= 1;
analogWrite(redPin, redVal);
analogWrite(greenPin, greenVal);
analogWrite(bluePin, blueVal);
delay(wait/8);
}
redVal = 0;
greenVal = 0;
blueVal = 0;
analogWrite(redPin, redVal);
analogWrite(greenPin, greenVal);
analogWrite(bluePin, blueVal);
}
else {
}
}
This is what happens when dynamic is set to the highest and wait is set to almost the lowest. The color crescendo is really fast.