skizziks_53

Reddit Arduino Servo Thing v1.0

Jul 2nd, 2019
1,975
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.77 KB | None | 0 0
  1. /*
  2.    Reddit Arduino Servo attempt
  3.    July 2, 2019
  4.    { wayward parentheses, mostly }
  5. */
  6.  
  7. #include <Servo.h>
  8. Servo myservo; // create servo object to control a servo
  9. const int trigPin = 2;
  10. const int echoPin = 4;
  11.  
  12.  
  13. // { void setup() // -- This curly brace should be after the parentheses, not at the start of the whole line.
  14. void setup() { // -- corrected line
  15.   // initialize serial communication:
  16.   Serial.begin(9600);
  17.   myservo.attach(9); // attaches the servo on pin 9 to the servo object
  18. }
  19.  
  20. // { void loop() // -- This curly brace should be after the parentheses, not at the start of the whole line.
  21. void loop() { // -- corrected line
  22.  
  23.   // and the distance result in centimeters:
  24.   long duration, cm;
  25.   pinMode(trigPin, OUTPUT);
  26.   digitalWrite(trigPin, LOW);
  27.   delayMicroseconds(2); // This isn't going to work like you think it will.
  28.   // If I remember correctly, the smallest unit of time that the Uno and Mega chips can normally clock is 4 microseconds.
  29.   digitalWrite(trigPin, HIGH);
  30.   delayMicroseconds(20);
  31.   digitalWrite(trigPin, LOW);
  32.   pinMode(echoPin, INPUT);
  33.   duration = pulseIn(echoPin, HIGH);
  34.  
  35.   // convert the time into a distance
  36.  
  37.   cm = microsecondsToCentimeters(duration); // I do not know why it said that "cm was not declared in this scope"...???
  38.   //                                           When I fixed the other curly braces errors, it no longer gave that message.
  39.  
  40.   // the condition for the distance
  41.  
  42.  
  43.   if (cm > 7 && cm < 14) {
  44.     myservo.write(140); // sets the servo position according to the scaled value
  45.     delay(4000);
  46.   }
  47.   else if (cm < 8) {
  48.     myservo.write(40); // sets the servo position according to the scaled value
  49.     delay(100);
  50.   }
  51.   else {
  52.     myservo.write(40); // sets the servo position according to the scaled value
  53.     delay(100);
  54.   }
  55.   Serial.print(cm);
  56.   Serial.print("cm");
  57.   Serial.println();
  58.   delay(100);
  59. }
  60.  
  61.  
  62. //{ long microsecondsToCentimeters(long microseconds) // -- This curly brace should be after the parentheses, not at the start of the whole line.
  63. long microsecondsToCentimeters(long microseconds) { // -- corrected line
  64.  
  65.   // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  66.   // The ping travels out and back, so to find the distance of the
  67.   // object we take half of the distance travelled.
  68.  
  69.   /*
  70.     The error given for the line below ( sketch_jun29a:103:1: error: a function-definition is not allowed here before 'return' )
  71.     may have been related to the incorrectly-placed curly brace at the beginning of the function.
  72.     When I moved that curly brace to the proper location, this error message was no longer present.
  73.   */
  74.   return microseconds / 29 / 2; // If this doesn't give a correct answer, you may need to use parentheses to establish the order of operation.
  75. }
Add Comment
Please, Sign In to add comment