Whine-bot
Here is my very whiny robot. He complains when you wake him up! He whines no for the right button and grudgingly whines yes for the left button. But after three yeses he gets mad. You’ve got to go Jack Bauer style and twist his arm! Eventually he’ll relent. If you twist too long though, he’ll crash. Literally. Hold both buttons for a more humane reset.
Code:
int speakerPin = 9;
int flexPin=0;
boolean on = false;
int val;
int painCount=0;
int rightCount=0;
const int buttonLeft = 2;
int buttonLeftState = 0;
const int buttonRight = 3;
int buttonRightState = 0;
void setup() {
Serial.begin(9600);
pinMode(speakerPin, OUTPUT);
pinMode(buttonLeft, INPUT);
pinMode(buttonRight, INPUT);
}
void loop() {
if (on==false){
turnOn();
on=true;
}
buttonCheck();
}
void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
}
void buttonCheck(){
buttonLeftState=digitalRead(buttonLeft);
buttonRightState=digitalRead(buttonRight);
val=analogRead(flexPin);
Serial.println(val);
if (val>300){
painSound();
}
else if (val<=300){
painCount=0;
}
if (buttonRightState==LOW&&buttonLeftState==LOW){
resetSound(100);
on=false;
}
else if (buttonLeftState==LOW && buttonRightState==HIGH && rightCount<2){
left();
rightCount++;
}
else if (buttonLeftState==LOW && buttonRightState==HIGH && rightCount>=2){
right();
rightCount++;
}
else if (buttonRightState==LOW&&buttonLeftState==HIGH){
right();
}
}
void turnOff(){
int note=200;
int count=2;
int length=40;
for(int i=0;i< 15; i++){
for (int i = 0; i < 10; i++) {
playTone(note,length);
note+=count;
}
count=1.5*count;
length=.85*length;
}
playTone(note, 500);
}
void resetSound(int note){
int interval=500;
playTone(note, interval);
delay(interval);
playTone(note, interval);
delay(interval);
playTone(note,interval);
delay(interval);
playTone(6000, 3000);
delay(1000);
painCount=0;
rightCount=0;
delay(250);
}
void turnOn(){
delay(250);
playTone(300,30);
delay(100);
playTone(250,400);
delay(600);
playTone(300,80);
delay(100);
playTone(250,800);
delay(1000);
playTone(300,100);
delay(100);
playTone(200,1500);
delay(300);
}
void left(){
int note=300;
for (int i = 0; i < 10; i++) {
playTone(note, 5);
note-=15;
}
playTone(note, 200);
for (int i = 0; i < 5; i++) {
playTone(note, 5);
note-=1;
}
delay(200);
for (int i = 0; i < 5; i++) {
playTone(note, 5);
note-=5;
}
playTone(note, 1500);
delay(250);
}
void right(){
int note=500;
for (int i = 0; i < 30; i++) {
playTone(note, 10);
note-=5;
}
for (int i = 0; i < 60; i++) {
playTone(note, 13);
note+=5;
}
for (int i = 0; i < 2; i++) {
playTone(note, 15);
note+=1;
}
delay(250);
}
void painSound(){
int note=100;
if (painCount>400 && painCount<500){
left();
delay(500);
painCount=550;
rightCount=0;
}
if (painCount>1000){
turnOff();
painCount=0;
rightCount=0;
}
for (int i = 0; i < 50; i++) {
playTone(note, 10);
note+=10;
painCount+=1;
}
}
Reply