Can anyone tell me what I did wrong with this code the led won't lit=ght up when the button is pressed, I can't seem to figure it out?
int button = 2; // pin to connect the button
int presses = 0; // variable to store number of presses
long time = 0; // used for debounce
long debounce = 100; // how many ms to "debounce"
const byte numPins = 8; // how many leds
int state; // used for HIGH or LOW
// pins to connect leds
byte pins[] = {5, 6, 7, 8, 9, 10, 11, 12};
void count() {
// debouncing the button and increase the presses
if (millis() - time > debounce){
presses++;
time = millis();
}
}
void setup()
{
for (int i = 0; i <= numPins; i++) {
pinMode(pins[i], OUTPUT);
}
pinMode(button, INPUT);
//use pin 2 which has interrupt 0 on Arduino UNO
attachInterrupt(0, count, LOW);
}
void loop()
{
//convert presses to binary and store it as a string
String binNumber = String(presses, BIN);
//get the length of the string
int binLength = binNumber.length();
if (presses <= 255) {
for (int i = 0, x = 1; i < binLength; i++, x+= 2) {
if (binNumber[i] == '0') {state = LOW;}
if (binNumber[i] == '1') {state = HIGH;}
digitalWrite(pins[i] + binLength - x, state);
}
}
}
not sure why you're using an interrupt to read a button.
button switches are typically connected between the pin and ground, which means they need a pullup resistor. this can be done by configuring the pin as INPUT_PULLUP
all values on a processor are binary. no need to translate it to a String and then back to a binary value
why only when presses <= 255?
shouldn't your for loop be for each pin, numPins, not binLength?
what is x?
while there's no need for "binNumber[]", there's no need for separate ifs for each possible value of binNumber. could have been
if (binNumber [i] == '0') // is '0' a binary value 0, or an [ASCII char](https://en.wikipedia.org/wiki/ASCII#/media/File:USASCII_code_chart.png)
state = LOW;
else
state = HIGH;
simpler yet
state = binNumber [i] == '0'; // if ascii
simpler yet, see below
which pin value is "pins [i] + binLenght - x"? (see below)
digitalWrite(pins[i] + binLength - x, state);
consider
#undef MyHW
#ifdef MyHW
const int button = A1;
const byte pins[] = {10, 11, 12, 13};
#else
int button = 2; // pin to connect the button
byte pins[] = {5, 6, 7, 8, 9, 10, 11, 12};
#endif
const byte numPins = sizeof(pins);
byte presses = 0; // variable to store number of presses
byte butLst;
void setup()
{
Serial.begin (9600);
for (int i = 0; i < numPins; i++)
pinMode(pins[i], OUTPUT);
pinMode(button, INPUT_PULLUP);
}
void loop()
{
byte but = digitalRead (button);
if (butLst != but) {
butLst = but;
delay (10); // debounce
if (LOW == but)
presses++;
}
for (int i = 0; i < numPins; i++)
digitalWrite (pins[i], presses & 1<<i);
}
Hi
this sentence doesn't help us much.
"Can anyone tell me what I did wrong with this code, I can't seem to figure it out"
Why do you think you are doing something wrong with this code?
What should he do and what is he doing wrong.
Tell us, so we can help you better.
I have a distrust of this line.
"attachInterrupt(0, count, LOW);"
See, as long as the button is and, LOW interrupts will occur.
If you hold down the button, there will be many, many interrupts.
If this is what his code is doing wrong,
Try this line like this:
I apologize for not being able to answer all the questions since it was originally not mine and it was defective. I worked on it as much as I could to make it work but in the end, it didn't work.
I thought that was a good way to read the button
apologies, always forget about this with push button, hopefully I won't make same mistake twice
I thought I had to, that what my professor told me. thank you for letting me know
the reason for this because 8bit binary-only hold 255 or 256 values that is why limited to 255
I thought it could work since this code was orignally there
one from the original code, I think if I remember correctly the value that gave me was high in the serial that is why I added x to subtract it.
ok I'll work on it on my own code and start from scratch this time