I am installing lights in a model train and there are multiple prototypical lighting procedures that need to be activated. The lights are connected as follows: Headlight Top - Pin 1 Headlight Bottom - Pin 2 Front Right Marker - Pin 3 Front Left Marker - Pin 4 Rear Right Marker - Pin 5 Rear Left Marker - Pin 6 Rear Left Headlight - Pin 7 Rear Right Headlight - Pin 8 Front Right Ditch Light - Pin 9 Front Left Ditch Light - Pin 10
I would like to simply address a forward procedure which would turn on pins 1, 2, 3, 4, 5, 6, 9, and 10. This is the lighting configuration when the loco is shifted into the forward gear. For reverse, pins 3, 4, 5, 6, 7, and 8 would be activated. There will be a few others too but those are the primary procedures. Is there a way I could set up a variable which names all those pins at once? Or is there some other way I could name a forwards or backwards lighting procedure with a single short name such as "Procedure 2"? I am using Arduino IDE.
2 Answers 2
perhaps an array and a loop? (dont have the Arduino IDE atm, so sorry about any typos)
int lights[] = {1, 2, 3, 4, 5, 9, 10};
int count = 7;//because there are 7 items in the array
//
main(){
for (int i = 0; i < count; i++){
digitalWrite(lights[i], HIGH);
delay(1000);
}
}
EDIT: still no IDE, but here is a little more depth.
forward(true) = forward lights on
forward(false) = forward lights off
reverse(true) = reverse lights on
reverse(false) = reverse lights off
void forward(bool state){
int lights[] = {1, 2, 3, 4, 5, 6, 9, 10};
int count = sizeof(lights) / sizeof(lights[0]);
for (int i = 0; i < count; i++){
switch (state){
case true: //turn on
digitalWrite(lights[i], HIGH);
break;
case false: //turn off
digitalWrite(lights[i], LOW);
break;
}
//delay(1000); //optional delay between lights?
}
}
void reverse(bool state){
int lights[] = {3, 4, 5, 6, 7, 8};
int count = sizeof(lights) / sizeof(lights[0]);
for (int i = 0; i < count; i++){
switch (state){
case true: //turn on
digitalWrite(lights[i], HIGH);
break;
case false: //turn off
digitalWrite(lights[i], LOW);
break;
}
//delay(1000); //optional delay between lights?
}
}
I would like to simply address a forward procedure which would turn on pins 1, 2, 3, 4, 5, 6, 9, and 10.
And what about pins 7 and 8? You didn't say what to do with them in the forward procedure. Should they be turned off? Should they stay in whatever state they were before? I know it is kind of obvious to you, but it doesn't hurt being specific, for the benefit of those lacking the required background. Lacking a complete specification, we are left to making assumptions.
kei, in his answer, assumed that any pin you did not mention should be left alone, in whatever state it was before. In this answer I will instead assume that you want to always control the 10 pins, turning each of them either on or off.
With this assumption, the obvious solution is to have, for each
procedure, an array of 10 "states" (either HIGH
or LOW
), and loop
through the array setting each pin to its appropriate state:
for (int i = 0; i < LED_COUNT; i++)
digitalWrite(led_pins[i], pattern[i]);
Here, pattern[i]
is the state associated to led_pin[i]
in this
specific procedure. Now, since all these pins are consecutive, you don't
need the led_pins
array, and you can replace led_pins[i]
by
i + FIRST_LED_PIN
(i.e. i+1
).
This way you can implement as many procedures as you want, each one being defined as a simple 10-number array. I would add a single function to activate any procedure. This function would take as an argument a pointer to the light pattern you want to apply:
const uint8_t FIRST_LED_PIN = 1;
const int LED_COUNT = 10;
const uint8_t formward_pattern[LED_COUNT] = {
HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, LOW, LOW, HIGH, HIGH
};
const uint8_t reverse_pattern[LED_COUNT] = {
LOW, LOW, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, LOW, LOW
};
void set_led_pattern(const uint8_t *pattern)
{
for (int i = 0; i < LED_COUNT; i++)
digitalWrite(i + FIRST_LED_PIN, pattern[i]);
}
With this in place, you can set the light pattern of the forward procedure by issuing the single statement:
set_led_pattern(formward_pattern);
and for the reverse procedure:
set_led_pattern(reverse_pattern);