Created
September 14, 2014 00:04
-
-
Save sj82516/9c8d0530e1407b62e1f6 to your computer and use it in GitHub Desktop.
Arduino_robotFaceDIY
This file contains 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
#include <Servo.h> | |
#include <IRremote.h> | |
//IR | |
int RECV_PIN = 11; | |
IRrecv irrecv(RECV_PIN); | |
decode_results results; | |
const unsigned long IRmove1 = 0XFF30CF; | |
const unsigned long IRmove2 = 0XFF18E7; | |
const unsigned long IRmove3 = 0XFF7A85; | |
//servo | |
Servo myservo; | |
int pos=0; | |
//motor | |
const int motorIn1 = 5; | |
const int motorIn2 = 6; | |
const int motorIn3 = 9; | |
const int motorIn4 = 10; | |
const int relayMotor=2; | |
//fan | |
const int relayFan=12; | |
const int DELAY = 1000; | |
void setup() | |
{ | |
Serial.begin(9600); | |
irrecv.enableIRIn(); | |
pinMode(motorIn1, OUTPUT); | |
pinMode(motorIn2, OUTPUT); | |
pinMode(motorIn3, OUTPUT); | |
pinMode(motorIn4, OUTPUT); | |
pinMode(relayMotor,OUTPUT); | |
pinMode(relayFan,OUTPUT); | |
myservo.attach(3); | |
} | |
void loop() | |
{ | |
int state=0; | |
if (irrecv.decode(&results)) { | |
if(results.value==IRmove1)state=1; | |
else if (results.value==IRmove2)state=2; | |
else if (results.value==IRmove3)state=3; | |
else state=10; | |
irrecv.resume(); // Receive the next value | |
} | |
switch(state){ | |
case 1:digitalWrite(relayMotor,HIGH); | |
selfTest(); | |
digitalWrite(relayMotor,LOW); | |
break; | |
case 2:servor(); | |
delay(20); | |
break; | |
case 3:digitalWrite(relayFan,HIGH); | |
wind(); | |
digitalWrite(relayFan,LOW); | |
break; | |
default:break; | |
} | |
} | |
void selfTest() | |
{ | |
forward(); | |
delay(DELAY); | |
motorstop(); delay(500); | |
backward(); | |
delay(DELAY); | |
motorstop(); delay(500); | |
} | |
void motorstop() | |
{ | |
digitalWrite(motorIn1, LOW); | |
digitalWrite(motorIn2, LOW); | |
digitalWrite(motorIn3, LOW); | |
digitalWrite(motorIn4, LOW); | |
} | |
void forward() | |
{ | |
digitalWrite(motorIn1, HIGH); | |
digitalWrite(motorIn2, LOW); | |
digitalWrite(motorIn3, HIGH); | |
digitalWrite(motorIn4, LOW); | |
} | |
void backward() | |
{ | |
digitalWrite(motorIn1, LOW); | |
digitalWrite(motorIn2, HIGH); | |
digitalWrite(motorIn3, LOW); | |
digitalWrite(motorIn4, HIGH); | |
} | |
void servor(){ | |
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees | |
{ // in steps of 1 degree | |
myservo.write(pos); // tell servo to go to position in variable 'pos' | |
delay(40); // waits 15ms for the servo to reach the position | |
} | |
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees | |
{ | |
myservo.write(pos); // tell servo to go to position in variable 'pos' | |
delay(40); // waits 15ms for the servo to reach the position | |
} | |
} | |
void wind(){ | |
digitalWrite(relayFan,HIGH); | |
delay(5000); | |
digitalWrite(relayFan,LOW); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment