Week 3 – Assignment

CONCEPT

For this week’s assignment, we had to get information from at least one analog sensor and at least one digital sensor (switch), and use this information to control at least two LEDs, one in a digital fashion and the other in an analog fashion.

I did a different design than my last assignment. With this design, when you turn the potentiometer you can control how bright the LED will light up. And the switch in my circuit works an input and when you push the button, the LED’s will blink for 10 times back to back.

SCHEMATIC

BEHAVIOR

My potentiometer satisfies the analog sensor part. It controls the LED in an analog way. It is coded as analogRead. My yellow switch satfies the digital sensor part. It is coded as digitalRead, in the loop I had to use analogWrite. I discuss the reason for that in the PROBLEMS section.

PROGRAM

int red = 5;
int blue = 3;
int yellowSwitch = 2;
int blue_thing = A0;
int value;
int pushButton;


void setup() {

  Serial.begin(9600);

  pinMode(3, OUTPUT);//blue
  pinMode(5, OUTPUT);//red
  pinMode(2, INPUT);//yellowswitch

}

void loop() {

  //potentiometer changing the light
  value = analogRead (A0) ;
  value = map(value, 0, 1023, 0, 255);
  analogWrite(3, value);
  analogWrite(5, value);
  //delay(100);

  pushButton = digitalRead(2);

  if (pushButton == 1 ) {

    for (int counter = 10; counter > 0; counter --) {
      Serial.println(counter);
      analogWrite(3, value);
      analogWrite(5, value);

      delay(500);
      analogWrite(3, 0);
      analogWrite(5, 0);

      delay(500);
    } //end of for statement

  } //end of if statement

}//end of loop




PROBLEMS

One problem, I had was I forgot to connect my switch to 5V with a cable. I just connected it to pin 2 but then I was checking on google why it was not working, I realized that I forgot that cable.

Another problem I had when I was coding was I wanted to play with the brightness of my LEDs when they were changing. But I coded as digitalWrite so I was not able to do that. When I changed it to analogWrite, it started working. Also, my delays were too short (50) so I was not able to see the blinking for some reason. When I made it 500 it started working properly.

 

Leave a Reply

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