spider/Arduino/SerialServo/SerialServo.ino
2019-05-24 09:35:01 +02:00

104 lines
2.4 KiB
C++

/*
Reading a serial ASCII-encoded string.
This sketch demonstrates the Serial parseInt() function.
It looks for an ASCII string of comma-separated values.
It parses them into ints, and uses those to fade an RGB LED.
Circuit: Common-Cathode RGB LED wired like so:
- red anode: digital pin 3
- green anode: digital pin 5
- blue anode: digital pin 6
- cathode: GND
created 13 Apr 2012
by Tom Igoe
modified 14 Mar 2016
by Arturo Guadalupi
This example code is in the public domain.
*/
#include <ESP32Servo.h>
Servo S1,S2,S3,S4,S5,S6,S7,S8 ;
// pins for the LEDs:
const int s1Pin = 21;
const int s2Pin = 32;
const int s3Pin = 12;
const int s4Pin = 13;
const int s5Pin = 22;
const int s6Pin = 19;
const int s7Pin = 16;
const int s8Pin = 23;
void setup() {
// initialize serial:
Serial.begin(9600);
// make the pins outputs:
S1.attach(s1Pin,550,2350);
S2.attach(s2Pin,550,2350);
S3.attach(s3Pin,550,2350);
S4.attach(s4Pin,550,2350);
S5.attach(s5Pin,550,2350);
S6.attach(s6Pin,550,2350);
S7.attach(s7Pin,550,2350);
S8.attach(s8Pin,550,2350);
}
void loop() {
// if there's any serial available, read it:
while (Serial.available() > 0) {
// look for the next valid integer in the incoming serial stream:
int Servonummer = Serial.parseInt();
// do it again:
int Servowinkel = Serial.parseInt();
// look for the newline. That's the end of your sentence:
if (Serial.read() == '\n') {
// constrain the values to 0 - 255 and invert
// if you're using a common-cathode LED, just use "constrain(color, 0, 255);"
// fade the red, green, and blue legs of the LED:
// print the three numbers in one string as hexadecimal:
Serial.println(Servonummer);
Serial.println(Servowinkel);
Serial.println("-------------------------------------------------------");
switch(Servonummer){
case 0:
S1.write(Servowinkel);
break;
case 1:
S2.write(Servowinkel);
break;
case 2:
S3.write(Servowinkel);
break;
case 3:
S4.write(Servowinkel);
break;
case 4:
S5.write(Servowinkel);
break;
case 5:
S6.write(Servowinkel);
break;
case 6:
S7.write(Servowinkel);
break;
case 7:
S8.write(Servowinkel);
break;
}
}
}
}