Help with random blink, etc

OK, so I'm REALLY new to arduino and could use a little beginner help.

What I want to do is have an on/off switch that will blink a handful of LEDs as long as it's in the ON position but if in the off position the LEDs would default to other switches. Some of those LEDs will blink at a given rate and some will just be controlled as ON/OFF with their respective switches.

So, in my primitive logic I see it as follows.

If SW1=I then LED1 random flash. If SW1=I then LED2 random flash. If SW1=I then LED3 random flash. If SW1=I then LED4 random flash. If SW1=I then LED5 random flash. If SW1=I then LED6 random flash. If SW1=I then LED7 random flash. If SW1=I then LED8 random flash. If SW1=I then LED9 random flash.

Else

If SW2=I then LED1 flash 500,500 Else LED2=O If SW3=I then LED3 flash 500,500 Else LED3=O If SW4=I then LED4 flash 500,500 Else LED4=O If SW5=I then LED5 flash 500,500 Else LED5=O If SW6=I then LED6=I Else LED6=O If SW7=I then LED7=I Else LED7=O If SW8=I then LED8 flash 500,500 Else LED8=O If SW9=I then LED9=I Else LED9=O

How would I code this? Any help is appreciated.

If you're a total beginner, have you looked at this tutorial page?

In particular, look at the one on how to read a switch and then how to blink an led without using delay. That should get you off to a good start.

You would almost certainly benefit from using arrays to house all those pesky pin numbers, but that might be Mark II.

Thanks. I have read a lot of the tutorials and sample codes but I was hoping to shave some edge off the learning curve. Mostly, it's the random blinking I'm worried about combined with how to code to default to the on/off switches if the master switch is off.

I guess what I'm hoping for is an angel.

Last night I put together the code I thought would work and ran the analyzer. It had so many errors that I have no idea how to do what I want to do.

Any help, even if it's pointing me to similar code, would be appreciated.

I suggest you start by looking at the standard examples which show how to read digital inputs. You will need that to determine which mode your sketch is supposed to be in.

PeterH: I suggest you start by looking at the standard examples which show how to read digital inputs. You will need that to determine which mode your sketch is supposed to be in.

As I have done. For some reason, I am not getting something in my brain correctly.

I typically learn things by seeing a complete example and then it makes sense. If anyone could give me an example of three inputs that are combined elements as well as their output result, it would be very helpful.

I think what you will notice is that people are reluctant to write code for you, it is more instructive if you find out by writing your own code. I suggest you start with one light turning one and off with one switch, add a delay to cause it to flash and then keep adding switches and lights as you build up your understanding. Use the examples in the arduino library such as blink to get you started.

matelot: I think what you will notice is that people are reluctant to write code for you, it is more instructive if you find out by writing your own code. I suggest you start with one light turning one and off with one switch, add a delay to cause it to flash and then keep adding switches and lights as you build up your understanding. Use the examples in the arduino library such as blink to get you started.

Yes, I've noticed that. Not at all what I'm used to in a help forum but I suppose it is what it is.

Guess I'll just take/waste time and wait til my boards arrive.

Bittsen:
I typically learn things by seeing a complete example and then it makes sense.

This is why I steered you towards an example that showed how to read an input and take actions based on the status.

If you think that what you need to solve your problem is a complete working solution to your exact problem then I think you’re either wrong, or have a very difficult/short programming future ahead of you. Divide the problem down into simple steps that you can solve separately, and then gradually work up towards the whole solution.

By the way, if you are certain that you want somebody to provide you with a complete working solution then the Gigs and Collaborations section is the place to ask for that. Note that most people would expect to be paid for working for you.

If you decide you’re going to implement it yourself then there are plenty of people here, me included, who will try to support you while you do that. We won’t do it for you, though.

I hear what you are saying and wasn't aware there was a gigs section. It's apparent that if I want code hand delivered then I have to pay for it. But I wasn't really asking for that even though it might seem like it.

I'm actually asking someone to hand feed me the necessary steps for making something work.

Think of it like this... Someone knows nothing about auto repair but wants to learn the basics. So this person asks, how do I change my spark plugs. Well, the easy way is to tell them how to change spark plugs. Or you could tell them to take an automotive class. Or direct them to a mechanic, etc.

Anyhow...

This is what I think I know.

You can't do this!

void setup() { pinMode(13, OUTPUT); pinMode(14, OUTPUT); pinMode(15, OUTPUT); pinMode(16, OUTPUT); pinMode(17, OUTPUT); pinMode(18, OUTPUT); pinMode(19, OUTPUT); pinMode(20, OUTPUT); pinMode(21, OUTPUT);

pinMode(2, INPUT); pinMode(3, INPUT); pinMode(4, INPUT); pinMode(5, INPUT); pinMode(6, INPUT); pinMode(7, INPUT); pinMode(8, INPUT); pinMode(9, INPUT); pinMode(10, INPUT); pinMode(11, INPUT); }

Or it won't work.

Now this is where I get to say "Why doesn't it work?"

And this is where I want help.

And, to top it off, I can't seem to see any of the rest of the arduino website with any of my browsers. They all say there is a redirect issue.

As for being a coder... I'm not going there. To use another analogy... I want to be able to make a nice dinner but have no desire to be a chef, nor am I resigned to paying someone to cook for me for the rest of my life.

P.S. I'm not an idiot, despite how I might appear. I have amassed a lot of knowledge and wish more. I'm one of those people who CAN rebuild an engine, cook, sew, build a house, perform surgery (OK, only the one time but the patient lived) and so much more. I've leaned a working knowledge of HTML, with the creation of thousands of websites (mostly coded in notepad), learned a lot of database management in MS Access, and so much more. Learning another language is a task but not terribly daunting. The problem is going to be forgetting some languages while using my working knowledge of others. And, unfortunately, C++ isn't something I will likely need in my professional career.

TMI? ~LOL~

You may as well say you want a cake but you don't want to use all the ingredients to make it properly ...

Your code in your last example works ... but it's not a good way, 2 for loops would save memory and your fingers....

Look up for loops, if , basic logic statements.... walk before you can run...

cjdelphi: You may as well say you want a cake but you don't want to use all the ingredients to make it properly ...

Your code in your last example works ... but it's not a good way, 2 for loops would save memory and your fingers....

Look up for loops, if , basic logic statements.... walk before you can run...

Nah, I know the ingredients, I just can't read the directions. They are in a completely different language.

Part of my learning curve issue is the color of these webpages. I'm having a helluva time reading them. I'm finding a bit better help in the YouTube tutorials than by trying to read the Arduino.cc webpages and make sense of them.

Correct or incorrect?

void loop() { digitalWrite(13, HIGH); delay(500); digitalWrite(13, LOW); delay(500); }

void loop() { digitalWrite(14, HIGH); delay(500); digitalWrite(14, LOW); delay(500); }

Well it's incorrect if you mean that to be in one sketch, becasue there can only be one loop().

And although it's correct it's not ideal to use delay() like that, since that stops the processor doing anything else for the duration. My post 24 hours ago suggested you look at blink without delay on the tutorial page.

JimboZA: Well it's incorrect if you mean that to be in one sketch, becasue there can only be one loop().

And although it's correct it's not ideal to use delay() like that, since that stops the processor doing anything else for the duration. My post 24 hours ago suggested you look at blink without delay on the tutorial page.

You answered my question. There's only one loop. As for the blink without delay function, that's what I'm trying to solve at the moment. I will take another look at what you posted before. I've seen a few youtube tutorials that focus on the blinking without the delay command but I'm taking baby steps at the moment.

I want a random blink but eventually I will have 30 LEDs blinking randomly as long as one button is pushed. Otherwise they will default to options specified by other inputs.

This is my first attempt at a full code for ONE input and ONE output. Yes, it should look familiar because I started with a sample sketch.

const int buttonPin2 = 2;    // pin that the pushbutton is attached to
const int ledPin13 = 13;       // pin that the LED is attached to
int buttonState = 0;

void setup() {
  
  pinMode(ledPin13, OUTPUT);
  pinMode(buttonPin2, INPUT);
  
  Serial.begin(9600);
}

void loop() {
  buttonState = digitalRead(buttonPin2);

  if (buttonState == HIGH) {
    digitalWrite(ledPin13, HIGH);
    delay(500);
    digitalWrite(ledPin13, LOW);
    delay(800);
  } 
  else {
    digitalWrite(ledPin13,LOW); 
  }
}

What I'd like to know is how do I make the delay random. I've looked all over the place and can't find the code snippet. I am not going to be happy if the processor stops running during a random delay as this totally defeats what I'm trying to accomplish as well as defeating the alternate purpose of this project.

Don’t rely on code snippets for everything.

Have a look at random() in the reference; it allows you to get a random number in a range you specify.

So using delay(), that could simply be the argument to delay: delay(random(min, max))

Or if you go for the better method without using delay(), you could use random() to define the threshold that you are looking to have expired.

Awesome reply! You have given me some useful tidbits to work with, especially knowing that two variables can come in the same string. It seems, like Excel, you have to make sure you have the same number of left parenthesis as right ones and you are good to go. I did not know that til now. I knew "random" was the correct "term" but didn't know how it was formatted.

I will get to the blink without delay function soon enough. Right now I'm dipping my pinky toe in the pool.

I wish my boards would get here already!

OK, next stupid question. And I hope I format my questions properly... Will it be possible to have multiple pins blinking at different rates simultaneously, independent of the others, based on a single stimuli (ie button press)?

Bittsen: OK, next stupid question. And I hope I format my questions properly... Will it be possible to have multiple pins blinking at different rates simultaneously, independent of the others, based on a single stimuli (ie button press)?

Sure, this is typically called Blink without Delay. The first place to look is here: http://arduino.cc/en/Tutorial/BlinkWithoutDelay.

However, a lot people don't really understand how to apply the technique used in Blink without Delay to multiple leds. Nick Gammon wrote this example that shows the basic idea in more detail: http://www.gammon.com.au/blink

MichaelMeissner: Sure, this is typically called Blink without Delay. The first place to look is here: http://arduino.cc/en/Tutorial/BlinkWithoutDelay.

However, a lot people don't really understand how to apply the technique used in Blink without Delay to multiple leds. Nick Gammon wrote this example that shows the basic idea in more detail: http://www.gammon.com.au/blink

OK, I'm reading those. They aren't clicking but they will. My brain is being shoved full of input the last couple days and after a long, tedious, physical day at work, all that I came up with is this...

const int BPin2 = 2;     // the number of the pushbutton pin
const int BPin3 = 3;
const int BPin4 = 4;
const int BPin5 = 5;
const int BPin6 = 6;
const int BPin7 = 7;
const int BPin8 = 8;
const int BPin9 = 9;
const int BPin10 = 10;
const int BPin11 = 11;

int buttonState = 0;

const int ledPin13 =  13;      // the number of the LED pin
const int ledPin14 =  14;
const int ledPin15 =  15;
const int ledPin16 =  16;
const int ledPin17 =  17;
const int ledPin18 =  18;
const int ledPin19 =  19;
const int ledPin20 =  20;
const int ledPin21 =  21;
const int ledPin22 =  22;



void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin13, OUTPUT);
  pinMode(ledPin14, OUTPUT);
  pinMode(ledPin15, OUTPUT);
  pinMode(ledPin16, OUTPUT);
  pinMode(ledPin17, OUTPUT);
  pinMode(ledPin18, OUTPUT);
  pinMode(ledPin19, OUTPUT);
  pinMode(ledPin20, OUTPUT);
  pinMode(ledPin21, OUTPUT);

  pinMode(BPin2, INPUT);
  pinMode(BPin3, INPUT);
  pinMode(BPin4, INPUT);
  pinMode(BPin5, INPUT);
  pinMode(BPin6, INPUT);
  pinMode(BPin7, INPUT);
  pinMode(BPin8, INPUT);
  pinMode(BPin9, INPUT);
  pinMode(BPin10, INPUT);
  pinMode(BPin11, INPUT);
}

void loop() {
  digitalWrite(ledPin13, HIGH);
  delay(500);
  digitalWrite(ledPin13, LOW);
  delay(500);

  digitalWrite(ledPin14, HIGH);
  delay(500);
  digitalWrite(ledPin14, LOW);
  delay(500);

  digitalWrite(ledPin15, HIGH);
  delay(500);
  digitalWrite(ledPin15, LOW);
  delay(500);

  digitalWrite(ledPin16, HIGH);
  delay(500);
  digitalWrite(ledPin16, LOW);
  delay(500);

  digitalWrite(ledPin17, HIGH);
  delay(500);
  digitalWrite(ledPin17, LOW);
  delay(500);

  digitalWrite(ledPin18, HIGH);
  delay(500);
  digitalWrite(ledPin18, LOW);
  delay(500);

  digitalWrite(ledPin19, HIGH);
  delay(500);
  digitalWrite(ledPin19, LOW);
  delay(500);

  digitalWrite(ledPin20, HIGH);
  delay(500);
  digitalWrite(ledPin20, LOW);
  delay(500);

  digitalWrite(ledPin21, HIGH);
  delay(500);
  digitalWrite(ledPin21, LOW);
  delay(500);
}

Which is a long way from where I started a couple days ago. I know it has a bunch of seemingly random pin assignments but I was playing to make sure I 'get it". And I know all it would do is turn on, and then off, in sequence, a series of LEDs at half second intervals. BUT, the point is it CLICKED! I am beginning to understand the logic. That's the first step in learning a new language.

You guys are helping a lot! Thanks!

I think you should move away from the delay() approach right now. Especially since you don't have the parts yet, you've got time to break that bad habit before you actually have it.