Advertisement
Guest User

Arduino Code

a guest
Apr 14th, 2016
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.57 KB | None | 0 0
  1. #include <Servo.h>
  2. #include<String.h>
  3. #include<stdio.h>
  4. #include<stdlib.h>
  5.  
  6. Servo myServo;
  7.  
  8.  
  9. char inData[8]; // Allocate some space for the string
  10. char* inData2;
  11. byte index = 0; // Index into array; where to store the character
  12. int angle = 0;
  13. int newAngle = 0;
  14. int MaxChars = 7;
  15. char ch;
  16. int Size=sizeof(inData);
  17.  
  18.  
  19. void SerialEvent();
  20. char* strrev( char* s );
  21.  
  22.  
  23.  
  24.  
  25.  
  26. void setup() {
  27.   // put your setup code here, to run once:
  28.  
  29.   myServo.attach(5);
  30.   Serial.begin(9600);
  31. }
  32.  
  33.  
  34.  
  35. void loop() {
  36.   // put your main code here, to run repeatedly:
  37.  
  38.   void SerialEvent();
  39.  
  40. }
  41.  
  42.  
  43.  
  44.  
  45.  
  46. void serialEvent()
  47. {
  48.   while (Serial.available())
  49.   {
  50.     char ch = Serial.read();
  51.     Serial.write(ch);
  52.     if (index < MaxChars && isDigit(ch))
  53.     {
  54.       inData[index++] = ch;
  55.     }
  56.     else {
  57.       inData[index] = 0;
  58.       inData2=strrev(inData);
  59.       newAngle = atoi(inData2);
  60.       newAngle=map(newAngle, 0.0, 9999.0, 0.0, 180.0);
  61.       if (newAngle > 0 && newAngle < 180) {
  62.         if (newAngle < angle)
  63.           for (; angle > newAngle; angle -= 1) {
  64.             myServo.write(angle);
  65.           }
  66.         else
  67.           for (; angle < newAngle; angle += 1) {
  68.             myServo.write(angle);
  69.           }
  70.       }
  71.       index = 0;
  72.       angle = newAngle;
  73.       Serial.write(1);
  74.     }
  75.   }
  76. }
  77.  
  78. char* strrev( char* s )
  79.   {
  80.   char  c;
  81.   char* s0 = s - 1;
  82.   char* s1 = s;
  83.  
  84.   /* Find the end of the string */
  85.   while (*s1) ++s1;
  86.  
  87.   /* Reverse it */
  88.   while (s1-- > ++s0)
  89.     {
  90.     c   = *s0;
  91.     *s0 = *s1;
  92.     *s1 =  c;
  93.     }
  94.  
  95.   return s;
  96.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement