Created
March 31, 2014 22:04
-
-
Save sdbakker/9903402 to your computer and use it in GitHub Desktop.
WdKA basic arduino workshop Part 3: Light, switch and servo
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
/************************************************************/ | |
/* Sample useless machine code */ | |
/* WdKA basic arduino workshop */ | |
/* */ | |
/* Part 3: Light, switch and servo */ | |
/* Simon de Bakker <simon@simbits.nl> */ | |
/************************************************************/ | |
/* import routines used to control a servo */ | |
#include <Servo.h> | |
/* pin definitions (what is connected to which arduino pin) */ | |
#define LED_PIN 12 | |
#define SWITCH_PIN 8 | |
#define SERVO_PIN 3 | |
/* servo positions in degrees */ | |
#define ARM_NEUTRAL_POS 41 | |
#define ARM_SWITCH_POS 128 | |
/* min and max time to wait before moving to turn off the switch (max +100ms) */ | |
#define ARM_MIN_WAIT_MS 0 | |
#define ARM_MAX_WAIT_MS 1000 | |
/* create a servo object */ | |
Servo arm_servo; | |
/* global variable to keep track if the arm is moving */ | |
/* towards the switch, and initialise it to false (not */ | |
/* moving) */ | |
bool arm_moving_to_switch = false; | |
void setup() { | |
/* set the directions of each pin */ | |
pinMode(LED_PIN, OUTPUT); | |
pinMode(SERVO_PIN, OUTPUT); | |
pinMode(SWITCH_PIN, INPUT); | |
/* attach the servo to our servo object */ | |
arm_servo.attach(SERVO_PIN); | |
/* move it to our defined neutral position */ | |
arm_servo.write(ARM_NEUTRAL_POS); | |
/* just a small routine to let us know setup is finished */ | |
/* turn on our LED */ | |
digitalWrite(LED_PIN, HIGH); | |
/* wait half a second */ | |
delay(500); | |
/* turn our LED off again */ | |
digitalWrite(LED_PIN, LOW); | |
} | |
void loop() { | |
/* check if the switch is toggled */ | |
if ( digitalRead(SWITCH_PIN) == LOW ) { | |
/* if the switch is in the off position */ | |
/* go back to neutral arm position */ | |
arm_servo.write(ARM_NEUTRAL_POS); | |
/* arm is not moving to switch */ | |
arm_moving_to_switch = false; | |
/* turn of the LED */ | |
digitalWrite(LED_PIN, LOW); | |
} else if ( arm_moving_to_switch == false ) { | |
/* if switch is in the on position AND arm is not moving | |
/* towards the switch yet */ | |
/* some internal variables */ | |
/* led_state will keep track whether the LED is on or off, used for blinking */ | |
int led_state = LOW; | |
/* wait_ms is the amount of (approximate) milliseconds the arm will wait before */ | |
/* it moves to toggle of the switch */ | |
int wait_ms = random(ARM_MIN_WAIT_MS, ARM_MAX_WAIT_MS); | |
/* a little loop to blink the LED while waiting for the arm to move */ | |
for (int i=0; i < wait_ms; i+=100) { | |
/* toggle the LED */ | |
led_state = ~led_state; | |
digitalWrite(LED_PIN, led_state); | |
/* wait 100ms */ | |
delay(100); | |
} | |
/* move the arm to toggle of the switch */ | |
arm_servo.write(ARM_SWITCH_POS); | |
/* we are moving towards the switch */ | |
arm_moving_to_switch = true; | |
/* make sure the LED is on */ | |
digitalWrite(LED_PIN, HIGH); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment