I'm not really sure why my code isn't working. I've been comparing it to the simple 'Button' sketch, and.. Well, I dunno, but mine seems to follow it fairly well I'd say. I just can't get it to work. Anyone have any idea?
const int button1(2);
const int button2(3);
const int m1(4);
const int m2(5);
const int m3(6);
const int data(7);
int buttonState1 = 0;
int buttonState2 = 0;
void setup()
{
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
digitalWrite(buttonState1 && buttonState2 && data && m1 && m2 && m3, LOW);
}
void loop()
{
buttonState1 == digitalRead(button1);
buttonState2 == digitalRead(button2);
if (buttonState1 == HIGH){
digitalWrite(buttonState1, HIGH);
digitalWrite(m1, HIGH);
delay(2000);
digitalWrite(m2, HIGH);
delay(5000);
digitalWrite(m3, HIGH);
delay(8000);
}
if (buttonState1 && buttonState2 == HIGH){
digitalWrite(data, HIGH);
delay(5000);
digitalWrite(buttonState1 && buttonState2 && data && m1 && m2 && m3, LOW);
}
}
My problem is, buttons. Not working. If I make Pin 2 high, nothing happens. I need it to also stay high until I have both pins high. After that, I'd have them both go low. I just can't figure out how to do it.
Thanks in advance, guys.
What's that?
The best you could hope for with that is to set either pin 0 or pin 1 LOW.
I don't think that's what you want to do.
Have a look at digitalWrite
Maybe I see && as an almighty shortcut? I'm assuming from those two posts that it absolutely isn't. I'll just need to make it longer, with no &&'s, right? As in digitalWrite(m1, LOW); and do that for each one?
Still, I don't understand why nothing is working after I make Pin 2 high. Pin 2 being button1, and buttonState1 having it's value. Do either of you know?
You code is a little untidy - you've given the pins nice names when you declared them and used them in "loop()", but not in "setup()".
When things are consistent and tidied-up and you've sorted out your assignment/comparison confusion, maybe you'll see the problem.
digitalWrite(buttonState1, HIGH);
I think you're confused about the identity of the pin, and the value you write to it.
buttonState1 should only ever have the values HIGH (1) or LOW (0), and as I said before, you don't want to be writing to pins 0 or 1.
The compiler doesn't interpret && in the same way you do. It is the compiler's interpretation, though, that defines what really happens.
I'll just need to make it longer, with no &&'s, right?
Yes, and no. You still need the && in the if statements, if you want the block of code to be executed only if buttonState1 is HIGH AND buttonState2 is HIGH.
As in digitalWrite(m1, LOW); and do that for each one?
Yes.
Still, I don't understand why nothing is working after I make Pin 2 high. Pin 2 being button1, and buttonState1 having it's value. Do either of you know?
I think so, but you need to define under what conditions you want this block of code executed:
I tidied it up, and changed some stuff, but still no joy. Thanks for Yoda-ing me, though. It's better to figure it out with tips, than to be given the answer!
const int button1(2);
const int button2(3);
const int m1(4);
const int m2(5);
const int m3(6);
const int data(7);
int buttonState1 = LOW;
int buttonState2 = LOW;
void setup()
{
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(m1, OUTPUT);
pinMode(m2, OUTPUT);
pinMode(m3, OUTPUT);
pinMode(data, OUTPUT);
digitalWrite(buttonState1, LOW);
digitalWrite(buttonState2, LOW);
digitalWrite(m1, LOW);
digitalWrite(m2, LOW);
digitalWrite(m3, LOW);
digitalWrite(data, LOW);
}
void loop()
{
buttonState1 == digitalRead(button1);
buttonState2 == digitalRead(button2);
if (buttonState1 == HIGH){
digitalWrite(buttonState1, HIGH);
digitalWrite(m1, HIGH);
delay(2000);
digitalWrite(m2, HIGH);
delay(5000);
digitalWrite(m3, HIGH);
delay(8000);
}
if (buttonState1 == HIGH && buttonState2 == HIGH){
digitalWrite(data, HIGH);
delay(5000);
digitalWrite(buttonState1, LOW);
digitalWrite(buttonState2, LOW);
digitalWrite(m1, LOW);
digitalWrite(m2, LOW);
digitalWrite(m3, LOW);
digitalWrite(data, LOW);
}
}
What do you think of it now, though?
Ooh, new post.
Thanks, PaulS. Eh. I'm not quite sure I understand what you mean by 'when the compiler knows' it should be executed.
Sorry if I'm a bit slow at this. I don't work at this often, and I've had no training. I'm just trying to learn as I go along. You're providing teaching, however, and that's much appreciated!
if (buttonState1 == HIGH){
digitalWrite(buttonState1, HIGH);
See my comments about the identity of the pin and the value you read/write from/to it.
Imagine "buttonState1" does equal HIGH (1). digitalWrite(buttonState1, HIGH)
is then the same as digitalWrite(HIGH, HIGH)
See the problem?
The == operator is a comparison operator. This is comparing the value read from the pin to the value in the variable, and taking no action if the result of the comparison is true or false. You want a single = here.
if (buttonState1 == HIGH && buttonState2 == HIGH){
The compiler KNOWS what HIGH, ==, &&, etc. mean, and what the precedence of the operators is. What it knows is what matters, not what you think the operator does or the precedence is. This code is now correct (except that the state variables are not being valued).
const int button1(2);
const int button2(3);
const int m1(4);
const int m2(5);
const int m3(6);
const int data(7);
int buttonState1 = LOW;
int buttonState2 = LOW;
void setup()
{
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(m1, OUTPUT);
pinMode(m2, OUTPUT);
pinMode(m3, OUTPUT);
pinMode(data, OUTPUT);
digitalWrite(button1, LOW);
digitalWrite(button2, LOW);
digitalWrite(m1, LOW);
digitalWrite(m2, LOW);
digitalWrite(m3, LOW);
digitalWrite(data, LOW);
}
void loop()
{
buttonState1 = digitalRead(button1);
buttonState2 = digitalRead(button2);
if (buttonState1 == HIGH){
digitalWrite(button1, HIGH);
digitalWrite(m1, HIGH);
delay(2000);
digitalWrite(m2, HIGH);
delay(5000);
digitalWrite(m3, HIGH);
delay(8000);
}
if (buttonState1 == HIGH && buttonState2 == HIGH){
digitalWrite(data, HIGH);
delay(5000);
digitalWrite(button1, LOW);
digitalWrite(button2, LOW);
digitalWrite(m1, LOW);
digitalWrite(m2, LOW);
digitalWrite(m3, LOW);
digitalWrite(data, LOW);
}
}
Is that right?
So, what I want to happen (just to recap) is just for everything to go ON, when Button1 has been pressed, once.
After that, data goes on if Button2 is then pressed. After five seconds, everything goes off.
Erh. I've fixed it up a bit. But, everything (bar data) comes on WITHOUT an input on Button1. I bet I seem kinda hopeless at this point.
Absolutely not!
You're sticking with it, and that counts for a lot around here.
Just a few things - you set "button1" as an input, but you're both reading and writing to it.
A write to an input pin enables or disables the pullup resistors, which is probably not what you want.
Can you explain or show a diagram of how things are wired?
Oh, I can explain very briefly! I have a wire going from a 5Volt rail (on a breadboard) to Pin 2 (button1) on the Arduino Uno I'm using. It's going to be a button, but as it is, it's just a test bed and I actually don't know what equipment will be going onto it, aside from buttons. It's just going to control a few automatic measurement devices on a fixture. The Arduino is being used as a replacement for relays and other bulky things that a work friend doesn't want to have to put into the fixture. So he asked me to do this (It's not urgent, or work, really. Just to save him hassle).
So, yes, it's literally just a wire. So, what I want to happen is for the button to remain high after it's been pushed. And then when button2 is pushed, button1 will still be high, and they'll set data high, and then everything goes low. I hope that makes sense!
You should learn about Serial.begin(), Serial.print(), and Serial.println(). They make it possible to debug your program.
So, you connect the wire to the pin to make the connection, and unplug the wire to break it. Right? When nothing is connected to the pin, the pin is floating. Search for floating pin to understand what that means, and how to deal with the problem.
Now it's my turn to be confused.
I don't understand why you're turning the pullups on and off.
I would normally expect them to be enabled just once, and a closed switch condition would be signalled by connecting the pin to ground, not 5V.
If the pullup is disabled (digitalWrite (button1, LOW)) then the pin is floating, assuming there is no external pull up or pulldown, and that is a Bad Thing tm
Can you explain why you're doing the writes to button1's pin ?
I'm writing the buttons LOW, because I need them to go low? I.. Don't know how to make button1 stay HIGH without writing it, and how to make it LOW without writing it. So. Basically.
I want both buttons to go LOW after bother buttons have been HIGH?
Would a variable make more sense in this situation?
Remember, I'm not amazing at this! Though, you might have noticed already. Sorry for the confusion, guys.
I'm writing the buttons LOW, because I need them to go low?
Not the switch pins. Those are inputs. They go HIGH or LOW when the switch (when there really is one) is pressed or released. You want to read those pins to see when that happens.
Would a variable make more sense in this situation?
I'm writing the buttons LOW, because I need them to go low?
But you have no program control over the buttons - your program cannot affect their state.
If a pin is tied to 5V, nothing you do in your program is going to change that.
Right, it does make sense now!
So how could I resolve that? Because the button will be a momentary button. It needs to be user friendly, and kinda fool proof. It's meant to be used as 'Press this. Wait a while. Press this.' And it's not able to be more complicated. I have no idea what I'd do now! Would I have to include more variables, and give them a number, which will change depending on the state of the button, and have them control the other things?
Would I have to include more variables, and give them a number, which will change depending on the state of the button, and have them control the other things?
Maybe, maybe not. The Arduino is quite logical. Write out exactly what YOU would do to monitor the inputs and control the outputs. Then, we can help you convert that to code.
const int button1(2);
const int button2(3);
const int m1(4);
const int m2(5);
const int m3(6);
const int data(7);
int buttonState1 = LOW;
int buttonState2 = LOW;
void setup()
{
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(m1, OUTPUT);
pinMode(m2, OUTPUT);
pinMode(m3, OUTPUT);
pinMode(data, OUTPUT);
digitalWrite(m1, LOW);
digitalWrite(m2, LOW);
digitalWrite(m3, LOW);
digitalWrite(data, LOW);
}
void loop()
{
buttonState1 = digitalRead(button1);
buttonState2 = digitalRead(button2);
if (buttonState1 == HIGH){
digitalWrite(m1, HIGH);
delay(2000);
digitalWrite(m2, HIGH);
delay(5000);
digitalWrite(m3, HIGH);
delay(8000);
}
if (buttonState2 == HIGH){
digitalWrite(data, HIGH);
delay(5000);
digitalWrite(m1, LOW);
digitalWrite(m2, LOW);
digitalWrite(m3, LOW);
digitalWrite(data, LOW);
}
}
I guess this is what's happening; I'm writing those pins high, when the button is pressed. Even if the button is released, it doesn't matter. There's no other arguments, so it has nothing else to do if the button goes low.
Thanks very much for helping me sort out, and tidy up the code, guys. Much appreciated!