Skip to content

Instantly share code, notes, and snippets.

@damphyr damphyr/Robot.ino
Created Nov 17, 2013

Embed
What would you like to do?
ATCAR
const int buttonPin=2;
const unsigned debounceTime=100;
int buttonState = 0;
unsigned readTime=0;
void buttonInit()
{
pinMode(buttonPin, INPUT);
}
void handleButton()
{
buttonState = digitalRead(buttonPin);
if(millis()-readTime > debounceTime)
{
if (buttonState == HIGH)
{
if(mode==0)
{
mode=1;
}
else
{
mode=0;
}
}
readTime=millis();
}
}
// 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();
}
}
// 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
}
#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
You can’t perform that action at this time.