Hey, I'm just playing around with a button at the moment. Trying to get Serial read, the problem (problems) I'm having is I don't understand what analog input is for and same with digital. For instance:
int _BUTTON = 0;
int _VAL= 0;
void setup()
{
Serial.begin(9600); // setup serial
}
void loop()
{
_VAL = analogRead(_BUTTON); // read the input pin
Serial.println(_VAL); // debug value
}
Would scroll values 1023 while unpressed, and between 0-20 while pressed, scrolls too fast to tell.
and:
int BUTTON = 2; // Test button A on controller
int VALUE = 1; // Value to store (ON/OFF)
int CURV = 0;
void setup()
{
Serial.begin(9600); // Watch
pinMode(BUTTON, INPUT);
}
void loop()
{
/**/
delay(10);
VALUE = digitalRead(BUTTON);
if (VALUE == HIGH)
Serial.write('H');
else
Serial.write('L');
delay(10);
}
Simply scrolls H while unpressed and L while pressed. So my question is, obviously what's the best way to write this code using either of the pins. I only need to read an ON/OFF signal. Additionally what's the difference between analog and digital. I would have thought that analog would only take simple 1's and 0's while digital could read a multitude of different signals.
You could add delay(1000); in your code to delay 1 second before taking the next measurement and print to serial.
Let's think we have a faucet, water flows out of the faucet. An analog person comes in, turns the faucet smoothly so the flow rate increase all the way from 0 to 1023 assuming every single value in between because the analog person has that smooth hand. The status of faucet has so many values, from 0, to 1023. Now a digital person comes in, he turns the faucet all the way on, or all the way off. There are only two statuses of the faucet, all the way on, or 1, and all the way off, or 0.
A button apparently belongs to a digital input since it either conducts current (just like water in the example) or it conducts no current so it should be connected to a digital pin. If you have a potentiometer that you can use to change current smoothly (thus voltage), then you need an analog input. Using a button on an analog input is like you can sense so many different stages but your button only provides two. Connecting a potentiometer to a digital input is like you can provide so many different stages but your can only sense on or off. Both cases are to be avoided unless you have some special need.
That seems backwards, I though generally in electronics we were steering away from analog and everything is going digital. (EG mobile phone signals, radio etc. etc.) Is that simply because of hardware technology is so great now that we can store more info? (more ones and zeros)
Also using a 4051 MUX IC, would I be better off getting a different MUX that just took digital, or just adapt my code accordingly.
analogRead() is intended to read things like varying voltage from a potentiometer, read in the 0-1023 and use the resulting value to change the frequency of a digital output for example, having PWM output frequency vary with the voltage coming in to change the brightness of an LED.
digitalRead() is for reading a 0/1 type input, say if a button was pressed or not.
My preference for that is to use the internal pullup resistors:
// put these lines in void setup()
pinMode(randomPin, INPUT);
digitalWrite(randomPin, HIGH); // enables the pullup (THERE ARE NO PULLDOWNS)
and then have the button ground the pin to show a change; or, have the pin switched to ground all the time, and open the switch to let it go High and sense that instead.
Arduino (Atmega) pins configured as INPUT with pinMode() are said to be in a high-impedance state. One way of explaining this is that pins configured as INPUT make extremely small demands on the circuit that they are sampling, say equivalent to a series resistor of 100 Megohms in front of the pin. This makes them useful for reading a sensor, but not powering an LED.
But can you write to a pin that's set as INPUT ?
What logic or psudeocode is required to register button presses. Say I have 10 different buttons setup, do I use some kind of wait() or buttonListener() command? The above code isn't practical obviously.
Yes, it is the mechanism to enable the built-in pullup resistor.
I thought defining it as INPUT was the mechanism to enable the built-in pullup.
So if you only read the pin defined as INPUT then the pullup doesn't enable?
PaulS:
What logic or psudeocode is required to register button presses.
Connect the switch (buttons are used to keep shirts closed) to a digital pin, and use digitalRead() to read the pin.
If you have 10 switches, put the pins (and states) in arrays, and use a for loop, inside loop(), to read them all.
I'll give it a go (I don't actually own 10 switches, it was a hypothetical XD):
/////////////////
// # of switches
const int sw_count = 10;
/////////////////
int switches[sw_count] ;
void setup()
{
for (int i=0; i<sw_count; ++i)
{
switches[i] = i;
pinMode(i, INPUT);
}
Serial.begin(9600);
}
void loop()
{
for (int i=0; i<sw_count; ++i)
{
if (digitalRead(i) == LOW)
{
Serial.println("Button: " + i + " was pressed");
}
}
}
// end
PS good thing you removed your post about button semantics, trying to redefine the English language is never a good idea on a nerd forum. You would have seen a lot of rages.
That seems backwards, I though generally in electronics we were steering away from analog and everything is going digital.
That's true, but an unfortunate fact of life is that the real world is mostly analogue. In general we try to convert to digital ASAP but if you are doing much work with physical computing you will have to deal with analogue quite a lot.