Week 2+3 Circuit

For this week’s assignment, I decided to make a circuit with three LED’s that change how they behave depending on a potentiometer and a switch. All the three LED’s never do the same thing. Out of the three, one is off, one is on, and one blinks. Which LED does what depends on the potentiometer.

I divided the potentiometers frequency into three parts. I mapped the frequency recorded from the potentiometer from 0 to 225. When the potentiometer’s frequency is between 1 and 75, the red LED is on, the blue LED blinks, and the yellow LED is off. However, when the switch is pressed it changes to red LED is off, the blue LED is on, and the yellow LED is blinking. When the potentiometer’s frequency is between 75 and 150, the red LED is off, the blue LED is on, and the yellow LED is blinking. However, when the switch is pressed it changes to red LED is blinking, the blue LED is off, and the yellow LED is on. A similar change happens when the potentiometer’s frequency is between 150 and 225 (see video).

One small syntax problem that I faced while coding this was getting nothing when the potentiometer was at it’s maximum and minimum. This was because in my code I had written the command “if (0 < sensorValue && sensorValue < 75)” instead of “if (0 <= sensorValue && sensorValue < 75)”. I had missed the = sign for 0 and I had made the same mistake in the command for the maximum frequency. This would lead ti nothing happening when the potentiometer was at its extremes. However I asked Jack for help and he helped my figure it out and I was able to get all the readings required.

SCHEMATIC:

CODE:

int  pushButton = 2;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode (3, OUTPUT);
  pinMode(pushButton, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  int sensorValue = analogRead(A0);
  sensorValue = map(sensorValue, 0, 1023, 0, 225);
  //Serial.println(sensorValue);
  //delay(100);

  int buttonState = digitalRead(pushButton);
  Serial.println("button state is: ");
  Serial.println(buttonState);
  delay(1);

  if (0 <= sensorValue && sensorValue < 75) {
    if (buttonState == 0) {
      // yellow light off
      digitalWrite(3, LOW);
      // red light on
      digitalWrite(5, HIGH);
      //blue light blinking
      digitalWrite(7, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(1000);              // wait for 2 seconds
      digitalWrite(7, LOW);    // turn the LED off by making the voltage LOW
      delay(1000);
    }
    else if (buttonState = 1) {
      digitalWrite(3, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(200);              // wait for 2 seconds
      digitalWrite(3, LOW);    // turn the LED off by making the voltage LOW
      delay(100);
      //red light off
      digitalWrite(5, LOW);
      //blue light on
      digitalWrite(7, HIGH);
    }
  }

  else if (75 <= sensorValue && sensorValue < 150) {
    if (buttonState == 0) {
      // yellow light blinking
      digitalWrite(3, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(200);              // wait for 2 seconds
      digitalWrite(3, LOW);    // turn the LED off by making the voltage LOW
      delay(100);
      //red light off
      digitalWrite(5, LOW);
      //blue light on
      digitalWrite(7, HIGH);
    }
    else if (buttonState = 1) {
      digitalWrite(3, HIGH);
      //red light blinking
      digitalWrite(5, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(200);                       // wait for a second
      digitalWrite(5, LOW);    // turn the LED off by making the voltage LOW
      delay(100);
      //blue light off
      digitalWrite(7, LOW);
    }

  }
  else if (150 <= sensorValue && sensorValue <= 225) {
    if (buttonState == 0) {
      //yellow light on
      digitalWrite(3, HIGH);
      //red light blinking
      digitalWrite(5, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(100);                       // wait for a second
      digitalWrite(5, LOW);    // turn the LED off by making the voltage LOW
      delay(100);
      //blue light off
      digitalWrite(7, LOW);
    }
    else if (buttonState = 1) {
      // yellow light off
      digitalWrite(3, LOW);
      // red light on
      digitalWrite(5, HIGH);
      //blue light blinking
      digitalWrite(7, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(1000);              // wait for 2 seconds
      digitalWrite(7, LOW);    // turn the LED off by making the voltage LOW
      delay(1000);
    }
  }
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *