skizziks_53

Reddit two-button motor control 1.0

Feb 8th, 2020
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.91 KB | None | 0 0
  1. /*
  2.   Reddit double-button motor control v1.0
  3.   8 Feb 2020
  4.  
  5.   Description:
  6.   i need it so if "button1" is pressed the turn motor for X amount of time,
  7.   then if "button1" is pressed and held down then "button2" is pressed then turn motor on intill "button2" is released
  8.   With my current code it dose what i want it to but if i press button 1 then wait before i press button 2 it dose nothing,
  9.   but if i press button1 then button 2 shortly after it works?
  10. */
  11.  
  12. //============ Mosfet Arduino ============
  13. // #include "OneButton.h" ------------- ??? was this supposed to do anything? There is no object instance declared? (I think)
  14. // ------------------------------------ is it from https://github.com/mathertel/OneButton ? You did not say where you got it from.
  15. // ------------------------------------ If you use a non-standard library, it is ALWAYS good to put a web link so that you always know where you got it from.
  16. // ------------------------------------ Library names aren't guaranteed to be unique, and other people who look at your code don't know which library you really used.
  17.  
  18. int MOSFET = A0;                   // Mosfet pin A0 --------- I guess this is the motor control pin? Your code does not say.
  19. int led_pin = 13;
  20.  
  21. int switchPin = A5;               // Switch 1 ---------- I assume these are INPUT_PULLUP from the low==active code.
  22. int switchPin2 = A4;             //Switch   2 ---------- I assume these are INPUT_PULLUP from the low==active code.
  23. //int startTime = millis;
  24. #include <SoftwareSerial.h>
  25. SoftwareSerial BT(0, 1);
  26. String bt = "";
  27. unsigned long startTime = millis();
  28. bool T1 = 0;
  29.  
  30.  
  31.  
  32. float motor_run_time_1 = 4000; // The time for motor to run when button #1 is pressed only. Time value is in milliseconds.
  33. unsigned long motor_timer_begin_time = 0; // This is for using to check the motor_run_time_1 time.
  34. unsigned long motor_timer_current_time = 0; // This is for using to check the motor_run_time_1 time.
  35.  
  36. int program_state = 0; // -------- This is a state machine variable.
  37. // zero = nothing happening.
  38. // 1 = change to this value when button 1 is pressed down (alone).
  39. // 2 = change to this value when button #2 is pressed down (with button 1).
  40.  
  41.  
  42. //============  Start Of Void Setup ============
  43. void setup()
  44. {
  45.   pinMode(led_pin, OUTPUT);
  46.   BT.begin(9600);
  47.   Serial.begin(9600);
  48.   bt = (40);
  49.   pinMode(MOSFET, OUTPUT);
  50.   pinMode(switchPin, INPUT);
  51.   digitalWrite(switchPin, HIGH);
  52.   pinMode(switchPin2, INPUT);
  53.   // pinMode(switchPin2, OUTPUT);
  54.   digitalWrite(switchPin2, HIGH);
  55. }
  56. //============  Void End ============
  57. //============  Start Of Void Loop ============
  58. void loop() {
  59.  
  60.  
  61.   // Below checks if button 1 is pressed, but only if the motor is not running already.
  62.   if (program_state == 0) {
  63.     // Check button #1:
  64.     if (digitalRead(switchPin) == LOW) {
  65.       program_state = 1;
  66.       start_motor_timer_button_1();
  67.       Serial.println("button #1 pressed");
  68.     }
  69.   }
  70.  
  71.  
  72.   // Below checks if button 2 is pressed, but only if button 1 is already pressed.
  73.   if (program_state == 1) { // This means button 1 is already been pressed, and the motor timer is running.
  74.     if (digitalRead(switchPin2) == LOW) { // This means button 2 is pressed.
  75.       program_state = 2;
  76.       Serial.println("button #1 & #2 pressed");
  77.     }
  78.   }
  79.  
  80.  
  81.   if (program_state == 1) {
  82.     // This is to automatically turn off the motor if button 1 is the only button pressed.
  83.     check_motor_timer_button_1();
  84.   }
  85.  
  86.  
  87.   if (program_state == 2) {
  88.     if (digitalRead(switchPin2) == HIGH) {
  89.       turn_motor_off(); // This turns off the motor if button 2 is released.
  90.       Serial.println("button #2 released");
  91.     }
  92.   }
  93.  
  94.  
  95.   // Below is the section to read the serial messages while the motor is not running.
  96.   // Note that I do not do anything with this part, you need to fix that yourself.
  97.   if (program_state == 0 && BT.available()) {
  98.     bt = BT.readString();
  99.     Serial.println(bt);
  100.   }
  101.  
  102.   /*
  103.     //============   Switchpin pressed ============
  104.     if (digitalRead(switchPin) == LOW) {
  105.  
  106.     delay(bt.toInt());                                                         // How Long to Keep the motor running for
  107.     startTime = millis();
  108.     //get the start time before entering the loop
  109.  
  110.     while ( digitalRead(switchPin) == LOW && millis() < (startTime + 1000) && (T1 = 1)) {
  111.       digitalWrite(MOSFET, HIGH );
  112.       digitalWrite(led_pin, HIGH);
  113.     }
  114.     digitalWrite(led_pin, LOW);
  115.     digitalWrite(MOSFET, LOW);
  116.  
  117.     //this is the first thing it will do after exiting the while loop
  118.  
  119.     }
  120.  
  121.  
  122.     if (digitalRead (A1) == LOW && BT.available() ) {
  123.     bt = BT.readString();
  124.     Serial.println(bt);
  125.     }
  126.  
  127.     // digitalWrite(MOSFET, LOW);
  128.   */
  129. }
  130.  
  131. void start_motor_timer_button_1() {
  132.   // This starts the button-1 timer and turns the motor on.
  133.   motor_timer_begin_time = millis();
  134.   digitalWrite(MOSFET, HIGH);
  135.   digitalWrite(led_pin, HIGH);
  136.   Serial.println("button #1 motor started");
  137. }
  138.  
  139. void check_motor_timer_button_1() {
  140.   // This function runs the motor for the time limit after button #1 is pressed alone.
  141.   motor_timer_current_time = millis();
  142.   if (motor_timer_current_time >= motor_timer_begin_time) {
  143.     if (motor_timer_current_time >= (motor_timer_begin_time + motor_run_time_1)) {
  144.       turn_motor_off();
  145.       // WE can put a message here for button #1, since only button #1 uses this to stop the motor.
  146.       Serial.println("button #1 timer ended");
  147.     }
  148.   }
  149.   else {
  150.     motor_timer_begin_time = millis(); // Millis() rollover condition.
  151.   }
  152. }
  153.  
  154. void turn_motor_off() {
  155.   // This is used to turn the motor off, and also to reset the program_state back to zero.
  156.   // Button #1 and #2 use this function.
  157.   digitalWrite(MOSFET, HIGH);
  158.   digitalWrite(led_pin, HIGH);
  159.   program_state = 0;
  160. }
  161.  
  162. /*
  163.   void callMillis(unsigned long x)
  164.   {
  165.   unsigned long presentMillis = millis();
  166.   while (millis() - presentMillis != x)
  167.   {
  168.  
  169.   }
  170.   }
  171. */
  172.  
  173. // ------- fini --------
Advertisement
Add Comment
Please, Sign In to add comment