Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Reddit double-button motor control v1.0
- 8 Feb 2020
- Description:
- i need it so if "button1" is pressed the turn motor for X amount of time,
- then if "button1" is pressed and held down then "button2" is pressed then turn motor on intill "button2" is released
- 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,
- but if i press button1 then button 2 shortly after it works?
- */
- //============ Mosfet Arduino ============
- // #include "OneButton.h" ------------- ??? was this supposed to do anything? There is no object instance declared? (I think)
- // ------------------------------------ is it from https://github.com/mathertel/OneButton ? You did not say where you got it from.
- // ------------------------------------ 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.
- // ------------------------------------ Library names aren't guaranteed to be unique, and other people who look at your code don't know which library you really used.
- int MOSFET = A0; // Mosfet pin A0 --------- I guess this is the motor control pin? Your code does not say.
- int led_pin = 13;
- int switchPin = A5; // Switch 1 ---------- I assume these are INPUT_PULLUP from the low==active code.
- int switchPin2 = A4; //Switch 2 ---------- I assume these are INPUT_PULLUP from the low==active code.
- //int startTime = millis;
- #include <SoftwareSerial.h>
- SoftwareSerial BT(0, 1);
- String bt = "";
- unsigned long startTime = millis();
- bool T1 = 0;
- float motor_run_time_1 = 4000; // The time for motor to run when button #1 is pressed only. Time value is in milliseconds.
- unsigned long motor_timer_begin_time = 0; // This is for using to check the motor_run_time_1 time.
- unsigned long motor_timer_current_time = 0; // This is for using to check the motor_run_time_1 time.
- int program_state = 0; // -------- This is a state machine variable.
- // zero = nothing happening.
- // 1 = change to this value when button 1 is pressed down (alone).
- // 2 = change to this value when button #2 is pressed down (with button 1).
- //============ Start Of Void Setup ============
- void setup()
- {
- pinMode(led_pin, OUTPUT);
- BT.begin(9600);
- Serial.begin(9600);
- bt = (40);
- pinMode(MOSFET, OUTPUT);
- pinMode(switchPin, INPUT);
- digitalWrite(switchPin, HIGH);
- pinMode(switchPin2, INPUT);
- // pinMode(switchPin2, OUTPUT);
- digitalWrite(switchPin2, HIGH);
- }
- //============ Void End ============
- //============ Start Of Void Loop ============
- void loop() {
- // Below checks if button 1 is pressed, but only if the motor is not running already.
- if (program_state == 0) {
- // Check button #1:
- if (digitalRead(switchPin) == LOW) {
- program_state = 1;
- start_motor_timer_button_1();
- Serial.println("button #1 pressed");
- }
- }
- // Below checks if button 2 is pressed, but only if button 1 is already pressed.
- if (program_state == 1) { // This means button 1 is already been pressed, and the motor timer is running.
- if (digitalRead(switchPin2) == LOW) { // This means button 2 is pressed.
- program_state = 2;
- Serial.println("button #1 & #2 pressed");
- }
- }
- if (program_state == 1) {
- // This is to automatically turn off the motor if button 1 is the only button pressed.
- check_motor_timer_button_1();
- }
- if (program_state == 2) {
- if (digitalRead(switchPin2) == HIGH) {
- turn_motor_off(); // This turns off the motor if button 2 is released.
- Serial.println("button #2 released");
- }
- }
- // Below is the section to read the serial messages while the motor is not running.
- // Note that I do not do anything with this part, you need to fix that yourself.
- if (program_state == 0 && BT.available()) {
- bt = BT.readString();
- Serial.println(bt);
- }
- /*
- //============ Switchpin pressed ============
- if (digitalRead(switchPin) == LOW) {
- delay(bt.toInt()); // How Long to Keep the motor running for
- startTime = millis();
- //get the start time before entering the loop
- while ( digitalRead(switchPin) == LOW && millis() < (startTime + 1000) && (T1 = 1)) {
- digitalWrite(MOSFET, HIGH );
- digitalWrite(led_pin, HIGH);
- }
- digitalWrite(led_pin, LOW);
- digitalWrite(MOSFET, LOW);
- //this is the first thing it will do after exiting the while loop
- }
- if (digitalRead (A1) == LOW && BT.available() ) {
- bt = BT.readString();
- Serial.println(bt);
- }
- // digitalWrite(MOSFET, LOW);
- */
- }
- void start_motor_timer_button_1() {
- // This starts the button-1 timer and turns the motor on.
- motor_timer_begin_time = millis();
- digitalWrite(MOSFET, HIGH);
- digitalWrite(led_pin, HIGH);
- Serial.println("button #1 motor started");
- }
- void check_motor_timer_button_1() {
- // This function runs the motor for the time limit after button #1 is pressed alone.
- motor_timer_current_time = millis();
- if (motor_timer_current_time >= motor_timer_begin_time) {
- if (motor_timer_current_time >= (motor_timer_begin_time + motor_run_time_1)) {
- turn_motor_off();
- // WE can put a message here for button #1, since only button #1 uses this to stop the motor.
- Serial.println("button #1 timer ended");
- }
- }
- else {
- motor_timer_begin_time = millis(); // Millis() rollover condition.
- }
- }
- void turn_motor_off() {
- // This is used to turn the motor off, and also to reset the program_state back to zero.
- // Button #1 and #2 use this function.
- digitalWrite(MOSFET, HIGH);
- digitalWrite(led_pin, HIGH);
- program_state = 0;
- }
- /*
- void callMillis(unsigned long x)
- {
- unsigned long presentMillis = millis();
- while (millis() - presentMillis != x)
- {
- }
- }
- */
- // ------- fini --------
Advertisement
Add Comment
Please, Sign In to add comment