Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@lvidarte
Created March 26, 2015 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lvidarte/4a10d41ec9bf4ccc74b1 to your computer and use it in GitHub Desktop.
Save lvidarte/4a10d41ec9bf4ccc74b1 to your computer and use it in GitHub Desktop.
Cannon laser client
#include <Servo.h>
#define SERVO_PIN 9
#define LASER_PIN 10
Servo myServo;
byte n;
int laser_active = 0;
void setup()
{
Serial.begin(9600);
myServo.attach(SERVO_PIN);
pinMode(LASER_PIN, OUTPUT);
}
void laser_on()
{
digitalWrite(LASER_PIN, HIGH);
laser_active = 1;
}
void laser_off()
{
digitalWrite(LASER_PIN, LOW);
laser_active = 0;
}
void laser_toggle()
{
if (laser_active) {
laser_off();
} else {
laser_on();
}
}
void laser_fire(int d)
{
laser_on();
delay(d);
laser_off();
}
void loop()
{
while (Serial.available() > 0) {
n = Serial.read();
switch (n)
{
case 181:
laser_on();
break;
case 182:
laser_off();
break;
case 183:
laser_toggle();
break;
case 184:
laser_fire(500);
break;
default:
myServo.write(n);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment