Last active
August 29, 2015 14:00
-
-
Save X-Y/11302115 to your computer and use it in GitHub Desktop.
MittMollan
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* | |
* MittMollan | |
* Created by PirateCheese | |
* Modified and tested by X-Y | |
* | |
* Note: IR ranger is generating a lot noise for pot. | |
* No Calibration for LDR. | |
* Servo working, weird enough. | |
* | |
*/ | |
#include <Servo.h> | |
#define O0 11 | |
#define O1 10 | |
#define O2 9 | |
#define O3 6 | |
#define O4 5 | |
#define O5 3 | |
//button - red LED | |
int button = A5; | |
int red = O5; | |
//Potentiometer - servo | |
int pot = A4; | |
Servo servS; //O4 | |
int val, lastVal; | |
//mic - dc motor | |
int micPin = A2; | |
int motor = O2; | |
//LDR - ultra bright led | |
int ldr = A3; | |
int led = O3; | |
//ir - piezo | |
int ir = A1; | |
int piezo = O1; | |
void setup(){ | |
Serial.begin(9600); | |
pinMode(button, INPUT); | |
servS.attach(O4); | |
pinMode(9, OUTPUT); | |
pinMode(10, OUTPUT); | |
analogWrite(led, 0); | |
} | |
void loop(){ | |
//Resets board when going from moving to still | |
//Button - Servo | |
// Serial.println(digitalRead(button)); | |
Serial.println("button"); | |
if(digitalRead(button)){ | |
while(digitalRead(button)) digitalWrite(red, HIGH); | |
digitalWrite(red, LOW); | |
} | |
//LDR - LED | |
Serial.println("ldr"); | |
if(analogRead(ldr)<850){ | |
while(analogRead(ldr)<850){ | |
int ldrVal=analogRead(ldr); | |
//Serial.println(ldrVal); | |
int fade = constrain(map(analogRead(ldr), 500, 850, 255, 0), 0, 255); | |
analogWrite(led, fade); | |
delay(10); | |
} | |
analogWrite(led, 0); | |
} | |
//Mic - motor | |
//Serial.println(getMicDiff()); | |
Serial.println("mic"); | |
if(getMicDiff()>800){ | |
digitalWrite(motor, HIGH); | |
while(getMicDiff()>800){ | |
digitalWrite(motor, HIGH); | |
delay(100); | |
} | |
digitalWrite(motor, LOW); | |
delay(100); | |
} | |
//IR - piezo | |
Serial.println("ir"); | |
if(analogRead(ir)>300){ | |
while(analogRead(ir)>300) { | |
int pitch = map(analogRead(ir), 300, 700, 5000, 100); | |
tone(piezo, pitch); | |
} | |
noTone(piezo); | |
} | |
Serial.println("servo"); | |
int deg = map(analogRead(pot), 10, 900, 170, 10); | |
servS.write(deg); | |
delay(100); | |
Serial.println("."); | |
} | |
int getMicDiff(){ | |
int oldval=0; | |
int diffMax=0; | |
oldval=analogRead(micPin); | |
for(int i=0;i<10;i++){ | |
int val=analogRead(micPin); | |
int diff=abs(val-oldval); | |
if(diff>diffMax){ | |
diffMax=diff; | |
} | |
oldval=val; | |
delay(1); | |
} | |
return diffMax; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment