Hello All,
I am working on a model railway signal control and have decent progress made. However i am wanting to display a diverging aspect (Blinking yellow) so i have the code worked out but i need it to fade on and off. If i start Arduino with the series of buttons pressed to represent the aspect it fades flawlessly. But when i go from a different aspect to the blinking aspect the led goes to 255 and fades to 0, i need it to go from 0 to 255 back to 0 when ever the series of buttons is pressed.
here is the excerpt of code:
if (B3buttonState == DEACTIVE && B2buttonState == DEACTIVE && B1buttonState == DEACTIVE && Diverg1buttonState == ACTIVE) { // Check Block 3
digitalWrite(GreenledPin, LOW);
analogWrite(YelledPin, brightness);
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
Paul_B
December 25, 2015, 10:35pm
2
And an excerpt is useful because?
Because this is the line of code I'm having a problem with.
PaulRB
December 26, 2015, 2:30pm
4
nkpchris2009:
Because this is the line of code I'm having a problem with.
If you are so certain you know what the problem is, why do you need our help?
Post the full sketch please.
Full code:
#define ACTIVE LOW
#define DEACTIVE HIGH
const int RedledPin = 8; // Block 1 Red LED
const int YelledPin = 11; // Block 1 Yellow LED
const int GreenledPin = 7; // Block 1 Green LED
int Block1button = 2; // Block 1 input
int Block2button = 4; // Block 2 input
int Block3button = 5; // Block 3 input
int Diverg1button = 12; // Switch on Diverging Button
int B1buttonState = 0; // variable for reading Block 1
int B2buttonState = 0; // variable for reading Block 2
int B3buttonState = 0; // variable for reading Block 3
int Diverg1buttonState = 0; // variable for reading Switch 1
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
void setup() {
pinMode(RedledPin, OUTPUT); // declare Red LED as output
pinMode(YelledPin, OUTPUT); // declare Yellow LED as output
pinMode(GreenledPin, OUTPUT); // declare Green LED as output
pinMode(Block1button, INPUT); // declare Block 1 as input
pinMode(Block2button, INPUT); // declare Block 2 as input
pinMode(Block3button, INPUT); // declare Block 3 as input
pinMode(Diverg1button, INPUT); // declare Diverg button as input
digitalWrite(2,HIGH); // Enable Pull-Up Resistor on Pin 2
digitalWrite(4,HIGH); // Enable Pull-Up Resistor on Pin 4
digitalWrite(5,HIGH); // Enable Pull-Up Resistor on Pin 5
digitalWrite(12,HIGH); // Enable Pull-Up Resistor on Pin 8
}
void loop(){
B1buttonState = digitalRead(Block1button); // read input Block 1
B2buttonState = digitalRead(Block2button); // read input Block 2
B3buttonState = digitalRead(Block3button); // read input Block 3
Diverg1buttonState = digitalRead(Diverg1button); // read input on Diverg Button
brightness = brightness + fadeAmount;
if (B1buttonState == DEACTIVE && B2buttonState == DEACTIVE && B3buttonState == DEACTIVE && Diverg1buttonState == DEACTIVE) { // Check Blocks 1,2,3
digitalWrite(GreenledPin, HIGH); // turn GREEN LED ON
} else {
digitalWrite(GreenledPin, LOW); // turn GREEN LED OFF
}
if (B1buttonState == ACTIVE && B2buttonState == DEACTIVE && B3buttonState == DEACTIVE) { // Check Block 1
digitalWrite(RedledPin, HIGH); // turn LED ON
} else {
digitalWrite(RedledPin, LOW); // turn LED OFF
}
if (B2buttonState == ACTIVE && B1buttonState == DEACTIVE && B3buttonState == DEACTIVE) { // Check Block 2
digitalWrite(RedledPin, HIGH); // turn on Red LED
} else {
digitalWrite(RedledPin, LOW); // turn off Red LED
}
if (B1buttonState == ACTIVE && B2buttonState == ACTIVE && B3buttonState == DEACTIVE) { //Check Blocks 1 & 2
digitalWrite(RedledPin, HIGH); // turn on Red LED
} else {
digitalWrite(RedledPin, LOW); // turn Red LED OFF
}
if (B3buttonState == ACTIVE && B2buttonState == ACTIVE && B1buttonState == DEACTIVE) { // Check Blocks 2 & 3
digitalWrite(RedledPin, HIGH); // turn on Red LED
} else {
digitalWrite(RedledPin, LOW); // turn off Red LED
}
if (B3buttonState == ACTIVE && B2buttonState == DEACTIVE && B1buttonState == DEACTIVE) { // Check Block 3
digitalWrite(YelledPin, HIGH); // turn on Yellow LED
} else {
digitalWrite(YelledPin, LOW); // turn off Yellow LED
}
if (B1buttonState == ACTIVE && B2buttonState == DEACTIVE && B3buttonState == ACTIVE) { // Approach Block 1
digitalWrite(RedledPin, HIGH); // turn on Red LED
} else {
digitalWrite(RedledPin, LOW); // turn off Red LED
}
if (B1buttonState == ACTIVE && B2buttonState == ACTIVE && B3buttonState == ACTIVE) { // All 3 blocks Occ.
digitalWrite(RedledPin, HIGH); // turn on Red LED
} else {
digitalWrite(RedledPin, LOW); // turn off Red LED
}
if (B3buttonState == DEACTIVE && B2buttonState == DEACTIVE && B1buttonState == DEACTIVE && Diverg1buttonState == ACTIVE) { // Check Block 3
digitalWrite(GreenledPin, LOW);
analogWrite(YelledPin, brightness);
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
delay(35); // wait for 30 milliseconds to see the dimming effect
}
if (B3buttonState == DEACTIVE && B2buttonState == ACTIVE && B1buttonState == DEACTIVE && Diverg1buttonState == ACTIVE) { // Check Block 3
analogWrite(YelledPin, brightness);
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
delay(30); // wait for 30 milliseconds to see the dimming effect
}
if (B3buttonState == DEACTIVE && B2buttonState == ACTIVE && B1buttonState == ACTIVE && Diverg1buttonState == ACTIVE) { // Check Block 3
analogWrite(YelledPin, brightness);
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
delay(30); // wait for 30 milliseconds to see the dimming effect
}
}
PaulRB
December 26, 2015, 5:26pm
6
Try this:
#define ACTIVE LOW
#define DEACTIVE HIGH
const int RedledPin = 8; // Block 1 Red LED
const int YelledPin = 11; // Block 1 Yellow LED
const int GreenledPin = 7; // Block 1 Green LED
int Block1button = 2; // Block 1 input
int Block2button = 4; // Block 2 input
int Block3button = 5; // Block 3 input
int Diverg1button = 12; // Switch on Diverging Button
int B1buttonState = 0; // variable for reading Block 1
int B2buttonState = 0; // variable for reading Block 2
int B3buttonState = 0; // variable for reading Block 3
int Diverg1buttonState = 0; // variable for reading Switch 1
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
void setup() {
pinMode(RedledPin, OUTPUT); // declare Red LED as output
pinMode(YelledPin, OUTPUT); // declare Yellow LED as output
pinMode(GreenledPin, OUTPUT); // declare Green LED as output
pinMode(Block1button, INPUT); // declare Block 1 as input
pinMode(Block2button, INPUT); // declare Block 2 as input
pinMode(Block3button, INPUT); // declare Block 3 as input
pinMode(Diverg1button, INPUT); // declare Diverg button as input
digitalWrite(2, HIGH); // Enable Pull-Up Resistor on Pin 2
digitalWrite(4, HIGH); // Enable Pull-Up Resistor on Pin 4
digitalWrite(5, HIGH); // Enable Pull-Up Resistor on Pin 5
digitalWrite(12, HIGH); // Enable Pull-Up Resistor on Pin 8
}
void loop() {
B1buttonState = digitalRead(Block1button); // read input Block 1
B2buttonState = digitalRead(Block2button); // read input Block 2
B3buttonState = digitalRead(Block3button); // read input Block 3
Diverg1buttonState = digitalRead(Diverg1button); // read input on Diverg Button
if (B1buttonState == DEACTIVE && B2buttonState == DEACTIVE && B3buttonState == DEACTIVE && Diverg1buttonState == DEACTIVE) { // Check Blocks 1,2,3
digitalWrite(GreenledPin, HIGH); // turn GREEN LED ON
} else {
digitalWrite(GreenledPin, LOW); // turn GREEN LED OFF
}
if (B1buttonState == ACTIVE && B2buttonState == DEACTIVE && B3buttonState == DEACTIVE) { // Check Block 1
digitalWrite(RedledPin, HIGH); // turn LED ON
} else {
digitalWrite(RedledPin, LOW); // turn LED OFF
}
if (B2buttonState == ACTIVE && B1buttonState == DEACTIVE && B3buttonState == DEACTIVE) { // Check Block 2
digitalWrite(RedledPin, HIGH); // turn on Red LED
} else {
digitalWrite(RedledPin, LOW); // turn off Red LED
}
if (B1buttonState == ACTIVE && B2buttonState == ACTIVE && B3buttonState == DEACTIVE) { //Check Blocks 1 & 2
digitalWrite(RedledPin, HIGH); // turn on Red LED
} else {
digitalWrite(RedledPin, LOW); // turn Red LED OFF
}
if (B3buttonState == ACTIVE && B2buttonState == ACTIVE && B1buttonState == DEACTIVE) { // Check Blocks 2 & 3
digitalWrite(RedledPin, HIGH); // turn on Red LED
} else {
digitalWrite(RedledPin, LOW); // turn off Red LED
}
if (B3buttonState == ACTIVE && B2buttonState == DEACTIVE && B1buttonState == DEACTIVE) { // Check Block 3
digitalWrite(YelledPin, HIGH); // turn on Yellow LED
} else {
digitalWrite(YelledPin, LOW); // turn off Yellow LED
}
if (B1buttonState == ACTIVE && B2buttonState == DEACTIVE && B3buttonState == ACTIVE) { // Approach Block 1
digitalWrite(RedledPin, HIGH); // turn on Red LED
} else {
digitalWrite(RedledPin, LOW); // turn off Red LED
}
if (B1buttonState == ACTIVE && B2buttonState == ACTIVE && B3buttonState == ACTIVE) { // All 3 blocks Occ.
digitalWrite(RedledPin, HIGH); // turn on Red LED
} else {
digitalWrite(RedledPin, LOW); // turn off Red LED
}
if (B3buttonState == DEACTIVE && B2buttonState == DEACTIVE && B1buttonState == DEACTIVE && Diverg1buttonState == ACTIVE) { // Check Block 3
digitalWrite(GreenledPin, LOW);
brightness = brightness + fadeAmount;
analogWrite(YelledPin, brightness);
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
delay(35); // wait for 30 milliseconds to see the dimming effect
}
if (B3buttonState == DEACTIVE && B2buttonState == ACTIVE && B1buttonState == DEACTIVE && Diverg1buttonState == ACTIVE) { // Check Block 3
brightness = brightness + fadeAmount;
analogWrite(YelledPin, brightness);
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
delay(30); // wait for 30 milliseconds to see the dimming effect
}
if (B3buttonState == DEACTIVE && B2buttonState == ACTIVE && B1buttonState == ACTIVE && Diverg1buttonState == ACTIVE) { // Check Block 3
brightness = brightness + fadeAmount;
analogWrite(YelledPin, brightness);
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
delay(30); // wait for 30 milliseconds to see the dimming effect
}
}
Paul_B
December 26, 2015, 8:44pm
7
nkpchris2009:
Because this is the line of code I'm having a problem with.
Well, you learned something today, didn't you?