Skip to content

Instantly share code, notes, and snippets.

@taka-wang
Last active March 6, 2016 18:18
Show Gist options
  • Save taka-wang/7396cbc460fd9d4c3f32 to your computer and use it in GitHub Desktop.
Save taka-wang/7396cbc460fd9d4c3f32 to your computer and use it in GitHub Desktop.
Arduino Serial (LED, Servo, meArm)
#include <Servo.h>
Servo servoLeft;
Servo servoRight;
Servo servoBottom;
Servo servoClaw;
void setup() {
Serial.begin(9600);
servoLeft.attach(10);
servoRight.attach(9);
servoBottom.attach(6);
servoClaw.attach(5);
delay(100);
servoLeft.write(170);
servoRight.write(100);
servoBottom.write(75);
servoClaw.write(52);
servoClaw.write(27);
}
String str;
void loop() {
if (Serial.available() > 0) {
str = Serial.readStringUntil('\n');
int servo = str.substring(0,1).toInt();
int angel = str.substring(2).toInt();
switch (servo) {
case 1: // left
//
angel = map(angel, 0, 1023, 40, 170);
servoLeft.write(angel);
break;
case 2: // right
//
angel = map(angel, 0, 1023, 100, 150);
servoRight.write(angel);
break;
case 3: // bottom
//
angel = map(angel, 0, 1023, 10, 160);
servoBottom.write(angel);
break;
case 4: // claw
//
angel = map(angel, 0, 1023, 27, 52);
servoClaw.write(angel);
break;
default:
Serial.println("unknow");
}
delay(500);
}
}
const int ledPin = 13;
void setup(){
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop(){
if (Serial.available()) {
int a = Serial.read() - '0';
light(a);
}
delay(500);
}
void light(int n){
for (int i = 0; i < n; i++) {
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment