Description: For this assignment, I decided to use a force sensor for my input and a piezo buzzer for my output. I decided to use a piggy bank as the object to base the project around. I thought it would be interesting to provide positive feedback everytime a coin was inputted to hte bank. This thus creates a positive reinforcement loop and incentivizes someone to save money. The force sensor detects an added coin, resulting in the buzzer creating a noise.
Components:
1 Arduino Uno
1 Breadboard
1 piggy bank
1 force sensor
1 piezo buzzer
various wires and resistors
Code:
int sensorPin = A0;
int sensorVal = 0;
// Values
int total = 0;
int speakerPin = 7;
byte names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};
int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
byte melody[] = "2d2a1f2c2d2a2d2c2f2d2a2c2d2a1f2c2d2a2a2g2p8p8p8p";
int speakerOut = 7;
int count = 0;
int count2 = 0;
int count3 = 0;
int MAX_COUNT = 24;
int statePin = LOW;
void setup()
{
pinMode(speakerPin, OUTPUT);
pinMode(sensorPin, INPUT);
Serial.begin(9600); // Open serial communication for reporting
}
void loop(){
sensorVal = analogRead(sensorPin);
while (sensorVal > 1010) {
sensorVal = analogRead(sensorPin);
Serial.println(sensorVal);
digitalWrite(speakerOut, LOW);
for (count = 0; count < MAX_COUNT; count++) {
statePin = !statePin;
for (count3 = 0; count3 <= (melody[count*2] - 48) * 30; count3++) {
for (count2=0;count2<8;count2++) {
if (names[count2] == melody[count*2 + 1]) {
digitalWrite(speakerOut,HIGH);
delayMicroseconds(tones[count2]);
digitalWrite(speakerOut, LOW);
delayMicroseconds(tones[count2]);
}
if (melody[count*2 + 1] == 'p') {
// make a pause of a certain size
digitalWrite(speakerOut, 0);
delayMicroseconds(500);
}
}
}
}
/*if (sensorVal < 1000) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(1915);
digitalWrite(speakerPin, LOW);
delayMicroseconds(1915);
}*/
}
}
- Login to post comments