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);
}


 

Leave a Reply