--- /dev/null
+
+//Motors
+const int limitleftpin = 3;
+const int limitrightpin = 2;
+const int motorpin1 = 5;
+const int motorpin2 = 4;
+const int motorsenable = 6;
+
+//Servo motor
+const int servopin = 7;
+
+//buffer stuff
+char command = 0;
+int value = 0;
+#define POSITIVE 0
+#define NEGATIVE 1
+int sign = POSITIVE;
+
+
+void eval(int command, int value)
+{
+ switch(command)
+ {
+ case 'I':
+ Serial.print(F("LivingScreen\0\n"));
+ Serial.flush();
+ break;
+ case 'T':
+ Serial.print("Translate ");
+ Serial.println(value);
+ translate(value);
+ break;
+ case 'R':
+ Serial.print("Rotate ");
+ Serial.println(value);
+ rotate(value);
+ break;
+ }
+}
+
+
+void setup()
+{
+ //Motors
+ init_motors();
+
+ //Communication
+ Serial.begin(9600);
+}
+
+void loop()
+{
+
+ char c;
+ //read in characters if we got them.
+ if (Serial.available() > 0)
+ {
+ c = Serial.read();
+
+ if (c == '\n' || c == '\r')
+ {
+ if (command)
+ {
+ if (sign == NEGATIVE)
+ value = -value;
+ eval(command, value);
+ command = 0;
+ value = 0;
+ sign = POSITIVE;
+ }
+ }
+ else if (!command)
+ command = c;
+ else if (c == '-')
+ sign = NEGATIVE;
+ else if (c >= '0' && c <= '9')
+ value = value * 10 + (c - '0');
+ }
+ else
+ delayMicroseconds(100);
+}
+
+
+
--- /dev/null
+#include <Servo.h>
+
+//DC motors
+const int motorspeed = 33; // mm/s
+
+//Servo
+Servo servo;
+
+void init_motors()
+{
+ //Limit switches
+ pinMode(limitleftpin, INPUT_PULLUP);
+ pinMode(limitrightpin, INPUT_PULLUP);
+
+ //Motors
+ pinMode(motorpin1, OUTPUT);
+ pinMode(motorpin2, OUTPUT);
+ pinMode(motorsenable,OUTPUT);
+
+ //Servo
+ //pinMode(servopin, OUTPUT);
+ servo.attach(servopin);
+}
+
+/*
+ distance
+ +: right
+ -: left
+*/
+void translate(int distance)
+{
+ unsigned int end = 0;
+ digitalWrite(motorsenable, HIGH);
+
+ //forward
+ if (distance > 0)
+ {
+ digitalWrite(motorpin1, HIGH);
+ digitalWrite(motorpin2, LOW);
+ end = millis() + (distance * 1000.) / motorspeed;
+ }
+ else
+ {
+ //left motor backward
+ digitalWrite(motorpin1, LOW);
+ digitalWrite(motorpin2, HIGH);
+
+ end = millis() + (-distance * 1000.) / motorspeed;
+ }
+
+ while(millis() < end)
+ {
+ if (distance > 0)
+ {
+ if (!digitalRead(limitrightpin))
+ {
+ Serial.println("Left reached");
+ break;
+ }
+ delayMicroseconds(10);
+ }
+ else
+ {
+ if (!digitalRead(limitleftpin))
+ {
+ Serial.println("right reached");
+ break;
+ }
+ delayMicroseconds(10);
+ }
+ }
+
+ Serial.println("Finished");
+
+ //motor stop
+ digitalWrite(motorpin1, LOW);
+ digitalWrite(motorpin2, LOW);
+
+ digitalWrite(motorsenable, LOW);
+}
+
+/*
+ angle
+ +: clockwise
+ -: anticlockwise
+*/
+void rotate(int angle)
+{
+}
+