Last active
January 26, 2019 21:33
-
-
Save robotjoosen/3867840 to your computer and use it in GitHub Desktop.
Simple robot code used for the coding of a simple robot part of a workshop.
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
//Laad de servo bibliotheek zodat je het zelf niet hoeft te schrijven. | |
#include <Servo.h> | |
//IR_SENSOR | |
static int ir_sensor = 0; //Analoge poort A0 | |
static int ir_threshold = 400; //0-1023 [0 = 0v / 1023 = 5v] | |
int ir_value; | |
//SERVO | |
static int servo_pin_left = 2; //Digital pin 2 | |
static int servo_pin_right = 3; //Digital pin 3 | |
Servo servo_left; | |
Servo servo_right; | |
//Begin de setup van de Arduino | |
void setup(){ | |
//Begin serial for "debugging pur" | |
Serial.begin(9600); | |
//Stel de pinnen in als Servo | |
servo_left.attach(servo_pin_left); | |
servo_right.attach(servo_pin_right); | |
} | |
//Dit is een oneindige loop en ziet er ongeveer zo uit -> for(;;) { loop(); } | |
void loop(){ | |
//Lees de IR afstandssensor uit | |
ir_value = analogRead(ir_sensor); | |
//Kijk of de waarde groter is dan de drempel zoals boven is ingesteld. | |
if(ir_value > ir_threshold){ | |
//Kies een kant (0 = links / 1 = rechts) | |
int selection = random(2); | |
//Voer de actie uit. | |
switch(selection){ | |
case 0 : | |
Serial.println("LEFT"); | |
servo_left.write(180); | |
servo_right.write(180); | |
delay(450); | |
break; | |
case 1 : | |
Serial.println("RIGHT"); | |
servo_left.write(0); | |
servo_right.write(0); | |
delay(450); | |
break; | |
} | |
} else { | |
// Als de drempel niet word bereikt blijf rechtdoor gaan :) | |
servo_left.write(0); | |
servo_right.write(180); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment