#c: light2
Explore tagged Tumblr posts
fuckyeah-exoarmpithair · 9 months ago
Text
Tumblr media
(╯。◕‿◕。)╯︵(credit)
3 notes · View notes
rose-pc-blog · 5 years ago
Text
WeatherSensor and MusicBox
After getting the bluetooth working, I started putting all the pieces together. I have two arduino circuits, one named ‘WeatherSensor’ and one named ‘MusicBox’. The WeatherSensor is made up of one raindrop sensor, two LDR’s and a HC-05 Module transmitting the data. The MusicBox is, at the moment, one Piezo speaker and a HC-05 Module. 
int rain = analogRead(A0); int light1 = analogRead(A1); int light2 = analogRead(A2); int light = (light1 + light2) / 2; Serial1.write(rain); Serial1.write(light); delay(1000); Serial.flush();
I started this development by sending a String containing direct readings from the rain sensor and an average reading of the LDR’s to my phone, just to check the data was sending. 
Then I set up the MusicBox to receive this message. The next step was parsing the message into separate readings again. Due to sending String instead of raw int or byte values, this was more complicated than I expected. My prime language is Javascript, so I’m very used to being able to easily manipulate String values. This is not as easy in C, and I had to struggle with splitting and clearing the String message. I did eventually figure out a system, but once I fully understood the Serial Buffer and how it works, I was able to switch to just sending and receiving two integer values which was a lot easier. 
if (Serial1.available()) { int val = Serial1.read(); if (alt) { Serial.println(val); rainVal = map(val, 0, 1050, 0, 100); Serial.println(rainVal); alt = false; } else { lightVal = map(val, 0, 30, 0, 100); alt = true; } }
Next I wrote a series of if/else conditions to determine what kind of weather it was depending on the Light and Rain values. Each statement passes different values into the ‘playMelody()’ function, the index of the required melody and the tempo it is to be played at.
//------DECIDE CONDITION //LIGHT LOW + NO RAIN if (lightVal < 50 && rainVal < 30) { Serial.println("CLOUDY"); playMelody(1, 140); // zelda theme } //LIGHT LOW + SOME RAIN else if (lightVal < 50 && rainVal < 60) { Serial.println("DRIZZLE"); playMelody(0, 88); // zeldas lullaby } //LIGHT LOW + LOTS OF RAIN else if (lightVal < 50 && rainVal >= 60) { Serial.println("HEAVY RAIN"); playMelody(4, 85); // jigglypuff song } //LIGHT HIGH + NO RAIN else if (lightVal > 50 && rainVal < 30) { Serial.println("SUNNY"); playMelody(3, 140); // take on me } //LIGHT HIGH + SOME RAIN else if (lightVal > 50 && rainVal < 60) { Serial.println("SUMMER SHOWER"); playMelody(2, 140); // mii channel theme } else { Serial.println("ERROR"); } //light high + lots of rain not included as it is very unlikely to occur
I created a separate header file to contain the arrays of melodies, simply for organisations sake, and created a global constant ‘jagged’ (multidimensional) array called ‘melodies’ to store them. I also created a separate array containing the sizes of these melodies, as the sizeof() function didn’t seem to work on the instances of the ‘melodies’ array. Each of the melodies are from this GitHub https://github.com/robsoncouto/arduino-songs
extern const int* melodies[] = {zelda1, zelda2, miiChannel, takeOnMe, jigglypuff}; extern const int sizes[] = {sizeof(zelda1), sizeof(zelda2), sizeof(miiChannel), sizeof(takeOnMe), sizeof(jigglypuff)};
The playMelody() function is a reworked version of the loop that comes with the melodies from that GitHub, altered a bit to function with the ‘melodies’ array. As it uses the delay, I had a problem with the Serial buffer becoming backlogged with unprocessed values. I added a serial.flush() function to the WeatherSensor code to clear the outgoing buffer each loop and keep only the most current data in it. 
void playMelody( int index, int tempo) { int notes = sizes[index] / sizeof(melodies[index][0]) / 2; // this calculates the duration of a whole note in ms (60s/tempo)*4 beats int wholenote = (60000 * 4) / tempo; int divider = 0, noteDuration = 0; for (int thisNote = 0; thisNote < notes * 2; thisNote = thisNote + 2) { // calculates the duration of each note divider = melodies[index][thisNote + 1]; if (divider > 0) { // regular note, just proceed noteDuration = (wholenote) / divider; } else if (divider < 0) { // dotted notes are represented with negative durations!! noteDuration = (wholenote) / abs(divider); noteDuration *= 1.5; // increases the duration in half for dotted notes } Serial.println(melodies[index][thisNote]); // we only play the note for 90% of the duration, leaving 10% as a pause tone(buzzer, melodies[index][thisNote], noteDuration * 0.9); // Wait for the specief duration before playing the next note. delay(noteDuration); // stop the waveform generation before the next note. noTone(buzzer); } Serial.println("end"); }
The next step is to get the project working remotely. I have some batteries, a couple of snap connecters and some barrel connectors arriving soon that I will use.
WeatherSensor
Tumblr media Tumblr media
MusicBox
Tumblr media Tumblr media
vimeo
Evaluation
I am really happy with how this turned out. It was definitely a fairly complex and long process, but it all works and I’m proud of how far I’ve come. There are just a couple more pieces to add and I will do a full evaluation of circuit, code and general project outcomes then.
0 notes