Tone lab and Servo lab (for 2/21)

Tone lab

For this lab, I used photoresistors to control the tone of a buzzer. The buzzer makes a barely audible sound because it needs more than 5v of power to be loud. I only can hook the buzzer up to 5v. Below you will find the code, an image of setup, and a video of me using the photoresistors to control a servo in place of the speaker because you cannot hear the speaker in the video.

Code:
void setup() {
  Serial.begin(9600);       // initialize serial communications
}

void loop(){
  int analogValue = analogRead(A0); // read the analog input
  Serial.println(analogValue);      // print it
  // get a sensor reading:
   int sensorReading = analogRead(A0);
   // map the results from the sensor reading's range
   // to the desired pitch range:
   float frequency = map(sensorReading, 200, 900, 100, 1000);
   // change the pitch, play for 10 ms:
   tone(8, frequency, 10);

}







Servo lab

For this lab, I used a potentiometer to control a servo.

Code:

#include <Servo.h>
Servo myservo;
int potpin = 5;
int val;
void setup()
{
myservo.attach(9);
}
void loop()
{
val = analogRead(potpin);
   
   
val = map(val, 0, 1023, 0, 179);
   
   
myservo.write(val);
   
delay(15); 
}




Mini Project

Finally, I had an idea based off the fact that the buzzer was not loud enough and I used the servo in the video instead. I came up with a little fun spin on the project. I used the same code for the servo lab, and changed my sensor from the potentiometer to the photoresistors. Then, I taped a flower to the servo. I created a light detector. I take medication at night that requires me to be in low light settings from the time I take it to after I go to bed. So I thought it would be fun to make a little visualizer for that. This is also a cute device that could test the light amounts for flowers. If the flower is too low, it probably means your real flowers need more light than their current situation allows. 

Here is the video of my flower reacting to light levels. 




NOTES FROM CLASS

I learned that my buzzer was receiving enough power but my resistor was too high

I created a voltage divider between photoresistors and so each sensor controlled one half of the servo's movement. One resistor made the flower move right, and the other resistor made the flower move left. This is because when one resistor is low the other is high, and if they are both high, they cancel each other out, leaving the flower at a neutral 90 degrees. I thought it was exciting that I did this exclusively through my hardware decisions and did not need to write more complex code to create this effect. I learned that there are more solutions to problems than just altering code because of this. 


Comments

Popular posts from this blog

PROJECT 1 (2.0!) : LOCK BOX (For 3/26/19)

Project one observations