We will be using the LCD Display (Arduino) library to control our screen. While setting up, all we need to do is print out the initial message we want the user to see.
The calibration of the sensor is the most important part in this mini-project.
int sensLow = 1023;
int sensHigh = 0;
void setup() {
while (millis() < 5000) {
sensVal = analogRead(A0);
if (sensVal > sensHigh) {
sensHigh = sensVal;
}
if (sensVal < sensLow) {
sensLow = sensVal;
}
}
}
The millis function returns the amount of time for which our board has been working. Thus, we calibrate for the first five seconds by taking in the minimum and maximum possible sensor input values. These will be used to scale our input values later on.
Here, we take in our input value and map it to the appropriate frequency. Then, using the tone function, we output sound using our piezo (which vibrates as per the frequency provided by tone()). You can play around the frequency and mapping values just for fun.
You might notice that there are some differences between the schematic and what I’ve made. Firstly, the potentiometer is just too large to be placed on the same strip as a capacitor and power/ground connections, which is why I have used orange jumpers to elongate the entire setup. Also note that the capacitors, if connected incorrectly, could potentially explode. I would google up how to connect these and then proceed! It took me a lot of time to find out which pin is the anode and which pin is the cathode. Turns out the pin which has a grey arrow full of zeros pointing downwards is the negatively charged electrode, or the cathode.
To start, we import the library required to interact with the servo motor, and create an instance of the Servo object to handle our motor. Then, all we need is 4 lines of functional code.
Firstly, we read our analogue potentiometer value and convert it into an angle for rotating our servo motor. Then, using the servo library, we turn our motor. The delay of 15 milliseconds is to accomodate the time used up in rotating the motor.
This entire thing is fairly simple. We will use phototransistors to input the RGB components of any light source to our arduino. The arduino will then convert these to digital values, and combine the RGB components to operate a four pin, RGB LED. All we have to do is input to our uno, process the data (fancy word for dividing by 4), and output.
You can see that I didn’t use the wooden components already in the starter kit to appropriately connect the filters to the phototransistors. This is because those components were too small (probably a defect in the kit), fitting the screens caused them to fly across the room at random intervals.
Code
You can find the full code here. I’m not discussing the setup part here.
Here we’re reading in the rgb components of the light source under scrutiny. A delay of five milliseconds has been taken as the arduino takes a while to process analogue inputs and convert them into digital values.
Now we divide by four because our analogue input corresponds to digital values between 0 and 1023. However, our output LED requires digital values between 0 and 255. Given that our output LED requires analogue inputs, we use the UNO’s built in DAC to display our combined RGB components.
Note
Many people (including me) initially thought that this entire thing wasn’t working. This isn’t the case. The fact is that the phototransistors that come with the starter kit are incredibly insensitive. They registers values of o,0,0 for room lights (and most lights), which is why there is no discernible output unless one uses a strong flashlight (built-in in smartphones) or a decent light source.
Here’s a small demo. You can visibly see that iphone flashlights have strong blue and green components.
You must be logged in to post a comment.