I have 2 buttons that I need to control 3 outputs with. If one button is pressed its 2 outputs if 2 buttons are pressed then it only needs to output 1 output. How would the best way be to do this ?
Where are you stuck ?
Can you read the state of the 2 inputs into 2 variables ?
Can you compare the state of 2 states using && and || ?
Can you turn on an LED ?
I was told an arduino would be my best way to accomplish this. I’m savvy with computers but have never messed with arduino. I have been watching YouTube channels and trying to ready over other people’s uses but haven’t figured out what to get or how to code it to work.
What do you mean by “best way” ?
Sorry maby not “best way” maby I should of said a way. Not best way.
noiasca:
based on your logic It might even be possible without any micro controller
Given the (completely inadequate) description, and
dragginncrds:
I was told an Arduino would be my best way to accomplish this.code it to work.
For pure combinatorial problems, that is where sequence is unimportant, while a microprocessor can (of course, obviously! ) do it, it is essentially unnecessary.
As with this problem recently posed, an E(E)PROM will perform the task perfectly well.
In fact, an EEPROM plus a latch and a clock makes a state machine which is capable of many tasks. You effectively have an 8-bit "RAM" and whatever you can do with that (which is a surprising amount; 256 "states"), will work.
dragginncrds:
I was told an arduino would be my best way to accomplish this. I’m savvy with computers but have never messed with arduino. I have been watching YouTube channels and trying to ready over other people’s uses but haven’t figured out what to get or how to code it to work.
Hello dragginncrds,
I'm not sure what you were hoping for in terms of a response but I suspect you are not getting it. What you are asking is trivial and you should be able to accomplish it having done the various tutorials in the Arduino IDE, on this web site and elsewhere. Please do the tutorials, make an attempt at solving your problem and post here if you get stuck. I get the impression you were hoping someone would just give you the answer; sorry, but it does not work like that here unless you post in 'gigs and collaborations' and indicate how much you are willing to pay.
Have fun
PerryBebbington:
I get the impression you were hoping someone would just give you the answer;
(S)He hasn't even posted the question yet!
(deleted)
I have never worked with a arduino in particular. I’m not asking for someone to write it for me just asking to be pointed in the right direction. As far as the button presses go I would need this
Code:
buttonA buttonB OutA OutB OutC
off off off off off
on off. On. On. Off
off on. Off. On. On
on on. Off. On. Off
I have been going over YouTube videos and such but I really don’t know which board would be best or would I need other components or what exactly I need to buy
I really don't know which board would be best or would I need other components or what exactly I need to buy.
Uno or Mega for learning. Personally I prefer the Mega because it has more of everything, especially serial ports. Follow the tutorials then try to write some code to do what you want once you understand the basics from the tutorials.
buttonA buttonB OutA OutB OutC
off off off off off
on off. On. On. Off
off on. Off. On. On
on on. Off. On. Off
OutA = buttonA & !buttonB
OutB = buttonA | buttonB
OutC = !buttonA & buttonB
buttonA buttonB OutA OutB OutC
off off off off off
on off. On. On. Off
off on. Off. On. On
on on. Off. On. Off
Start by deciding whether a button input being "on" is HIGH or LOW. It may seem perverse, but it easier in the long run if on is LOW and that the button is wired to take the input LOW when pressed and use INPUT_PULLUP in the button pinModes() to turn on the built in pullup resistors to keep the input HIGH when the button is not pressed
Similarly, depending on how the LEDs are wired is "on" HIGH or LOW. In this case it does not matter as long as the LEDs have a current limiting resistor in series with them but you need to know in order to write the program
As to the logic, what should happen if a user presses buttonA, holds it down and then presses buttonB ? Is there a time limit for pressing the second button before the LED states are set or should the program respond immediately to button presses ?
Here's my first attempt:
You have made the mistake of assuming that the OP will not suddenly announce a requirement to do something else in the sketch such as reading user input to change the logic, count the number of button presses, blink an LED or one of many other possibilities as yet unknown.
If, as is often the case, this is actually a school assignment as an early part of a programming course then a hardware solution is going to lead nowhere. If, however, it is a stand alone requirement then a hardware solution would be perfect as long as it is acceptable for the outputs to change as the first then second button is pressed which has not been clarified
@TO:
when you have decided about your logic, it becomes straight forward to write some code.
It's not optimized in any way. It's just written down what you have specified:
/*
buttonA buttonB OutA OutB OutC
off off off off off
on off. On. On. Off
off on. Off. On. On
on on. Off. On. Off
by noiasca
https://forum.arduino.cc/index.php?topic=694710.0
*/
const byte buttonA = A0; // Input Pin
const byte buttonB = A1; // Input Pin
const byte outA = 2; // Output Pin
const byte outB = 3; // Output Pin
const byte outC = 4; // Output Pin
const bool isOn = LOW; // as we are using INPUT_PULLUP we need "invers" logic when reading the inputs.
const bool isOff = HIGH;
void setup() {
Serial.begin(115200);
pinMode(buttonA, INPUT_PULLUP);
pinMode(buttonB, INPUT_PULLUP);
pinMode(outA, OUTPUT);
pinMode(outB, OUTPUT);
pinMode(outC, OUTPUT);
}
void loop() {
//buttonA buttonB OutA OutB OutC
// off off off off off
if (digitalRead(buttonA) == isOff && digitalRead(buttonB) == isOff)
{
digitalWrite(outA, LOW);
digitalWrite(outB, LOW);
digitalWrite(outC, LOW);
}
//on off. On. On. Off
if (digitalRead(buttonA) == isOn && digitalRead(buttonB) == isOff)
{
Serial.println(F("A -"));
digitalWrite(outA, HIGH);
digitalWrite(outB, HIGH);
digitalWrite(outC, LOW);
}
//off on. Off. On. On
if (digitalRead(buttonA) == isOff && digitalRead(buttonB) == isOn)
{
Serial.println(F("- B"));
digitalWrite(outA, LOW);
digitalWrite(outB, HIGH);
digitalWrite(outC, HIGH);
}
//on on. Off. On. Off
if (digitalRead(buttonA) == isOn && digitalRead(buttonB) == isOn)
{
Serial.println(F("A B"));
digitalWrite(outA, LOW);
digitalWrite(outB, HIGH);
digitalWrite(outC, LOW);
}
}
As PaulRB has already shown, a microcontroller is overkill. You can achieve the same with two IC's without any line of code.
Karma+ for PaulRB!!!
UKHeliBob:
You have made the mistake
Frankly spoken: the only mistake I see in this topic, are such words coming from a global moderator, blaming someone to have done something wrong.
I stand by what I said although it was somewhat tongue in cheek.
I await with interest the answers to my questions in the final paragraph of reply #14
I’m not in school it’s strictly for a project I’m working on personally. As far as button input I can do positive or negative inputs. Which ever is easiest. The buttons can be pressed separate or at the same time as to which then I would prefer to have little to no lag in the function changes. As far as outputs I’m driving relays with them so how ever it needs to be for that. Could be a ground output or a positive output. On my control board I have 3 buttons. 2 which go straight to the input and then one that has diodes to tie the other two together so when that button is pressed the other two are pressed at the exact same time. Currently it is manually controlled inputs. I will eventually switch to a electronic control that will be similar just a different system
That hardware solution might actually work better. I’m up to try either. I guess I’ll order up some 5prong relays and give it a go.