Advertisement
skizziks_53

function division demo code #1

Jul 16th, 2019
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.21 KB | None | 0 0
  1. /*
  2.  
  3.    THere is other stuff to be put here, but for clarity I deleted it. You would need to put all that back in.
  4.  
  5. */
  6.  
  7. bool button_is_enabled = true;
  8. int button_check_interval = 500; // time is in milliseconds: this is the time interval that the button will be checked at.
  9. unsigned long buttonpress_begin_time = 0;
  10. unsigned long buttonpress_current_time = 0;
  11.  
  12. bool servo_in_use = false; // If the servo is not in use at all, this variable allows totally skipping all the servo code.
  13.  
  14. int servo_current_position = 0; // This is always set to where the servo is actually at.
  15. int servo_target_position = 0; // This is set to where you want the servo to end up at.
  16.  
  17. bool servo_is_waiting = false;
  18. int servo_waiting_period = 25; // the servo's waiting time in milliseconds
  19. unsigned long servo_waiting_begin_time = 0;
  20. unsigned long servo_waiting_current_time = 0;
  21.  
  22.  
  23.  
  24. // ,,,,,other stuff deleted also,,,,,,
  25.  
  26. void setup() {
  27.   Serial.begin(9600);
  28.   //pinMode(buttonPin, INPUT_PULLUP); ---------- commented out, just because I removed the declaration
  29.   //myservo.attach(sprinklerServo1); ---------- commented out, just because I removed the declaration
  30. }
  31.  
  32. void loop() {
  33.  
  34.   //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  35.   // The code section below only handles checking the button.
  36.   // The button is only checked for a new value on the desired time interval.
  37.   if (button_is_enabled == true) {
  38.     updateButton();
  39.     // also you would change button_is_enabled to false here, and record the current millis() time in buttonpress_begin_time
  40.   }
  41.   else {
  42.     check_to_enable_button();
  43.   }
  44.   //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  45.  
  46.  
  47.   //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  48.   // The code section below only handles operating the servo.
  49.   if (servo_in_use == true) {
  50.     if (servo_is_waiting == false) { // If this is false, then the servo can be moved.
  51.       check_servo_position();
  52.       // also you would change servo_is_waiting to true here, and record the current millis() time in servo_waiting_begin_time
  53.     }
  54.     else { // if servo_is_waiting == true , then this part checks if enough time has passed to allow moving the servo again
  55.       check_to_enable_servo();
  56.     }
  57.   }
  58.   //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  59.  
  60. }
  61.  
  62.  
  63. void updateButton() {
  64.   // code to get the new button reading is here
  65.   // if button was pressed, then--
  66.   //    change servo_target_position to a different value
  67.   //    set servo_in_use to true
  68. }
  69.  
  70.  
  71. void check_to_enable_button() {
  72.   // this is the code to re-enable the button if the button_check_interval time interval has passed
  73.   // buttonpress_current_time = millis();
  74.   // then compare buttonpress_begin_time to buttonpress_current_time to see if { button_check_interval } amount of time has passed
  75.   //     if it has, then re-enable the button so it gets checked again
  76. }
  77.  
  78.  
  79. void check_servo_position() {
  80.   // this function is the one that changes the servo's position. this does not handle the servo's time delay.
  81.   if (servo_current_position != servo_target_position) {
  82.     // If the servo_current_position is not the same as the servo_target_position, then the servo needs to be moved still.
  83.     change_servo_position();
  84.   }
  85.   else {
  86.     // If servo_current_position is the same value as servo_target_position, then the servo is done moving and can be 'shut off'.
  87.     servo_in_use = false;
  88.   }
  89. }
  90.  
  91.  
  92. void change_servo_position() {
  93.   // This is the fuction that would write a new PWM value to servo_current_position and then to the PWM pin, to change the servo's position.
  94.   // The new PWM value would need to be closer to servo_target_position than the previous PWM value was.
  95. }
  96.  
  97.  
  98. void check_to_enable_servo() {
  99.   // this function is the one for checking if the servo has been disabled long enough that it can be re-enabled (this handles the servo's time delay)
  100.   // servo_waiting_current_time = millis();
  101.   // then compare servo_waiting_begin_time to servo_waiting_current_time to see if { servo_waiting_period } amount of time has passed
  102.   //     if it has, then change servo_is_waiting to false
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement