I had code set up, with one button being pressed switching between two relays (as it will be controlling relays which makes a model train turn out switch)
I had a one button configuration working (push once the track switch goes to turn, press it again it goes to straight).
I have now written the code to have 4 buttons, as I am running 4 points, which means 8 outputs in total.
However, when I push button A0 it just seems to cycle all the outputs to relays. pushing the other buttons does nothing.
My four points are Mainline, Quarry, Dropoff and Shunt.
I dont know where the problem is occuring, as i am new to arduino programming, and have not done more than some breadboard testing with single button. You will see a stepper motor declared, but this isnt used in the code, but will be to control a turntable in the future.
I am using an Arduino Uno, buttons are anolog 0 thru to 3, and output is 0 thru to 7.
The idea was that button press calls code, and that way i can easily make adjustments in the void Mainline section opposed to making adjustments in the void loop (etc) as i might want to add indicator LEDs etc.
power on is momentary for signal to relays, as once switch is triggered it only needs momentary power then switched off to prevent burn out.
But as discussed, issue is pressing mainlinbutton seems to just cycle through all the outputs (can see them light up on relay board) if i remove the button, it just cycles through without stopping, until i plug the button back in.
button has 5v power going in, output goes between input, and resistor to ground.
#include <Stepper.h>
#define mainlinebutton A0
#define quarrybutton A1
#define dropoffbutton A2
#define shuntbutton A3
#define mainlinestraight 0
#define mainlinecurve 1
#define quarrypass 2
#define quarryin 3
#define dropoffpass 4
#define dropoffin 5
#define shuntpass 6
#define shuntin 7
const int stepsPerRevolution = 2038;
Stepper turntable(stepsPerRevolution, 8, 10, 9, 11);
int stepCount = 0;
void setup() {
pinMode(mainlinebutton, INPUT);
pinMode(mainlinestraight, OUTPUT);
pinMode(mainlinecurve, OUTPUT);
pinMode(quarrybutton, INPUT);
pinMode(quarrypass, OUTPUT);
pinMode(quarryin, OUTPUT);
pinMode(dropoffbutton, INPUT);
pinMode(dropoffpass, OUTPUT);
pinMode(dropoffin, OUTPUT);
pinMode(shuntbutton, INPUT);
pinMode(shuntpass, OUTPUT);
pinMode(shuntin, OUTPUT);
digitalWrite(mainlinestraight, LOW);
digitalWrite(mainlinecurve, LOW);
digitalWrite(quarrypass, LOW);
digitalWrite(quarryin, LOW);
digitalWrite(dropoffpass, LOW);
digitalWrite(dropoffin, LOW);
digitalWrite(shuntpass, LOW);
digitalWrite(shuntin, LOW);
}
enum MAINLINESWITCH
{
ML_OFF1,
ML_OFF2,
ML_STRAIGHT,
ML_CURVE,
};
enum QUARRYLINESWITCH
{
QL_OFF1,
QL_OFF2,
QL_STRAIGHT,
QL_CURVE,
};
enum DROPOFFLINESWITCH
{
DO_OFF1,
DO_OFF2,
DO_STRAIGHT,
DO_CURVE,
};
enum SHUNTLINESWITCH
{
SL_OFF1,
SL_OFF2,
SL_STRAIGHT,
SL_CURVE,
};
MAINLINESWITCH mainswitch=ML_OFF1;
QUARRYLINESWITCH quarryswitch=QL_OFF1;
DROPOFFLINESWITCH dropoffswitch=DO_OFF1;
SHUNTLINESWITCH shuntswitch=SL_OFF1;
void loop() {
if (analogRead(mainlinebutton==HIGH)) { // if button pressed then
switch(mainswitch) // process the button press
{
case ML_OFF1: // if current state is ML_OFF1 then set the switch to straight
mainlstraight(); //calls mainlstraight code
break;
case ML_OFF2: // if current state is ML_OFF2 then set the switch to turn
mainlswitch(); // calls mainlswitch code
break;
}
delay(200);
}
if (analogRead(quarrybutton==HIGH))
{
switch(quarryswitch)
{
case QL_OFF1:
quarrylstraight();
break;
case QL_OFF2:
quarrylswitch();
break;
}
delay(200);
}
if (analogRead(dropoffbutton==HIGH))
{
switch(dropoffswitch)
{
case DO_OFF1:
dropofflstraight();
break;
case DO_OFF2:
dropofflswitch();
break;
}
delay(200);
}
if (analogRead(shuntbutton==HIGH))
{
switch(shuntswitch)
{
case SL_OFF1:
shuntlstraight();
break;
case SL_OFF2:
shuntlswitch();
break;
}
delay(200);
}
}
void mainlswitch(){
digitalWrite(mainlinecurve, HIGH); //activates mainlinecurve relay
delay(200);
digitalWrite(mainlinecurve,LOW); //turns off mainlinecurve relay
mainswitch=ML_OFF1; //chages to ML_OFF1
}
void mainlstraight(){
digitalWrite(mainlinestraight, HIGH);
delay(200);
digitalWrite(mainlinestraight,LOW);
mainswitch=ML_OFF2;
}
void quarrylswitch(){
digitalWrite(quarryin, HIGH);
delay(200);
digitalWrite(quarryin,LOW);
quarryswitch=QL_OFF1;
}
void quarrylstraight(){
digitalWrite(quarrypass, HIGH);
delay(200);
digitalWrite(quarrypass,LOW);
quarryswitch=QL_OFF2;
}
void dropofflswitch(){
digitalWrite(dropoffpass, HIGH);
delay(200);
digitalWrite(dropoffpass,LOW);
dropoffswitch=DO_OFF1;
}
void dropofflstraight(){
digitalWrite(dropoffin, HIGH);
delay(200);
digitalWrite(dropoffin,LOW);
dropoffswitch=DO_OFF2;
}
void shuntlswitch(){
digitalWrite(shuntpass, HIGH);
delay(200);
digitalWrite(shuntpass,LOW);
shuntswitch=SL_OFF1;
}
void shuntlstraight(){
digitalWrite(shuntin, HIGH);
delay(200);
digitalWrite(shuntin,LOW);
shuntswitch=SL_OFF2;
}