I wrote this program to run a turnout and 2 red green leds on a train layout using an attiny85
To me the code seems to be ok, but when when i wire it up onm a projedt boadr nothing works.
Also I will be using 2 ldr in parallel with each side of a spdt switch so the train can automaticle throw the turnout.
any suggestins
Paul f
#include <Servo8Bit.h>
Servo8Bit servo1;
const int servo1Pin = 0;
const int pos = 10;
const int pos_2 = 180;
const int led1gPin = 3 ;
const int led1rPin = 4;
const int switchPin1 = 2;
const int switchVal1;
const int switchPin2 = 1;
void setup() {
servo1.attach(0);
servo1.write(pos);
pinMode(led1gPin, OUTPUT);
pinMode(led1rPin, OUTPUT);
pinMode(1, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
}
void loop() {
int switchVal1;
switchVal1 = digitalRead(switchPin1);
int pos = 0;
int pos_2 = 180;
if (switchVal1 == LOW) {
digitalWrite(1, LOW);
digitalWrite(2, HIGH);
servo1.write(pos);
digitalWrite(led1gPin, HIGH);
digitalWrite(led1rPin, LOW);
}
int switchVal2;
switchVal2 = digitalRead(switchPin2);
if (switchVal2 == LOW) {
digitalWrite(2, LOW);
digitalWrite(1, HIGH);
servo1.write ( pos_2);
digitalWrite(led1rPin, HIGH);
digitalWrite(led1gPin, LOW);
}
}
You've got local and global variables with the same name - that can lead to confusion.
You gave the pins nice names and then forgot to use them consistently.
if the sw is thrown left then it will keep the turnout in that position unti the sw is thrown left. also if the loco covers a ldr, the ldr can change the turnout posotion
Sounds like if one side is closed (LOW) you want the switch that side but if the other side ALSO goes LOW you want to switch to the other side. That's going to work until the LDR gets uncovered before the train is all the way through the switch.
Can you get a three-way momentary switch (ON-OFF-ON) or two momentary buttons (L, R)? That way the switch can be thrown manually and automatically and stay in position until thrown again.