Skip to content

Instantly share code, notes, and snippets.

@gbonanome
Created July 10, 2015 07:21
Show Gist options
  • Save gbonanome/38e4c24f5625839dc822 to your computer and use it in GitHub Desktop.
Save gbonanome/38e4c24f5625839dc822 to your computer and use it in GitHub Desktop.
Natalino's sketches
#include <IRremote.h>
#include <Servo.h>
#include <SoftwareSerial.h>
//link types:
//echo,IR,serial(BT),photores
const int speed1 = 30;
const int speed2 = 130;
//serial(BT)
Servo servoSx;
Servo servoDx;
const int pinServoSx = 7;
const int pinServoDx = 8;
char command;
SoftwareSerial mySerial(2,3);
//IR
IRrecv irrecv(9);
decode_results results;
//echo
long duration,cm;
const int pingPin = 6;
const int trigPin = 5;
const long tooClose = 10;
const int buzzPin = 13;
//going around randomly
const int interval = 200;
//photores
const int lightSensor = A0;
const int lightThreshold = 40;
void setup() {
Serial.begin(9600); //serial
mySerial.begin(9600);//BT
Serial.println("ready");
mySerial.println("ready");
irrecv.enableIRIn(); //IR
pinMode(pingPin, INPUT); //echo
pinMode(trigPin, OUTPUT);
}
void attServo() {
servoSx.attach(pinServoSx);
servoDx.attach(pinServoDx);
}
void fdMotion(){
attServo();
servoSx.write(speed2);
servoDx.write(speed1);
}
void bkMotion(){
attServo();
servoSx.write(speed1);
servoDx.write(speed2);
}
void sxMotion(){
attServo();
servoSx.write(speed2);
servoDx.write(speed2);
}
void dxMotion(){
attServo();
servoSx.write(speed1);
servoDx.write(speed1);
}
void noMotion(){
servoSx.detach();
servoDx.detach();
}
/*
void loop() { //Serial Mode (BT)
while(mySerial.available()) {
command = mySerial.read();
if (command == 'F') {
fdMotion();
} else if (command == 'B') {
bkMotion();
} else if (command == 'R') {
dxMotion();
} else if (command == 'L') {
sxMotion();
} else {
Serial.println(command);
noMotion();
}
}
}
*/
void loop() { //IR mode
if (irrecv.decode(&results)) {
Serial.println(results.value);
if (results.value == 16736925) {
fdMotion();
} else if (results.value == 16754775) {
bkMotion();
} else if (results.value == 16761405) {
dxMotion();
} else if (results.value == 16720605) {
sxMotion();
} else if (results.value == 16712445) {
noMotion();
}
irrecv.resume();
delay(100);
}
if (proximity()){
analogWrite(buzzPin, 200);
} else {
analogWrite(buzzPin,0);
}
}
/*
void loop() { //echo, photores or just go around randomly
int x = 0;
fdMotion();
while (x < interval) {
if(photores()) {
noMotion();
} else {
fdMotion();
}
if (proximity()) {
bkMotion();
delay(3000);
sxMotion();
delay(3000);
}
x += 1;
}
}
*/
bool proximity() { //echo
long duration;
digitalWrite(trigPin,LOW);
delayMicroseconds(2);
digitalWrite(trigPin,HIGH);
delayMicroseconds(5);
digitalWrite(trigPin,LOW);
duration = pulseIn(pingPin,HIGH);
//Serial.print("cm ");
//Serial.println(duration/29/2);
if (duration/29/2 < tooClose ) {
return true;
} else {
return false;
}
}
bool photores() {
int sens = analogRead(lightSensor);
Serial.print("lux: ");
Serial.println(sens);
if(sens > lightThreshold) {
return true;
} else {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment