Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Servo.h>
- #include<String.h>
- #include<stdio.h>
- #include<stdlib.h>
- Servo myServo;
- char inData[8]; // Allocate some space for the string
- char* inData2;
- byte index = 0; // Index into array; where to store the character
- int angle = 0;
- int newAngle = 0;
- int MaxChars = 7;
- char ch;
- int Size=sizeof(inData);
- void SerialEvent();
- char* strrev( char* s );
- void setup() {
- // put your setup code here, to run once:
- myServo.attach(5);
- Serial.begin(9600);
- }
- void loop() {
- // put your main code here, to run repeatedly:
- void SerialEvent();
- }
- void serialEvent()
- {
- while (Serial.available())
- {
- char ch = Serial.read();
- Serial.write(ch);
- if (index < MaxChars && isDigit(ch))
- {
- inData[index++] = ch;
- }
- else {
- inData[index] = 0;
- inData2=strrev(inData);
- newAngle = atoi(inData2);
- newAngle=map(newAngle, 0.0, 9999.0, 0.0, 180.0);
- if (newAngle > 0 && newAngle < 180) {
- if (newAngle < angle)
- for (; angle > newAngle; angle -= 1) {
- myServo.write(angle);
- }
- else
- for (; angle < newAngle; angle += 1) {
- myServo.write(angle);
- }
- }
- index = 0;
- angle = newAngle;
- Serial.write(1);
- }
- }
- }
- char* strrev( char* s )
- {
- char c;
- char* s0 = s - 1;
- char* s1 = s;
- /* Find the end of the string */
- while (*s1) ++s1;
- /* Reverse it */
- while (s1-- > ++s0)
- {
- c = *s0;
- *s0 = *s1;
- *s1 = c;
- }
- return s;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement