Hello, I would like to ask for help. There is a motor control that works well with a switch. I would like to add a remote control switch. I tried several things but not really good. The program is uncertain or stuck. Can you tell me what I'm doing wrong?
//VALVE CONTROL//
int mOpen = 10;
int mClose = 11;
int mOpwm = 2;
int mCpwm = 3;
int Button = 7;
int RcButtonO = 5, LOW;
int RcButtonC = 6, LOW;
int Bled = 8;
int mState = LOW;
int ButtonCurrent;
long time = 0;
long debounce = 200;
void vOpen () {
digitalWrite (mOpwm, HIGH);
digitalWrite (mCpwm, HIGH);
analogWrite (mOpen, 255);
analogWrite (mClose, 0);
}
void vClose() {
digitalWrite (mOpwm, HIGH);
digitalWrite (mCpwm, HIGH);
analogWrite (mOpen, 0);
analogWrite (mClose, 255);
}
void vStop() {
digitalWrite (mOpwm, LOW);
digitalWrite (mCpwm, LOW);
analogWrite (mOpen, 0);
analogWrite (mClose, 0);
}
void setup() {
pinMode (mOpen, OUTPUT);
pinMode (mClose, OUTPUT);
pinMode (mOpwm, OUTPUT);
pinMode (mCpwm, OUTPUT);
pinMode (Button, INPUT);
pinMode (Bled, OUTPUT);
pinMode (RcButtonO, INPUT);
pinMode (RcButtonC, INPUT);
digitalWrite (Bled, LOW);
vClose ();
delay (1000);
vStop ();
}
void loop() {
ButtonCurrent = digitalRead (Button);
if ((millis () - time) > debounce)
{
if (ButtonCurrent == HIGH && mState == LOW)
{
digitalWrite (Bled, HIGH);
vOpen ();
delay (1000);
vStop ();
mState = HIGH;
delay (1000);
}
else if (ButtonCurrent == HIGH && mState == HIGH)
{
digitalWrite (Bled, LOW);
vClose ();
delay (1000);
vStop ();
mState = LOW;
delay (1000);
}
time = millis ();
}
if (digitalRead(RcButtonO) == HIGH)
{
digitalWrite (Bled, HIGH);
vOpen ();
delay (1000);
vStop ();
mState = HIGH;
delay (1000);
}
else if (digitalRead(RcButtonC) == HIGH)
{
digitalWrite (Bled, LOW);
vClose ();
delay (1000);
vStop ();
mState = LOW;
delay (1000);
}
}
Thank you very much!
George
jim-p
February 26, 2024, 9:09am
2
Your sketch does not compile.
Is this the right sketch?
Sorry. That's really bad. I fixed that error.
//VALVE CONTROL//
int mOpen = 10;
int mClose = 11;
int mOpwm = 2;
int mCpwm = 3;
int Button = 7;
int RcButtonO = 5;
int RcButtonC = 6;
int Bled = 8;
int mState = LOW;
int ButtonCurrent;
long time = 0;
long debounce = 200;
void vOpen () {
digitalWrite (mOpwm, HIGH);
digitalWrite (mCpwm, HIGH);
analogWrite (mOpen, 255);
analogWrite (mClose, 0);
}
void vClose() {
digitalWrite (mOpwm, HIGH);
digitalWrite (mCpwm, HIGH);
analogWrite (mOpen, 0);
analogWrite (mClose, 255);
}
void vStop() {
digitalWrite (mOpwm, LOW);
digitalWrite (mCpwm, LOW);
analogWrite (mOpen, 0);
analogWrite (mClose, 0);
}
void setup() {
pinMode (mOpen, OUTPUT);
pinMode (mClose, OUTPUT);
pinMode (mOpwm, OUTPUT);
pinMode (mCpwm, OUTPUT);
pinMode (Button, INPUT);
pinMode (Bled, OUTPUT);
pinMode (RcButtonO, INPUT);
pinMode (RcButtonC, INPUT);
digitalWrite (Bled, LOW);
vClose ();
delay (1000);
vStop ();
}
void loop() {
ButtonCurrent = digitalRead (Button);
if ((millis () - time) > debounce)
{
if (ButtonCurrent == HIGH && mState == LOW)
{
digitalWrite (Bled, HIGH);
vOpen ();
delay (1000);
vStop ();
mState = HIGH;
delay (1000);
}
else if (ButtonCurrent == HIGH && mState == HIGH)
{
digitalWrite (Bled, LOW);
vClose ();
delay (1000);
vStop ();
mState = LOW;
delay (1000);
}
time = millis ();
}
if (digitalRead(RcButtonO) == HIGH)
{
digitalWrite (Bled, HIGH);
vOpen ();
delay (1000);
vStop ();
mState = HIGH;
delay (1000);
}
else if (digitalRead(RcButtonC) == HIGH)
{
digitalWrite (Bled, LOW);
vClose ();
delay (1000);
vStop ();
mState = LOW;
delay (1000);
}
}
jim-p
February 26, 2024, 10:19am
4
Is this remote switch to do the same exact thing as Button?
It seem you have two remote switches. what is the purpose of each?
Yes, the remote switch does the same thing as the push button. But the remote switch moves with two buttons. It's either this or that switch. The system includes both.
Thank you for your help to me!
jim-p
February 26, 2024, 11:28am
6
Is this how the button is supposed to work?
Press once to open valve
Press again to close the valve.
Is it possible for RcButtonO and RcButtonC to both be high at the same time?
To see how debouncing is done, look at the example code in the IDE.
Find it on the menu under File->Examples->Digital
It is possible for both values to be HIGH. I already thought about that. But that's not the biggest problem. The motor periodically gets stuck in the closed position. Debounce works. If I remove the remote control from the program, it works perfectly. The remote control also works separately. Not good together...
jim-p
February 26, 2024, 12:38pm
8
nussbaum:
Debounce works
I don't see how. All your code does is checks the button state every 200ms
Are you sure you posted the correct code?
Thank you for your comment, I will improve this based on the description. I'll see what happens.
Hi!
This works, but i can only use the remote control after a minute. When it works, it's good.
const int mOpen = 10;
const int mClose = 11;
const int mOpwm = 2;
const int mCpwm = 3;
int mState = LOW;
const int Button = 12;
int LastButtonState = HIGH;
int CurrentButtonState = 0;
const int RcButtonO = 5;
const int RcButtonC = 6;
int RcButtonStateO;
int RcButtonStateC;
const int Bled = 8;
void vOpen () {
digitalWrite (mOpwm, HIGH);
digitalWrite (mCpwm, HIGH);
analogWrite (mOpen, 255);
analogWrite (mClose, 0);
}
void vClose() {
digitalWrite (mOpwm, HIGH);
digitalWrite (mCpwm, HIGH);
analogWrite (mOpen, 0);
analogWrite (mClose, 255);
}
void vStop() {
digitalWrite (mOpwm, LOW);
digitalWrite (mCpwm, LOW);
analogWrite (mOpen, 0);
analogWrite (mClose, 0);
}
void setup() {
pinMode (mOpen, OUTPUT);
pinMode (mClose, OUTPUT);
pinMode (mOpwm, OUTPUT);
pinMode (mCpwm, OUTPUT);
pinMode (Button, INPUT_PULLUP);
pinMode (Bled, OUTPUT);
pinMode (RcButtonO, INPUT);
pinMode (RcButtonC, INPUT);
digitalWrite (Bled, LOW);
vClose ();
delay (1000);
vStop ();
}
void loop() {
RcButtonStateO = digitalRead (RcButtonO);
RcButtonStateC = digitalRead (RcButtonC);
CurrentButtonState = digitalRead (Button);
if (LastButtonState == LOW && CurrentButtonState == HIGH)
{
if (mState == LOW)
{
digitalWrite (Bled, HIGH);
mState = HIGH;
vOpen ();
delay (1000);
vStop ();
delay (1000);
}
else
{
digitalWrite (Bled, LOW);
mState = LOW;
vClose ();
delay (1000);
vStop ();
delay (1000);
}
}
if (RcButtonStateO == HIGH && mState == LOW)
{
digitalWrite (Bled, HIGH);
mState = HIGH;
vOpen ();
delay (1000);
vStop ();
delay (1000);
}
if (RcButtonStateC == HIGH && mState == HIGH)
{
digitalWrite (Bled, LOW);
mState = LOW;
vClose ();
delay (1000);
vStop ();
delay (1000);
}
LastButtonState = CurrentButtonState;
}
Please elaborate. Do you mean the remote control doesn't work for the first minute after reset?
Or that it takes a minute between times it works?
Did you say everything is fine after this minute has elapsed?
a7
alto777:
Please elaborate. Do you mean the remote control doesn't work for the first minute after reset?
Or that it takes a minute between times it works?
Did you say everything is fine after this minute has elapsed?
a7
It's in a car. After switching on, only the push button works for one minute. After about a minute, both the remote control and the push button work as they should. Everything is fine from there.
nussbaum:
It's in a car.
At a glance, nothing in the code I see would account for that problem. I'll look to see if you have drawn a diagram of who is talking to what, if you haven't please do.
It seems like it is a problem outside the context you have provided. You cou,d always just meditate for a minute while everyone agrees it's time to roach and roll.
a7
system
Closed
August 31, 2024, 4:43pm
14
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.