2.Method

This is what we did :Connect the 8 steps to a circuit that regulates 5V and have the Arduino MEGA check for any change in voltage in the circuit and then play a note on the speaker when a change was detected.



More Detailed version:
1)make 8 steps
2)connect one side of each step a breadboard which receives 5V from the Arduino MEGA.
3)make touch point that make the circuit close when it makes contact with the steps
4)connect a wire from each touch point to an Analog Input on the Arduino MEGA
5)now connect one speaker(N.o of speaker depends on how many you want) to one of the digital pin and the black wire to the GRD pin on the Arduino MEGA. (Additional : A amplifier is needed for more than one speaker as it will get softer)

Here is the code, I have annotated it. Please not that text in theese brackets {} are not part of the code, and are used by us to annotate it. 

6) upload the following code :

int analogPin1 = 1; int analogPin2 = 2; int analogPin3 = 3; int analogPin4 = 4; int analogPin5 = 5; int analogPin6 = 6; int analogPin7 = 7; int analogPin8 = 8; int val1 = 0; int val2 = 0; int val3 = 0; int val4 = 0; int val5 = 0; int val6 = 0; int val7 = 0; int val8 = 0; const int buzzer = 8;

{ From the start of the code till now, we are declaring the interger variables we are using in the code.} void setup() 
{ This is the part of the code that will only run once, it is used to declare certain pins as either inputs or outputs, as well as others, like the Serial Monitor. } { pinMode(8, OUTPUT); { This is the speaker pin, which is a output. } pinMode(analogPin1, INPUT); { These are the input pins from which our stairs are connected to. } pinMode(analogPin2, INPUT); pinMode(analogPin3, INPUT); pinMode(analogPin4, INPUT); pinMode(analogPin5, INPUT); pinMode(analogPin6, INPUT); pinMode(analogPin7, INPUT); pinMode(analogPin8, INPUT); Serial.begin(9600); 
{ This part of the code is used for us to be able to see the readings of the input pins on the arduino. This proved very usefull during the troubleshooting part. } } void loop() { This is the part of the code which will run repeatedly, untill power is cut to the arduino. } { val1 = analogRead(analogPin1); { This part is just to tell the arduino that " val x " is the value of the analog pins. } val2 = analogRead(analogPin2); val3 = analogRead(analogPin3); val4 = analogRead(analogPin4); val5 = analogRead(analogPin5); val6 = analogRead(analogPin6); val7 = analogRead(analogPin7); val8 = analogRead(analogPin8); Serial.println(val2); 
{ This is used to debug the code, we can replace " val2 " with other values such as " val1 " or "val7" to see the values that each step is inputing to the arduino, thus we can find out if the values are conflicting. }
{ The main part of the code starts here. This is where the arduino reads the analog pins, and calculates if the values are above a certain threshhold, and will output a different tone depending if which one is triggered. }

{ This part in blue tells the arduino if value 1 is More than 1020, it will trigger a tone on pin 8, with the frequency being 523 hz, which is the highest note the staircase is using. }
if (val1 > 1020) { tone(8, 523); }
{ This part in red tells the arduino that if the first statement which is the one is not true, skip over it and run this statement, which takes value 2 and if it is more than 1020, it will trigger a tone on pin 8, our speaker pin, and with a different frequency than the earlier statement, as it is a different note. } else if (val2 > 1020) { tone(8, 494); }

{ The way if / else statements work is that if the statement is true, it will follow the code in the brackets, but if it is false, it will skip over the piece of code there and move on to the next statement. }

{ Rinse and repeat. }
else if (val3 > 1020) { tone(8, 440); } else if (val4 > 1020) { tone(8, 392); } else if (val5 > 1020) { tone(8, 349); } else if (val6 > 1020) { tone(8, 330); } else if (val7 > 1020) { tone(8, 294); } else if (val8 > 1020) { tone(8, 262); } { This part of the code, in dark red, is one of the most important pieces of code here. As the arduino only has the capibillity of outputting one tone at a time, even if multiple speakers are connected, We have to call noTone for the sound to stop before we are able to have a new note. }
else { noTone(8); }
}



7) now press down the step and it should work





0 comments:

Post a Comment