Day/Night Arduino Simulation

For probably the first time since we started the semester, I was able to do a weekly assignment just as I planned it initially. As soon as we were assigned to include some analog sensor in our circuit and a digital one, I thought of the photocell (because it’s just so cool) and some application of it where the environment is dark and then is bright. I thought of having three bulbs, red, yellow, and green and as you get closer with your finger it changes from red to yellow to green. Then I thought of drawing a rabbit and a turtle and having bulbs as their eyes; if you get too close then the rabbit’s eyes brighten up, but if you are too far (bright environment)  the turtle’s eyes brighten up. But after a long time brainstorming, I decided to go with the most intuitive idea relating to darkness and light: day and night in real life.

Artistic (not so much) steps:

As visible, I draw small icons of the sun and the moon in order to represent day and night, where the yellow bulb would sit in the middle of the sun, and the white bulb will sit in the middle of the white crescent.

Process:

Learning from last assignment, I extrapolated the power and the ground to the rest of the board using two jumper wires, in order to give myself extra space to work with.

I put a button the middle as some kind of digital switch for the whole simulation, if you click once, it’s on and stays on until you click once again.

I put both bulbs in pins with the “~” sign next to them so that their pulse can be changed using the photocell readings.

I decided to make the simulation so that the yellow bulb is very bright (bright sun at the beginning of the day), and as you get your finger closer (as time passes throughout the day and light lessens) the brightness decreases until at one point it stops completely (the evening), and as you continue to get closer, the white bulb of the moon becomes brighter and bright (through the night), until it’s maximized when you physically touch the photocell and block almost all light (midnight), then as you move your finger back upwards the opposite happens (after midnight until dusk). The key is that I map the first values until half of the maximum reading of photocell into inverted values for the bulb so I would get that effect of a spectrum.

Struggles:

The only problem I faced was with the if conditions and digital/analog writing bearing in mind that I always do analogRead(); if I only had analogwrite() then the bulb would keep being bright, and of it’s only digitalWrite() then the bulb doesn’t turn on at all. I’m sure I had some errors in there which I was not able to deal with. So I decided to have two scenarios, if the simulation is on, read photocell and analogWrite() based on that, and if simulation is off then digitalWrite() the bulbs to be off.

Also, when I forgot to make “prevButtonState = buttonState” at the very end and rather had it inside the if statement, everything would work incorrectly, so that was another thing I learned.

The last issue was just that I had a non-working jumper wire, and a bulb that was probably not 100% functional, which caused some confusion for me at first but soon I realized the issue and resolved it.

Final Project Angle 1:

Final Project Angle 2:

The code for the assignment:

int button = 2;                 //button pin

int ledWhite = 3;               //white bulb pin
int ledState1;                  
int ledYellow = 5;              //yellow bulb pin
int ledState2
bool prevButtonState = LOW;     //initializing button to be unpressed

int knob = A0;                  //pin of photocell

void setup() {
 
  pinMode(button, INPUT);         //button initialized as input
  pinMode(ledWhite, OUTPUT);      //bulbs initialized as outputs
  pinMode(ledYellow, OUTPUT);
  
  Serial.begin(9600);
}

void loop() {
  int buttonState = digitalRead(button);              //storing reading from button
  Serial.println(buttonState);

  if (buttonState == HIGH && prevButtonState == LOW) {      //if button is clicked change the led states to their opposites
    ledState1 = !ledState1;
    ledState2 = !ledState2;
    }

  if (ledState1 == HIGH && ledState2 == HIGH) {           //if the bulbs were previously off then were turned on by button
      int knobValue = analogRead(knob);                   //store reading from photocell
      int mappedValue1 = constrain( map(knobValue, 596, 850, 0, 255), 0, 255);        //map the values from half the max until the max of the photocell readings into 0 -> 255 so that as they increase teh brightness increases, and constrain that within 0 to 255
      int mappedValue2 = constrain( map(knobValue, 340, 595, 255, 0), 0, 255);        //map the first values until half the max of photocell readings into 255 -> 0 so that as they increase the brightness decreases, and constrain that within 0 to 255
      
      
      Serial.println(knobValue);

    analogWrite(ledYellow, mappedValue1);             //change output of sun bulb accordingly
    analogWrite(ledWhite, mappedValue2);              //change output of moon bulb accordingly
    }
    
  else {
    digitalWrite(ledWhite, ledState2);              //make bulbs's state = LOW, hence turned off
    digitalWrite(ledYellow, ledState1);             //make bulb's state = LOW, hence turned off
    }

  prevButtonState = buttonState;                    //update prevButtonState in each loop
}

 

 

Week 8 Assignment: Analog Input and output, Mapping, data types, printing

INITIAL IDEA #1

For this week’s assignment, I originally thought of an idea where I would have different rows of LED lights on the breadboard along with a switch and a knob on a different row.

Whenever the switch is pressed, it will generate a random set of pattern or sequence that will light up different LED lights that will also be blinking on the breadboard. On the other hand, the knob will be able to control the blinking speed of those LED lights that are lit up.

IDEA #1.1

However, after thinking about the level of difficulty to make a total of 9 LED lights to light up in different patterns, I decided to decrease the amount of lights to just three. Also, I had changed my idea to not make the lights blink and to make 3 other LED lights to be controlled by the knob separately from the switch.

Initial Ideas for Setup
The LED Light patternsFinal Setup

Main Difficulties 

One of the major difficulties regarding the programming process for switching of LED light patterns was whenever I clicked the button, it does not switch to any of the other designated LED pattern. But, after a bit of debugging, I realized that I have to manually turn off all the LED lights everytime  the button starts to press and randomly generate a LED light pattern again.

One of the major difficulties regarding the programming behind making the LED lights dim or become brighter based on the knob is figuring out the entire logic behind it. I had to consult from Aaron regarding this problem and we had to search for an entire bell curve equation first, and figure out how to convert that into Arduino.

FINAL PRODUCT 

 

Interesting Thing…

I just found out that if I comment out one of my functions, the knobturn function, and leave the LEDfade 1,2,and 3 in the loop, it creates another amazing effect as originally in the knobturn function, I assigned every LED light with the brightness integer. However, I assigned it separately for each LED light (LEDfade1, LEDfade2, LEDfade3), therefore creating a fade effect.

MY CODE 

const int button = 2;

//digital LED lights
const int RedLed = 4;
const int YellowLed = 7;
const int BlueLed = 8;

//analog LED lights
const int RedLed1 = 3;
const int YellowLed1 = 5;
const int BlueLed1 = 6;

int RedLedState = LOW;
int YellowLedState = LOW;
int BlueLedState = LOW;

const int knob = A0;

int prevButtonState = LOW;
int randomNum;

//change spread to make bell curve wider or narrower
int spread = 60;

// set the postion to start above the screen so the first circle is dark
int bellCurveCenter = -spread;
//rewrite the array form in arduino
int ledPos [] = {
  160, 320, 480
};
int ledPosCount = 3; // the number of pins (i.e. the length of the array)
int ledIndex [] = {3, 5, 6};

void setup() {
  // put your setup code here, to run once:
  pinMode(button, INPUT);
  pinMode(knob, INPUT);

  pinMode(RedLed, OUTPUT);
  pinMode(YellowLed, OUTPUT);
  pinMode(BlueLed, OUTPUT);

  ////  ledPos = new int[3];
  //  // three different heights to put three different circle on screen
  //  ledPins[0] = 640/4;
  //  ledPins[1] = 640/2;
  //  ledPins[2] = 640 - 640/4;

  Serial.begin(9600);
}
//light up all three lights altogether first
//Each time the button's pressed, choose a random number (1-4)
//Light up the pattern that the chosen number corresponds to:
// If the number is 1, light up Green and Yellow
// If the number is 2, light up Green and Blue
// If the number is 3, light up Blue and Yellow
// If the number is 4, light up Green and Yellow and Blue
void loop() {
  buttonpress();
  LEDpattern();
  knobturn();
  LEDfade1();
  LEDfade2();
  LEDfade3();
}



void buttonpress() {

  int currentbuttonstate = digitalRead(button); //read current state of button
  if (currentbuttonstate == HIGH && prevButtonState == LOW) {
    randomNum = random(1, 5); //pick random number between 1 to 4
    Serial.println(randomNum);
  }
  prevButtonState = currentbuttonstate;
}

void LEDpattern() {
  //turn all LED lights offf irst
  digitalWrite(RedLed, LOW);
  digitalWrite(YellowLed, LOW);
  digitalWrite(BlueLed, LOW);

  if (randomNum == 1) {
    digitalWrite(RedLed, HIGH);
    digitalWrite(YellowLed, HIGH);
  }
  if (randomNum == 2) {
    digitalWrite(RedLed, HIGH);
    digitalWrite(BlueLed, HIGH);
  }
  if (randomNum == 3) {
    digitalWrite(YellowLed, HIGH);
    digitalWrite(BlueLed, HIGH);
  }
  if (randomNum == 4) {
    digitalWrite(RedLed, HIGH);
    digitalWrite(YellowLed, HIGH);
    digitalWrite(BlueLed, HIGH);
  }
}

void knobturn() {
  for (int thisPin = 3; thisPin < 7; thisPin++) {
    //taking the position of the circle and finding the distance of each LED from the center of our bell curve
    float distance = abs(ledPos[thisPin] - bellCurveCenter);
    // this is the formula for the bell curve, multiply by 255 to put in the proper range for brightness
    float brightness = exp(-0.5 * pow(distance / spread, 2.)) * 255;
    analogWrite(ledIndex[0], brightness);
    analogWrite(ledIndex[1], brightness);
    analogWrite(ledIndex[2], brightness);
  }
}

void LEDfade1() {
  int knobvalue = analogRead(knob);
  Serial.print(knobvalue);

  //might need to add an int before map, but test it out first
  bellCurveCenter = map(knobvalue, 0, 1023, -200, 640 + spread);

  //  for (int thisPin = 3; thisPin < 7; thisPin++) {
  //taking the position of the circle and finding the distance of each LED from the center of our bell curve
  float distance = abs(ledPos[0] - bellCurveCenter);
  // this is the formula for the bell curve, multiply by 255 to put in the proper range for brightness
  float brightness = exp(-0.5 * pow(distance / spread, 2.)) * 255;
  analogWrite(RedLed1, brightness);
}

void LEDfade2() {
  int knobvalue = analogRead(knob);
  Serial.print(knobvalue);

  //might need to add an int before map, but test it out first
  bellCurveCenter = map(knobvalue, 0, 1023, -200, 640 + spread);
  float distance = abs(ledPos[1] - bellCurveCenter);
  float brightness = exp(-0.5 * pow(distance / spread, 2.)) * 255;
  analogWrite(YellowLed1, brightness);
}

void LEDfade3() {
  int knobvalue = analogRead(knob);
  Serial.print(knobvalue);

  //might need to add an int before map, but test it out first
  bellCurveCenter = map(knobvalue, 0, 1023, -200, 640 + spread);
  float distance = abs(ledPos[2] - bellCurveCenter);
  float brightness = exp(-0.5 * pow(distance / spread, 2.)) * 255;
  analogWrite(BlueLed1, brightness);
}


 

Parking Sensor

For this week project I took inspiration from the problem that a lot of people face while parking. This being that it can be really difficult to park close to a wall especially for new drivers. Newer cars are often fitted parking sensors which makes beeping sounds to alert the drivers about there distance from an object. Older cars however do not have this feature so it can often be a challenge to park cars especially for newer drivers.

For my adaptation of the parking sensor I used an ultrasonic sensor to the determine the distance of the sensor to an object in front of it. Then using the equation of a line I determine the rate at which the led should blink. The led will blink with an interval of 800ms if the object is more than 25cm away or it will blink with an interval of 100ms if the object is less than 5cm away. The interval between 5cm and 25cm will be calculated using the equation of the line given below:

interval = 35*distance -75

Parking Sensor Planning Document

A button can be used to put the device in parking mode. When the device goes into parking mode the ultrasonic sensor is activated and sound waves are emitted. The time taken for these sound waves to come back determines the distance. The distance is then used to calculate the interval.

 

 

// echo produces a pulse when reflected signal is received
// the reflected signal is the one that is sent by the trig pin
int trigPin = 10;
int echoPin = 9;
int statusLedPin = 2;
int prevState = LOW;
int button = 4;
int warningPin = 5;
float distance = 10;
bool parking = false;
int delayMS = 0;
int interval = 500;
unsigned long prevTime = 0;
int warningState = LOW;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(statusLedPin, OUTPUT);
  pinMode(button, INPUT);
  pinMode(warningPin, OUTPUT);

}

void loop() {
  changeState();
  parkingState();
  if (parking) {
    distance = getDistance();
    calculateDelay();
    blinkLed();
    //    Serial.println(delayMS);
  }
  if (!parking && warningState == HIGH) {
    warningState = LOW;
    digitalWrite(warningPin, warningState);
  }
}

float getDistance() {
  digitalWrite(trigPin, LOW); // no signal is sent
  delayMicroseconds(2);

  digitalWrite(trigPin, HIGH); // after 2ms a signal is sent
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW); // signal is not sent after 10 ms

  // read the time taken for the pulse to return
  float duration = pulseIn(echoPin, HIGH);

  // calculate the distance
  return duration * 0.034 / 2;
}

void changeState() {
  if (digitalRead(button) == HIGH && prevState == LOW) {
    //    if (parking) {
    //      parking = false;
    //    } else if (!parking) {
    //      parking = true;
    //    }

    //refactor
    parking = !parking;
  }
  prevState = digitalRead(button);
}

void parkingState() {
  if (parking) {
    digitalWrite(statusLedPin, HIGH);
    //    digitalWrite(warningPin, LOW);
  } else if (!parking) {
    digitalWrite(statusLedPin, LOW);
    //    digitalWrite(warningPin, HIGH);
  }
}

void calculateDelay() {
  if (distance >= 5 && distance <= 25) {
    delayMS = (int) 35 * distance - 75;
  } else if (distance < 5) {
    delayMS = 100;
  } else if (distance > 25) {
    delayMS = 800;
  }
}

void blinkLed() {
  Serial.println("calling");
  unsigned long currTime = millis();
  if (currTime - prevTime >= delayMS) {
    prevTime = currTime;
    if (warningState == LOW) {
      warningState = HIGH;
    } else {
      warningState = LOW;
    }
    digitalWrite(warningPin, warningState);
  }
}

Circuit Diagram

Light Dimmer

Concept:

  • Replicate the dimming effect from film lights, and the dimmers attached to them that have a slow smooth transition.
const int led1 = 13;
const int led2 = 12;
const int led3 = 11;
const int led4 = 10;
const int led5 = 9;
const int led6 = 8;
const int led7 = 7;
const int led8 = 6;
const int button = 6;

int potpin = A0;
int potnum = 0; //values from potentiometer
int readValue;
int writeValue;

void setup() {
  // put your setup code here, to run once:
  pinMode(button, INPUT_PULLUP);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(potpin, INPUT);
  Serial.begin(9600);
}

void loop() {
  //analog and leds
  readValue = analogRead(potpin);
  writeValue = (255. / 1023.) * readValue;
  // formula from https://toptechboy.com/lesson-11-arduino-circuit-to-dim-led-with-potentiometer/

  //  bool buttonState = digitalRead(button);
  analogWrite(led1, writeValue);
  analogWrite(led2, writeValue);
  analogWrite(led3, writeValue);
  analogWrite(led4, writeValue);
  potnum = analogRead(potpin);
  Serial.println(potnum);
  //digital button and leds
  // put your main code here, to run repeatedly:

  bool buttonState = digitalRead(button);
  Serial.println(button);
  if (buttonState == HIGH) {
    digitalWrite(led5, LOW);
    digitalWrite(led6, LOW);
    digitalWrite(led7, LOW);
    digitalWrite(led8, LOW);

  } else {

    digitalWrite(led5, HIGH);
    digitalWrite(led6, HIGH);
    digitalWrite(led7, HIGH);
    digitalWrite(led8, HIGH);

  };
}

first attempt

second attempt

 

WK8 – Meera:

int button = 2;
int ledY = 3;
int ledG = 5;
int ledB = 6;
int ledR = 4;
bool ledYState = false;
bool ledGState = false;
bool ledBState = false;
bool ledRState = false;
bool prevButtonState = LOW;
int knob = A0;
void setup() {
  pinMode  ( button, INPUT);
  pinMode ( ledY , OUTPUT);
  pinMode ( ledG, OUTPUT);
  pinMode ( ledB, OUTPUT);
  pinMode ( ledR, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int buttonState = digitalRead(button);
  Serial.println(buttonState);
  digitalWrite( ledY, ledYState);
  digitalWrite( ledG, ledGState);
  digitalWrite( ledB, ledBState);
  digitalWrite( ledR, ledRState);

  if (buttonState == HIGH && prevButtonState == LOW) {
    ledYState = !ledYState;
  }
  if (buttonState == HIGH && prevButtonState == LOW) {
    ledGState = !ledGState;
  }
  if (buttonState == HIGH && prevButtonState == LOW) {
    ledBState = !ledBState;
  }
  if (buttonState == HIGH && prevButtonState == LOW) {
    ledRState = !ledRState;
  }


  prevButtonState = buttonState;

  int knobValue = analogRead ( knob);

  if ( 300 < knobValue) {
    digitalWrite(ledG, HIGH);
    digitalWrite(ledB, HIGH);
    digitalWrite(ledY, LOW);
    digitalWrite(ledR, LOW);
  }
  else {
    digitalWrite(ledG, LOW);
    digitalWrite(ledB, LOW);
  }

  if ( 700 < knobValue) {
    digitalWrite(ledY, HIGH);
    digitalWrite(ledR, HIGH);
    digitalWrite(ledG, LOW);
    digitalWrite(ledB, LOW);
  }
  else {
    digitalWrite(ledY, LOW);
    digitalWrite(ledR, LOW);

  }
  Serial. println ( knobValue);

}

Wk8 : 

Knob alternation : 

MY FINAL TWIST WAS ADDING A HOUSE, I KNOW THE HOUSE DOESNT LOOK GREAT. HOWEVER, I THOUGHT IT GAVE PURPOSE TO THE LED SWITCH. IMAGINE WALKING INTO A HOUSE , TURNING ON THE LIGHT AND HAVING A SWITCH THAT CAN ALTERNATE BETWEEN THE LIGHTS YOU NEED. THIS IS WHAT I TRY TO DISPLAY IN THIS WEEKS ASSIGNMENT.

 

 

 

 

Playing with multiple LEDs using a light sensor and a push button!

I created a project where you can play with both analog input and digital input to activate different actions on the LED lights. To begin with, I have installed a photoresistor so that different number of lights can be turned on depending on the light intensity. With the least bright light switching on the red LED, it can go all the way up until the brightest light switching on the green LED. Another aspect I have added to this is the usage of a push button to initiate a collective blinking reaction. All four lights will blink four times and will not be interfered with the light during this process. The LEDs will go back to reacting to light after this. I initially wanted to use the potentiometer but I wanted to show something others have not already done so I chose to make use of the light sensor.

Here is my code:

int photoresistor = A0;
int yellow = 2;
int blue = 3;
int red = 4;
int green = 5;
int button = 6;


void setup() {
  pinMode(yellow, OUTPUT);
  pinMode(blue,OUTPUT);
  pinMode(red,OUTPUT);
  pinMode(green,OUTPUT);
  pinMode(button, INPUT);
}


void loop() {
  
  int button1 = digitalRead(button);
  if (button1 == HIGH) {
    digitalWrite(yellow, HIGH);
    digitalWrite(blue, HIGH);
    digitalWrite(red, HIGH);
    digitalWrite(green, HIGH);
    delay(2000);
    
    int count = 0;
    while (count < 5){
      digitalWrite(yellow, HIGH);
      digitalWrite(blue, HIGH);
      digitalWrite(red, HIGH);
      digitalWrite(green, HIGH);
      delay(20);                  
      digitalWrite(yellow, LOW);
      digitalWrite(blue, LOW);
      digitalWrite(red, LOW);
      digitalWrite(green, LOW);
      delay(1000); 
      count += 1;
      }
  }
  
  else{
    
    int analogValue = analogRead(photoresistor);
    if(analogValue < 50){            
      digitalWrite(red, HIGH);
    }
    else if(analogValue >= 50 && analogValue < 100){
      digitalWrite(blue, HIGH);
    }
    else if(analogValue >= 100 && analogValue <= 150){
      digitalWrite(yellow, HIGH);
    }
    else{
      digitalWrite(green, HIGH);  
    }
    
    delay(100);
    
    digitalWrite(yellow, LOW);
    digitalWrite(blue, LOW);
    digitalWrite(red, LOW);
    digitalWrite(green, LOW);
  }
}

 

Here is the video of how it works:

 

Week 8: Learning Progress Light

My idea for this week’s exercise is to create a ‘traffic light’ for learning progress where one can track how close one is to finishing an exercise. On turning the know, first a red, then a yellow, and then a green LED lights up. Then one can press the button to mark a task as completed and some ‘light confetti’ (blinking LEDs) show up to congratulate the learner.

In the end, this was not too difficult to create but it took me some time to understand the difference between analog and digital input but now it seems more clear after using analog input as well.

int greenledPin = 5;
int yellowledPin = 4;
int redledPin = 3;
int buttonPin = 2;

void setup() {
  // put your setup code here, to run once:
  pinMode(greenledPin, OUTPUT);
  pinMode(yellowledPin, OUTPUT);
  pinMode(redledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  Serial.begin(9600); //bit mode. 9600 commuting back and forth per minute
}

void loop() {
  // put your main code here, to run repeatedly:

  int knobValue = analogRead(A0);
  
  if (681 < knobValue) {
    digitalWrite(greenledPin, HIGH);
  } else {
    digitalWrite(greenledPin, LOW);
  }
  if (341 < knobValue) {
    digitalWrite(yellowledPin, HIGH);
  } else {
    digitalWrite(yellowledPin, LOW);
  }
  if (100 < knobValue) {
    digitalWrite(redledPin, HIGH);
  } else {
    digitalWrite(redledPin, LOW);
  }

  int buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {
    digitalWrite(redledPin, HIGH);
    digitalWrite(yellowledPin, HIGH);
    digitalWrite(greenledPin, HIGH);
    delay(200);
    digitalWrite(redledPin, LOW);
    digitalWrite(yellowledPin, LOW);
    digitalWrite(greenledPin, LOW);
    delay(200);
    digitalWrite(redledPin, HIGH);
    digitalWrite(yellowledPin, HIGH);
    digitalWrite(greenledPin, HIGH);
    delay(200);
    digitalWrite(redledPin, LOW);
    digitalWrite(yellowledPin, LOW);
    digitalWrite(greenledPin, LOW);
    delay(200);
    digitalWrite(redledPin, HIGH);
    digitalWrite(yellowledPin, HIGH);
    digitalWrite(greenledPin, HIGH);
    delay(200);
    digitalWrite(redledPin, LOW);
    digitalWrite(yellowledPin, LOW);
    digitalWrite(greenledPin, LOW);
    delay(200);
    digitalWrite(redledPin, HIGH);
    digitalWrite(yellowledPin, HIGH);
    digitalWrite(greenledPin, HIGH);
    delay(200);
    digitalWrite(redledPin, LOW);
    digitalWrite(yellowledPin, LOW);
    digitalWrite(greenledPin, LOW);
  }

  Serial.println(buttonState);
}

 

Week8 : Analogue Readers

My first thing to do this week was to brainstorm some ideas. My most prominent one was that I really wanted to play with many lights, almost like a carousel of lights. After noting down some random notes, I decided to aim for a code in which I could have a bunch of lights, and have my analogue reader blend through the lights. In this case I wanted to use the potentiometer, just because it seemed the most appropriate for me.

Setting up the circuit was much smoother than last week for me, I knew what I was doing way more. The first thing i needed to do was get the code to work where the lights would pass through. It came really easy for me, I just mapped the analogue reads from 0 – 800, just because that could make it easily divisible by 8 (my number of lights), without changing the values too much. Then I created a bunch of if-statements on when to light up. When I first did this, it made the leds light up and not shut off. Although this was also a cool effect, it wasnt what I wanted. I figured this out using ‘else statements.’

But that gave me the idea on what to use with a digital reading. I decided that once the button was clicked, the setting would change, and the led’s would turn on one after the other without turning each off as they go. After that, I realised I faced an issue where once they were all on, there would be no way (in that setting to turn them off). While washing my dishes, I kept thinking about how to code a way to fix that, and I found it. I measured the previous analogue reading, if it was higher than the current one that means the pattern was moving down, so I set the LED’s to turn off. If the previous reading was lower, the pattern was moving up, making the LED’s turn on. This meant switching the direction of the potentiometer would change whether you are turning the lights on or off.

Finally, I started adding some small features (such as all the lights turning on at the highest reading, and debugging). Here is the final project ! (I accidentally filmed vertically)

 

int button = 2;
int led1 = 3;
int led2 = 4;
int led3 = 5;
int led4 = 6;
int led5 = 7;
int led6 = 8;
int led7 = 9;
int led8 = 10;
bool buttonState = false; 
bool prevState = false; 
int knob = A0;
int prevLed = 0;


void setup() {
  // put your setup code here, to run once:
  pinMode(button,INPUT);
  pinMode(led1,OUTPUT);
  pinMode(led2,OUTPUT);
  pinMode(led3,OUTPUT);
  pinMode(led4,OUTPUT);
  pinMode(led5,OUTPUT);
  pinMode(led6,OUTPUT);
  pinMode(led7,OUTPUT);
  pinMode(led8,OUTPUT);
  Serial.begin(9600);
  
}

void loop() {
//   put your main code here, to run repeatedly:
  changeState();
  
int knobValue = analogRead(knob);
//mapping the potentiometer values from 0 - 800 for easy reference
int mappedValue =  map(knobValue, 0,1023, 0, 800);

//Button setting 1: where each light only appears individually
if (buttonState == false){
  if (mappedValue >= 0 && mappedValue <100){
    digitalWrite(led1, HIGH);
  }  else {
    digitalWrite(led1, 0);
  }
  if (mappedValue >= 100 && mappedValue <200){
    digitalWrite(led2, HIGH);
  } else {
    digitalWrite(led2, 0);
  }
  if (mappedValue >= 200 && mappedValue <300){
    digitalWrite(led3, mappedValue );
  } else {
    digitalWrite(led3, 0);
  }
  if (mappedValue >= 300 && mappedValue <400){
    digitalWrite(led4, mappedValue );
  }  else {
    digitalWrite(led4, 0);
  }
  if (mappedValue >= 400 && mappedValue <500){
    digitalWrite(led5, mappedValue );
  } else {
    digitalWrite(led5, 0);
  }
  if (mappedValue >= 500 && mappedValue <600){
    digitalWrite(led6, mappedValue );
  } else {
    digitalWrite(led6, 0);
  }
  if (mappedValue >= 600 && mappedValue <700){
    digitalWrite(led7, mappedValue );
  } else {
    digitalWrite(led7, 0);
  }
  if (mappedValue >= 700 && mappedValue <800){
    digitalWrite(led8, mappedValue );
  } else {
    digitalWrite(led8, 0);
  }
  //if the potentiometer is at its max, all lights turn on
    if (mappedValue ==800 ){
      digitalWrite(led1, mappedValue );
      digitalWrite(led2, mappedValue );
      digitalWrite(led3, mappedValue );
      digitalWrite(led4, mappedValue );
      digitalWrite(led5, mappedValue );
      digitalWrite(led6, mappedValue );
      digitalWrite(led7, mappedValue );
      digitalWrite(led8, mappedValue );
    }

  
} 
//button state 2, where the lights turn on and off after one another
if (buttonState == true){
  //if analogue is increasing, they are turning on
  if (prevLed <= mappedValue){
    if (mappedValue >= 0 && mappedValue <100){
      digitalWrite(led1, HIGH);
      prevLed = mappedValue;
    }
    if (mappedValue >= 100 && mappedValue <200){
      digitalWrite(led2, HIGH);
      prevLed = mappedValue;
    }
    if (mappedValue >= 200 && mappedValue <300){
      digitalWrite(led3, mappedValue );
      prevLed = mappedValue;
    } 
    if (mappedValue >= 300 && mappedValue <400){
      digitalWrite(led4, mappedValue );
      prevLed = mappedValue;
    }  
    if (mappedValue >= 400 && mappedValue <500){
      digitalWrite(led5, mappedValue );
      prevLed = mappedValue;
    } 
    if (mappedValue >= 500 && mappedValue <600){
      digitalWrite(led6, mappedValue );
      prevLed = mappedValue;
    } 
    if (mappedValue >= 600 && mappedValue <700){
      digitalWrite(led7, mappedValue );
      prevLed = mappedValue;
    } 
    if (mappedValue >= 700 && mappedValue <800){
      digitalWrite(led8, mappedValue );
      prevLed = mappedValue;
    } 
    //if analogue is decreasing, they are turning off
  } else if (prevLed >= mappedValue){
    if (mappedValue >= 0 && mappedValue <100){
      digitalWrite(led1, 0);
      prevLed = mappedValue;
    }
    if (mappedValue >= 100 && mappedValue <200){
      digitalWrite(led2, 0);
      prevLed = mappedValue;
    }
    if (mappedValue >= 200 && mappedValue <300){
      digitalWrite(led3,0 );
      prevLed = mappedValue;
    } 
    if (mappedValue >= 300 && mappedValue <400){
      digitalWrite(led4, 0);
      prevLed = mappedValue;
    }  
    if (mappedValue >= 400 && mappedValue <500){
      digitalWrite(led5, 0 );
      prevLed = mappedValue;
    } 
    if (mappedValue >= 500 && mappedValue <600){
      digitalWrite(led6, 0 );
      prevLed = mappedValue;
    } 
    if (mappedValue >= 600 && mappedValue <700){
      digitalWrite(led7, 0);
      prevLed = mappedValue;
    } 
    if (mappedValue >= 700 && mappedValue <800){
      digitalWrite(led8, 0 );
      prevLed = mappedValue;
    } 
    }
  }

Serial.print(knobValue);
Serial.print(" ");
Serial.println(mappedValue);
}

//toggling the button state
void changeState() {
  if (digitalRead(button) == HIGH && prevState == LOW) {
    if (buttonState) {
      buttonState= false;
    } else if (!buttonState) {
      buttonState= true;
    }
  }
  prevState = digitalRead(button);
}


 

Analog Input/Output Circuit

For this week, I had to visit a lot of external resources to finally grasp what is analog input/output and how is it different from digital input/output. Things finally started to click in my head after reading this definition on NYU ITP web page:

“While a digital input to a microcontroller can tell you about discrete changes in the physical world, such as whether the cat is on the mat, or the cat is off the mat, there are times when this is not enough. Sometimes you want to know how fat the cat on the mat is.” 

Honestly, why all textbooks/videos/websites can’t explain concepts in this simple manner? 😄

I really like how all of the hardware stuff we are building has applications in real world that I can immediately think of: electricity switches, temperature sensors, etc. etc. Not to say that software isn’t applicable — it is just easier for me to remember engineering applications that we encounter in real life than, say, games.

Anyways, I am a bit digressing here, so let’s get into my homework circuit.

Complete Circuit

⚡️ idea

The idea behind was inspired by the air conditioning and heating system inside different cars. Usually, there is one LED light that turns on if the heater is on and another LED light turns on if the AC is on. So I have used a temperature sensor for the analog input/output communication, two LED bulbs + one button for digital input/output.

🛠 process

I have read quite a bit of documentation and watched some LinkedIn learning videos to finally understand how Arduino analog works.

First off, I almost burned my temperature sensor, xD. After I plugged everything in and ran my script for the first time, I started smelling something weird — and oh boy, it was a temperature sensor! Gladly I was quick to find out and immediately plugged everything out. Turns out I messed up the leg-connection to the 5V and the ground, which has caused this whole trouble.

After a bit of debugging, I was able to connect and read the temperature values. By the way, none of this would have been implemented had I not referred to this documentation on voltage and temperatures. It was super confusing, but apparently we have to manipulate voltage values in order to get the most precise measurements of temperature!

🎥 result

The most fun part was making it all work inside the script:

int analogPin = A0;
float voltageOut;
float temp;
int analog;
int LED1 = 7;
int LED2 = 8;
int button = 4;
int buttonState = 0;
boolean flag = 0;
boolean prevState = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(button, INPUT);
  Serial.begin(9600);
  analogReference(DEFAULT); // specify that I have 5 volts
}

void loop() {
  // calculating temp
  analog = analogRead(analogPin);
  buttonState = digitalRead(button);
  temp = (((double)analog/1024)*5-0.5)*100;

  // track the values on serial
  Serial.print("Current Temperature: ");
  Serial.println(temp);
  delay(500);

  // if button is pressed, run the program
  if (buttonState == 1 && prevState == LOW){
    flag = !flag;
  }

  // if button is pressed, turn on
  if (flag){

    // "heater" is on
    if (temp >= 26){
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, LOW);
    }

    // "AC" is on
    else {
      digitalWrite(LED1, LOW);
      digitalWrite(LED2, HIGH);
    }

  }
  // turn off
  else {
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, LOW);
  }

  // track button state
  prevState = buttonState;
}

Some of the challenges I faced was making a code run only when a button is pressed and turn everything off when the button is pressed the second time. I have spent about an hour trying to debug when I realized that I was missing the “else”-snippet when the flag is off. But now it works! 🥳

 

LEDs and Buttons

  • Name of puzzle: Stop the bomb.
  • The idea of this puzzle was to find away to stop the lights from blinking with the right clicking combination.
  • I managed to configure the buttons and LEDs to separate digital pins and control them with code.However, I don’t know how to input the number of times the button was clicked in order to result in something.
  • In the end this became more of a learning experience, as it took me longer than expected to debug the mechanical components.
const int led = 8; //red
const int led2 = 10; //blue
const int led3 = 3;//green
const int blueButton = 6;
const int redButton = 5;
const int greenButton = 4;


void setup() {
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(blueButton, INPUT_PULLUP);
  pinMode(redButton, INPUT_PULLUP);
  pinMode(greenButton, INPUT_PULLUP);
};
void loop() {
  bool bluebuttonState = digitalRead(blueButton);
  bool redbuttonState = digitalRead(redButton);
  bool greenbuttonState = digitalRead(greenButton);


  if (bluebuttonState == HIGH) {
    digitalWrite(led, LOW);

  } else {
    digitalWrite(led, HIGH);

  };
  if (redbuttonState == HIGH) {
    digitalWrite(led, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led3, LOW);

  } else {
    digitalWrite(led, HIGH);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, HIGH);

  };

  if (greenbuttonState == HIGH) {

    digitalWrite(led2, LOW);

  } else {

    digitalWrite(led2, HIGH);

  };

};