Shield motor with two ways run to operate with a simple trigger press button

hello guys

I'm trying use uno with shield motor with two ways run to operate with a simple trigger press button but I'm not shore if I'm doing all right
can some one help me read my project to find out why is not running??

this is the file:

#include <EEPROM.h>

const int buttonPin = 2; // Pino do botão de exemplo
const int valvePin = 3; // Pino da válvula de exemplo
bool valveOpen = false; // Flag para o estado da válvula
unsigned long openStartTime = 0; // Tempo em que a válvula foi última vez aberta ou fechada
const unsigned long openDuration = 3000; // Duração durante a qual a válvula permanece aberta (em milissegundos)
const unsigned long closeDuration = 2000; // Duração durante a qual a válvula permanece fechada (em milissegundos)

void setup() {
  pinMode(buttonPin, INPUT_PULLUP); // Pino do botão como entrada com resistor pull-up interno
  pinMode(valvePin, OUTPUT); // Pino da válvula como saída

  // Lê o último estado conhecido da válvula da EEPROM
  EEPROM.get(0, valveOpen);

  // Define o estado inicial da válvula
  digitalWrite(valvePin, valveOpen ? HIGH : LOW);

  // Verifica se a válvula estava aberta antes da queda de energia, fecha-a se necessário
  if (valveOpen) {
    closeValve();
  }
}

void loop() {
  // Verifica se o botão foi pressionado
  if (digitalRead(buttonPin) == LOW) {
    // Botão pressionado, inverte o estado da válvula e inicia o temporizador
    valveOpen = !valveOpen;
    digitalWrite(valvePin, valveOpen ? HIGH : LOW);
    openStartTime = millis();
    // Salva o estado da válvula na EEPROM
    EEPROM.put(0, valveOpen);
    // Aguarda um curto período para eliminar o efeito do botão
    delay(100);
  }
  
  // Verifica se a válvula está aberta e é hora de fechá-la
  if (valveOpen && millis() - openStartTime >= openDuration) {
    // Fecha a válvula
    closeValve();
  }
  
  // Verifica se a válvula está fechada e é hora de abri-la novamente
  if (!valveOpen && millis() - openStartTime >= closeDuration) {
    // Abre a válvula
    openValve();
  }
  
  // Verifica se houve uma queda de energia
  if (valveOpen && millis() < 2000) { // Supondo que a energia é restaurada dentro de 2 segundos
    // Fecha a válvula imediatamente por segurança
    closeValve();
  }
}

void closeValve() {
  // Fecha a válvula
  valveOpen = false;
  digitalWrite(valvePin, LOW);
  // Salva o estado da válvula na EEPROM
  EEPROM.put(0, valveOpen);
}

void openValve() {
  // Abre a válvula
  valveOpen = true;
  digitalWrite(valvePin, HIGH);

in the end wen i upload for the arduino nothing work...
thanks for the time

sheersTexto pré-formatado

"Nothing" indicates you have not uploaded your sketch to your Arduino. Can you better describe what you observe. Starting with a drawing showing how you have everything connected.

1 Like

Among the advice for using forum members are prompted to use a title telling what it's all about. Then new members can search and likely find a solution.

You should search for Help me here please and see what help You find,

Here's some advice telling how to post questions: How to get the best out of this forum - Using Arduino / Project Guidance - Arduino Forum

1 Like

Very good.

1 Like

Thank You.

Too often postings look like nonsense, garbage, trash, and wastes helpers time reading them.
Maybe a new topic should be created: Help me to create a proper question...

1 Like

I have plugged the power directly to the motor shield because I'm using 12v and to M1 terminals the dc motor for push button have connected GND to the extra pins available to soldering and the other wire to trigger pin 2 wen i push the button. in power i have all positive and negative in the right place and because the motor is an dc motor i think is not important to have the wires in order, but i have looking for the power in that M1 terminals and i didn't found nothing... i have a generic uno with all sow motor shield plugged, for help have installed the CH34 drivers running 64 bit in the end i think i made all the work here and it seems that wen i upload the program never prompt issue's
is this happening to some of you to?

you right, this is just a motor that goes forward and reverse only if i pulse the trigger and with a single press button to avoid extras,

hey mister you only have to be more polite you know? this is not a circus but it takes place wen your talk like a clown... go to do your work and leave alone the best party citizens. you waste my time... Use Your knowledge. If that’s not enough, look for education.
Having knowledge, think outside the box to gain more of it.

Welp…. I am not a great coder but i see a lot wrong with your code. you may be stressed with this project and deadline is fast approaching BUT There is a better way to conduct yourself when people are trying to help…

I for one will not be helping BUT maybe if you regroup, take advice from above and apologize. Someone may be willing to help

I'm trying to accomplish my project and not loosing time with some arrogant people here i hope all of you have great projects and help the others if not inconvenient but i fallow my way wen i have to without step in the others

Could You be so kind and read the advice given, and use it and avoid wasting helpers time?
Good nigjt.

You should make a drawing of your wiring. Draw a few boxes. Label the boxes. Write pin numbers and names in the boxes. Connect the pins with lines. Upload a picture of the drawing.

No, it is the other way around. Every person who has ever needed help must show their work in drawing (schematic), code (program) and description. You described your issue and posted your code, but missed the drawing. You will benefit by following these rules that you agreed to when you signed up for the forum. Railroad was just repeating the rules that you failed to follow. Do you understand this? He did not waste your time, you wasted his because he (we) had to ask for you to follow the rules.

I use google.translate.com for international correspondence. I could not understand your description. Write in your natural language and use "translate" to help the communication. You do not need to translate the program code.

Are you sure you want to constantly update openStartTime ?

This isn't doing what you think it is

If you don't want to accept help from volunteers, you should look for another form or hobby.
Cooking or collecting stamps is also nice.

Hello 2112 the idea is to ensure that the arduino recognize where is the valve and your position considering that eventually in a unexpected shutdown I can get the valve in the safe mode that is closed
In the end if the valve is opening and I have a shutdown the module can analise the moment and close the same time that he spends until that moment but if she is closing at this time she will get power to get the state close
I think I have explain you what I’m trying to achieve here
In the end I have a power bank to ensure that the safe mode is maintained if occurs a shutdown of primary energy
It’s a kind tricky but I want him to recognize power losts and able to do a controlled operation to safe mode state and after that he shut it down himself

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.