Hello everybody,
I have to create this circuit by using variables and after trying so many codes, i haven't yet found a way to do it. Could somebody pls help find a way to do it?
Sorry for the quality (it had to be a GIF so...)
have a look at arduino-button-tutorial
The thing is I've tried that before I even got the push button to act as a toggle button. I just can't find a way for the first button to make every LED blink one after the other. And the other 2 buttons too.
without seeing code it is difficult to advise!
upload your code (using code tags </>)
also a print of the serial monior output
// check multiple buttons and toggle LEDs
enum { Off = HIGH, On = LOW };
byte pinsLed [] = { 10, 11, 12 };
byte pinsBut [] = { A1, A2, A3 };
#define N_BUT sizeof(pinsBut)
byte butState [N_BUT];
// -----------------------------------------------------------------------------
int
chkButtons ()
{
for (unsigned n = 0; n < sizeof(pinsBut); n++) {
byte but = digitalRead (pinsBut [n]);
if (butState [n] != but) {
butState [n] = but;
delay (10); // debounce
if (On == but)
return n;
}
}
return -1;
}
// -----------------------------------------------------------------------------
void
loop ()
{
switch (chkButtons ()) {
case 2:
digitalWrite (pinsLed [2], ! digitalRead (pinsLed [2]));
break;
case 1:
digitalWrite (pinsLed [1], ! digitalRead (pinsLed [1]));
break;
case 0:
digitalWrite (pinsLed [0], ! digitalRead (pinsLed [0]));
break;
}
}
// -----------------------------------------------------------------------------
void
setup ()
{
Serial.begin (9600);
for (unsigned n = 0; n < sizeof(pinsBut); n++) {
pinMode (pinsBut [n], INPUT_PULLUP);
butState [n] = digitalRead (pinsBut [n]);
}
for (unsigned n = 0; n < sizeof(pinsLed); n++) {
digitalWrite (pinsLed [n], Off);
pinMode (pinsLed [n], OUTPUT);
}
}
This is the code i used (when i press the button it turns the first led on, and when i press it again it turns it off). I just have to find a way to make it so when i press it:
It turns on and off the leds one after the other (like the GIF above).
//C++ code
int LEDstate = 0;
int OldButton;
int NewButton = 1;
void setup()
{
pinMode(7,OUTPUT);
pinMode(6,OUTPUT);
pinMode(5,OUTPUT);
pinMode(4,OUTPUT);
pinMode(13,INPUT_PULLUP);
pinMode(12,INPUT_PULLUP);
pinMode(11,INPUT_PULLUP);
}
void loop()
{
NewButton=digitalRead(13);
if(OldButton==0 && NewButton==1)
{
if(LEDstate==0){
digitalWrite(7,1);
LEDstate=1;
}
else
{
digitalWrite(7,0);
LEDstate=0;
}
}
OldButton=NewButton;
}
try a web search for blink sequence of leds arduino
nothing, still can't find a way to make them work like the GIF above.
Start with only one pattern and then work up the code to add more. Don't try and do everything at once.
When is this school assignment due?
Have they taught you about arrays?
You do not "try" code you write them. There is far too much of this attitude is that all you need to do is to look for a code that does what you want. Without bothering to learn to read what a code does.
This is why your teacher set you these patterns.
For an idea of what you need to do, for the first pattern perform a web search for
Larson Scanner
That's what I did, I tried to do everything I could to make it work. But it just doesn't seem to work.
Yeah, I know that, I'm sorry I didn't express myself clearly, by "trying" I meant I used everything I was thought to try and make it work but it doesn't. I can't just go on the internet and copy a complex code and call it a day, since it would've been too obvious and not very correct of my part.
No one is suggesting that you do that. It isn't even complex code anyway. But if you look how one pattern is created you can see how to create others.
So you should post the code where you tried to get the first pattern to work and we can show you where you went wrong.
Post the code correctly. Read this because it tells you how, along with other things you need to know before using this forum.
How to get the best from this from this Forum
I would use a sub void for each button and write the code for each button in its own sub void
"void loop()" main program, create conditional call for sub loop, one for each button
"void Subrta()" sub void a, need one for each button
in the sub loop you can write step by step code for the LED's
I am a beginner program writer but this seems an easy solution to your program
There is nothing called a sub void, you are making it up.
Void in front of a function name just means that function returns nothing.
I think you mean have a separate function for each pattern. However that is not the way to solve this problem.
They are actually called subroutines in programming. Its a piece of the code which only runs when called for from the main program code.
not sure it's that simple. could write explicit logic with delays for each pattern, but a better approach is non-blocking and can generically handle various patterns
consider this approach
const byte LedPins [] = { 13, 12, 11, 10 };
#define Nleds sizeof(LedPins)
byte pat0 [] = { 1, 6, 0xe, 8, 4, 2, 1, 0 };
unsigned idx;
unsigned long msecPeriod = 250;
unsigned long msecLst;
unsigned long msec;
// -----------------------------------------------------------------------------
void
setLeds (
byte pattern )
{
Serial.print ("setLeds: ");
Serial.println (pattern);
for (unsigned n = 0; n < Nleds; n++) {
digitalWrite (LedPins [n], pattern & 1);
pattern = pattern >> 1;
Serial.println (pattern);
}
}
// ---------------------------------------------------------
void
ledLoop (
byte pattern [],
unsigned size )
{
if (msec - msecLst > msecPeriod) {
msecLst = msec;
setLeds (pattern [idx++]);
if (size <= idx)
idx = 0;
}
}
// ---------------------------------------------------------
void
loop (void)
{
msec = millis ();
ledLoop (pat0, sizeof(pat0));
}
// ---------------------------------------------------------
void
setup (void)
{
Serial.begin (9600);
for (unsigned n = 0; n < Nleds; n++)
pinMode (LedPins [n], OUTPUT);
}
Nope. They are called functions. Unless they are part of a class, in which case they are called methods.
Functions and methods can be called from many places within a program. They can be called from other functions. They can even be called from within themselves, this is called recursion.
It's good that you are trying to provide advice, but it's bad that you feel the need to correct other users. Sub void is nonsense, and subroutine is an archaic term that does not apply to C++.
Interesting. I googled and found this, wondering what the diff is between a function and a subroutine. You know no one says anything wrong on the internet, so I'm going with this:
Most modern languages don't even have the concept of a subroutine anymore, at least not with the limitations that originally distinguished it from a function.
A function returns a value, a subroutine does not. Is one thing ppl say.
In C/C++ there are both functions. Not "voids" or "void subs" or subroutines.
a7
Hi,
Welcome to the forum.
Have you looked at the switch.. case function?
Tom..
Hello averagestudent
This task can be solved very well with OOP.
Do you already have experience with programming in OOP?
No classes are necessary, just a few objects for the buttons and the LED show. An event-driven timer is used to run the sequence of the LED shows.
It is best to try to abstract the task. For example, I am thinking of a ringmaster who makes penguins run left or right or jump.
INPUT=ringmaster --> Read keys
PROCESSING: Start of selected LED show
OUTPUT: Run selected LED show
p.s. I like the gif presentation
simple program I implemented some years back to test a PCB with two switches and two LEDs
when switches pressed get different combinations of LEDs flashing
// display LED sequence polled version
// when switches is pressed change pattern for one sequence
// note: switches are low when pressed
int switch1 = 10; // SWITCH1
int switch2 = 12; // SWITCH2
int ledPin1 = 5; // SMALL YELLOW LED
int ledPin2 = 6; // SMALL YELLOW LED
boolean switchState = false;
void setup() {
Serial.begin(115200);
pinMode(switch1, INPUT);
pinMode(switch2, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
}
// LED test pattern sequences repeat - assume would be different in a real application
byte leds1[][2] = {{HIGH, HIGH}, {LOW, LOW}, {HIGH, HIGH}, {LOW, LOW}, {HIGH, HIGH}, {LOW, LOW}};
byte leds2[][2] = {{HIGH, LOW}, {LOW, HIGH}, {HIGH, LOW}, {LOW, HIGH}, {HIGH, LOW}, {LOW, HIGH}};
byte leds3[][2] = {{HIGH, LOW}, {LOW, LOW}, {HIGH, LOW}, {LOW,LOW}, {HIGH, LOW}, {LOW, LOW}};
byte leds4[][2] = {{LOW, HIGH}, {LOW, LOW}, {LOW, HIGH}, {LOW,LOW}, {LOW, HIGH}, {LOW, LOW}};
unsigned long int timer = 0;
void loop() {
Serial.print("Swich state SW1 ");
Serial.print(!digitalRead(switch1));
Serial.print(" SW2 ");
Serial.println(!digitalRead(switch2));
//Serial.println(sizeof(leds1));
// depending on switch pressed blink LED patterns
for (int i = 0; i < sizeof(leds1) / 2; i++)
{
// if switch1 = 1 and switch 2=1
if ((!digitalRead(switch1)) && (!digitalRead(switch2)))
{
digitalWrite(ledPin1, leds4[i][0]);
digitalWrite(ledPin2, leds4[i][1]);
}
else
if (!digitalRead(switch1)) // if switch1 = 1
{
digitalWrite(ledPin1, leds1[i][0]);
digitalWrite(ledPin2, leds1[i][1]);
}
else
if (!digitalRead(switch2)) // if switch2 = 1
{
digitalWrite(ledPin1, leds2[i][0]);
digitalWrite(ledPin2, leds2[i][1]);
}
else // if switch1 and switch2 = 0
{
digitalWrite(ledPin1, leds3[i][0]);
digitalWrite(ledPin2, leds3[i][1]);
}
timer = millis(); // delay to blink leds
while ((millis() - timer) < 200);
}
}