Week 9 Assignment : Color Dial

Concept: After seeing what my peers had already done with the assignment guidelines, I wanted to try something different that I hadn’t seen before. Initially, I had the idea of using a color-changing crossroads with an ultrasonic proximity sensor. However, since someone had already done that, I attempted to replicate it using a potentiometer instead. The prototype includes a button that turns on an LED light, and the potentiometer determines the color.

Prototype: During the prototyping phase, I tried to find the most efficient way to minimize the amount of wiring for the three LEDs I wanted. However, I realized that in order to have different LEDs light up for different scenarios, I needed to create separate digital output circuits.

To visualize this, I mapped out the design on TinkerCad, as shown in the following image:

After completing the circuit, I proceeded to the coding part. It took me some trial and error to create a nested loop that worked with the button and potentiometer setup I desired. Since the potentiometer values range from 0 to 1023, I implemented if-else statements for the Red, Yellow, and Green colors based on approximate ranges of 0-300, 300-700, and 700-1000, respectively.

The following is the code:

int buttonState = 0;  // variable for reading the pushbutton status


// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  pinMode(10, OUTPUT); //Green
  pinMode(11, OUTPUT); //Yellow
  pinMode(12, OUTPUT); //Red
  pinMode(3, INPUT_PULLUP); //Button
}

// the loop routine runs over and over again forever:
void loop() {

  buttonState = digitalRead(3);


  int sensorValue = analogRead(A2);
  Serial.println(sensorValue);

if (buttonState == LOW) {
  if (sensorValue < 300) {
    digitalWrite(12, HIGH);
    digitalWrite(11, LOW);
    digitalWrite(10, LOW);

  } else if (sensorValue < 700) {
    digitalWrite(12, LOW);
    digitalWrite(11, HIGH);
    digitalWrite(10, LOW);

  } else if (sensorValue < 1023){
    digitalWrite(12, LOW);
    digitalWrite(11, LOW);
    digitalWrite(10, HIGH);
  }
} else if (buttonState == HIGH) {
   digitalWrite(12, LOW);
    digitalWrite(11, LOW);
    digitalWrite(10, LOW);
}

  delay(30);  // delay in between reads for stability
}

 

Leave a Reply