Noob questions

I have a number of Arduino books and have done lots of reading and have tried to write a few sketches but the more I do the more confused I get. I have a rotary switch with 4 positions that I am going use to control the inputs into my Arduino Uno Rev3. I want to monitor the 4 inputs and trigger 4 output pins for each input. I am using some Sparkfun power driver shields to drive some multi LED lamps. I want these to light for 2 seconds and go off for 2 seconds and repeat, changing which lamps light according to what input they receive. Best I can tell is that I want to use "blink without delay" and possibly "switch case". I am looking for some guidance on how to structure my sketch. Below is an example of what I am trying to do:

const int LEFT = 5
const int LEFT = 6
const int LEFT = 9
const int LEFT = 10
const int SWITCHL = 4

void setup () {
pinMode (LEFT, OUTPUT)
pinMode (SWITCHL, INPUT)

The three other switch positions would be similar, just different input and output pins.

How to use this forum

Code tags please.

C statements end with a semicolon, eg.

const int LEFT = 5;

const int LEFT = 5
const int LEFT = 6
const int LEFT = 9
const int LEFT = 10

I'm not sure what you are trying to do here. What value is LEFT?

I want to monitor the 4 inputs and trigger 4 output pins for each input.

Do you have 16 output pins available ?

@ Nick, sorry about the semicolon slip. It was late and I was tired. As for LEFT that is what I am assigning that output instead of say LED. My other outputs would be RIGHT, DOUBLE, and CAUTION and use a combination of pins as their outputs. My other inputs would be labeled SWITCHR, SWITCHD, and SWITCHC.

@michinyon I want the FETs on my two power driver boards to trigger at the same time. That gives me 4 input pins and 6 output pins with several free pins on my UNO.

I .... have tried to write a few sketches but the more I do the more confused I get.

Ok I think maybe it's worth a bit of a back up KSf.

What sketches have you tried, and (more to the point) what happened that shouldn't have or didn't happen that should have? Have you resolved those problems? Did you get the blink and switch case sketches under you belt successfully?

And lastly, what specifically has confused you?- perhaps someone here can help clarify a stumbling block which is holding you up?

I'm not sure about what you meant by LEFT.
You're creating more than one variable with the same name (LEFT). Try using different names.

Here is where I am at. I decided to make a sketch for one section of my code. The "LEFT" part using the "Blinking LED" sketch. I figured if I can make this work then the rest should be fairly straightforward. When I read that "delay" pauses everything, I realized that this won't work because I need to monitor my inputs for changes and decided to try the Blink Without Delay sketch and change things to my designations. Here is what I came up with:

const int LEFT = 5; // the number of the LED pin
const int LEFT = 6;
const int LEFT = 9;
const int LEFT = 10;
const int SWITCH = 4;

int ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated

long interval = 2000; // interval at which to blink (milliseconds)

void setup() {
// set the digital pin as output:
pinMode(LEFT, OUTPUT);
}

void loop()
{
unsigned long currentMillis = millis();

if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;

// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;

// set the LED with the ledState of the variable:
digitalWrite(LEFT, ledState);
}
}

When I "verify" it shows problems with using LEFT multiple times. From what I have read I understand that I want the Uno, whenever it see's LEFT, to know that this is an output and know that the output will be pins 5,6,9,10. I am assuming that because "verify" shows that this is a problem, that I can't use LEFT to define all 4 output pins. If that is so, how do I need to change this so that when I have an input on pin 4, I output to pins 5,6,9,10?

OK. Based on the questions you guys were asking me about "LEFT", I modified it and now "verify" is happy with the result. I will post the new version below. Can someone confirm for me if this will infact output to pins 5,6,9,10 when it receives an input on pin 4 and that the outputs will blink on and off @ 2 second intervals?

const int LEFT813 = 5; // the number of the LED pin
const int LEFT9 = 6;
const int LEFT4 = 9;
const int LEFT513 = 10;
const int SWITCHL = 4;

int ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated

long interval = 2000; // interval at which to blink (milliseconds)

void setup() {
// set the digital pin as output:
pinMode(LEFT813, OUTPUT);
pinMode (LEFT9, OUTPUT);
pinMode (LEFT4, OUTPUT);
pinMode (LEFT513, OUTPUT);
pinMode (SWITCHL, INPUT);
}

void loop()
{
unsigned long currentMillis = millis();

if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;

// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;

// set the LED with the ledState of the variable:
digitalWrite(LEFT813, ledState);
digitalWrite(LEFT9, ledState);
digitalWrite(LEFT4, ledState);
digitalWrite(LEFT513, ledState);
}
}

Hello,

I suggest you use millis as such, since i myself just learned it this way, so as to prevent the stack from overflowing and have a more performant timer.

if (millis() - previousMillis >= interval)
{
    previousmillis += interval
// . . . Your code goes here

}

Remember that after each IF statement you need to use "{ }".

For i to understand this i state it as such :

if (x, condition met) 
{ Execute what is within curly brackets }

same goes for ELSE statement.

As for your case of using LEFT variable name, your arduino will not know which is which, as you did define it now.

I suggest you use different name for each output pin, e.g

const int LEFT0 = 5;
const int LEFT1 = 6;
const int LEFT2 = 9;
const int LEFT3 = 10;

and if you get along with this you will have to define each single variable as output and trigger each one with its own delay.

Else, if you want arduino to know only one variable with different pins, then you will have to go for ARRAY [ ] . .

Hope this help . .

taz . .

when it receives an input on pin 4

But you don't ever read pin 4, so it never receives an input on that pin

you will need to have something as such wiithin your code :

pinMode (SWITCHL,INPUT);

and the use of

digitalRead(SWITCHL);

test the status of SWITCHL and execute what you need it to do, again IF statement will come into action.

You may want to use the debounce method to avoid incorrect reading of data.

.........

taz ..

I made the suggested changes and loaded the sketch and used some LEDs on my breadboard and have some success. The LEDs do blink in 2 second intervals but they blink without input on pin 4. How should I remedy this? I believe I am getting close. Once this piece of code it working properly I can repeat it for the other 3 sections and then I will just have to possibly use switchcase? to allow me to monitor the 4 inputs for change of state. Here is the revised code, what am I missing?

const int LEFT813 = 5; // the number of the LED pin
const int LEFT9 = 6;
const int LEFT4 = 9;
const int LEFT513 = 10;
const int SWITCHL = 4;

int val= 0;
int ledState = 0; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated

long interval = 2000; // interval at which to blink (milliseconds)

void setup() {
// set the digital pin as output:
pinMode(LEFT813, OUTPUT);
pinMode (LEFT9, OUTPUT);
pinMode (LEFT4, OUTPUT);
pinMode (LEFT513, OUTPUT);
pinMode(SWITCHL, INPUT);
}

void loop()
{
digitalRead(SWITCHL);

unsigned long currentMillis = millis();

if (millis() - previousMillis >= interval)
{
previousMillis += interval;

// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;

// set the LED with the ledState of the variable:
digitalWrite(LEFT813, ledState);
digitalWrite(LEFT9, ledState);
digitalWrite(LEFT4, ledState);
digitalWrite(LEFT513, ledState);
}
}

Well, you aren't doing anything with that read. As taz pointed out, you need to have an if to check if that switch is set, and if it is, perform your block of led code. As it stands, the led code runs all the time regardless of the switch

If you haven't already done so, I'd recommend working through the digital read serial tutorial to see how to wire a switch and read its state. On the subject of wiring, you haven't shown us how it's wired....

Jim suggestion is right . . how is your switch wired, is it pull up or pull down.

Based on these facts, you will test the status with either low or high, depending on your wiring, something like:

ButtonState = digitalRead(SWITCHL); 

if (ButtonState == low) 
{
  // execute your needs here
}

additional link supplied by jim, i request you to go through a basic tutorial, which i myself start with when i first had my arduino, read it, its so fun and make things so clear and comprehensive, and im pretty sure you will like it.

Here you go : Arduino Tutorial - Lesson 1 - Let there be blink!

..............

taz . .

I know the IDE has a "copy for forum" function. Don't use it please. Just copy and paste the raw code inside code tags. A graphic in this post: How to use this forum

Your use of data names is totally confusing:

const int LEFT813 = 5;      // the number of the LED pin
const int LEFT9 = 6;
const int LEFT4 = 9;
const int LEFT513 = 10;
const int SWITCHL = 4;

Why is LEFT9 pin 6? Better names might be GREEN_LED, BLUE_LED and so on.


digitalRead(SWITCHL);

Look at some of the examples that come with the IDE. This reads the switch but ignores the result. As taz3m suggests, you need to save the result in a variable and compare to LOW or HIGH (not low or high).

The numbers I assigned after LEFT correspond to the pin numbers on a cable that the powershields will go to. This way when I look at the sketch I can see which Arduino pin goes to which cable pin and ultimately which LED lamp it will control. Confusing to you guys, perfect sense to me. Thanks for your help thus far. I believe I have lots more reading to do until I can understand how to properly structure my sketches.