Created
November 17, 2013 15:40
-
-
Save damphyr/7514713 to your computer and use it in GitHub Desktop.
ATCAR
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
// constants won't change. Used here to | |
// set pin numbers: | |
const int ledPin = 13; // the number of the LED pin | |
// Variables will change: | |
int ledState = LOW; // ledState used to set the LED | |
long previousMillis = 0; // will store last time LED was updated | |
// the follow variables is a long because the time, measured in miliseconds, | |
// will quickly become a bigger number than can be stored in an int. | |
long interval = 1000; // interval at which to blink (milliseconds) | |
void ledInit() | |
{ | |
pinMode(ledPin, OUTPUT); | |
} | |
void ledBlink() | |
{ | |
unsigned long currentMillis = millis(); | |
if(currentMillis - previousMillis > interval) { | |
// save the last time you blinked the LED | |
previousMillis = currentMillis; | |
// if the LED is off turn it on and vice-versa: | |
if (ledState == LOW) | |
ledState = HIGH; | |
else | |
ledState = LOW; | |
digitalWrite(ledPin, ledState); | |
} | |
} | |
void ledOn() | |
{ | |
if(ledState == LOW) | |
{ | |
ledState = HIGH; | |
digitalWrite(ledPin, ledState); | |
} | |
} | |
void handleLed() | |
{ | |
if (mode==1) | |
{ | |
ledOn(); | |
} | |
else | |
{ | |
ledBlink(); | |
} | |
} |
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
// Controlling a servo position using a potentiometer (variable resistor) | |
// by Michal Rinott <http://people.interaction-ivrea.it/m.rinott> | |
int potpin = 0; // analog pin used to connect the potentiometer | |
int mode=1; | |
void setup() | |
{ | |
ledInit(); | |
buttonInit(); | |
servoInit(); | |
} | |
void loop() | |
{ | |
handleLed(); | |
handleButton(); | |
handleServo(); | |
delay(50); // waits for the servo to get there | |
} |
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
#include <Servo.h> | |
Servo myservo; // create servo object to control a servo | |
int position=0; | |
int direction=0; | |
void servoInit() | |
{ | |
myservo.attach(9); // attaches the servo on pin 9 to the servo object | |
} | |
int servoAutomatic() | |
{ | |
if (direction==0) | |
{ | |
if (position < 179) | |
{ | |
position=position+1; | |
} | |
else | |
{ | |
direction=1; | |
} | |
} | |
else | |
{ | |
if(position>0) | |
{ | |
position=position-1; | |
} | |
else | |
{ | |
direction=0; | |
} | |
} | |
return position; | |
} | |
int servoManual() | |
{ | |
int val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023) | |
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180) | |
return val; | |
} | |
void handleServo() | |
{ | |
int pos=90; | |
if (mode==0) | |
{ | |
pos=servoAutomatic(); | |
} | |
else | |
{ | |
pos=servoManual(); | |
} | |
myservo.write(pos); // sets the servo position according to the scaled value | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment