M
M
Max Darkleviathan2021-08-08 11:05:15
Arduino
Max Darkleviathan, 2021-08-08 11:05:15

Why does the button conflict with the motor on the Arduino?

Here is my sketch:

int analogPin = A0;
int val = 0;
#include "GyverMotor.h"

GMotor motor(DRIVER3WIRE, 7, 8, 9, HIGH);

unsigned long time_work;
int tester;

void setup() {
  Serial.begin(9600);
  motor.setLevel(HIGH);

  //motor.setSmoothSpeed(40);
  // ключ на старт!
  motor.setMode(FORWARD);
  //motor.setMinDuty(50);

}

void loop() {
  val = analogRead(analogPin);
  //Serial.println(val);

  if (val < 700) {tester = 1;} // кнопка включена
  if (val >= 700) {tester = 3;} // кнопка выключена
  
  if (tester == 1 && millis() - time_work > 5000) {
      time_work = millis();
      tester = 0;
      Serial.println("step1");
      motor.smoothTick(255);
    }

    if (tester == 0 && millis() - time_work > 5000) {
      time_work = millis();
      tester = 1;
      Serial.println("step2");
      motor.smoothTick(0);
    }

}


This kind of problem:
If the button is off, the tester variable = 3,
The engine fires, after pressing the button, which changes the value of the variable to 1, and after working out the condition, it changes to 0 and so on in a circle. For clarity, I write step1, step2 to the console.
So if the button is connected, only step1 works all the time, which is not clear why, the variable changes.

If I ignore the button and set initially tester = 1 , the start/stop veb algorithm works as it should. Where could be the problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
evgeniy_lm, 2021-08-08
@darkleviathan

And what kind of perversion do you have with connecting a button?
In principle, it is possible (but not necessary) to connect 4-5 buttons to the analog input. For one button, this is idiotic.
Look at the value of val in the plotter, I think it will be interesting
in the sketch you have if (tester == 1 and if (tester == 0 about tester == 3 your sketch knows nothing

A
Anton, 2021-11-03
@KJhas

and what is not so?
in the loop after not pressing the button tester=1 -> and 5s if(tester==1 -> timework=milis() and everything repeats,
if it is pressed, we do nothing at all. The second if will never work.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question