Last active
March 6, 2016 18:18
-
-
Save taka-wang/7396cbc460fd9d4c3f32 to your computer and use it in GitHub Desktop.
Arduino Serial (LED, Servo, meArm)
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
// the setup function runs once when you press reset or power the board | |
void setup() { | |
// initialize digital pin 13 as an output. | |
pinMode(13, OUTPUT); | |
} | |
// the loop function runs over and over again forever | |
void loop() { | |
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level) | |
delay(1000); // wait for a second | |
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW | |
delay(3000); // wait for a second | |
} |
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 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); | |
} | |
} |
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
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