What did I do wrong in writing this code

Greetings all

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 forgot the code tags... (on separate lines)

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.

so this is your code somewhat cleaned up to take into account what @anon73444976 said

#include <Servo8Bit.h>
Servo8Bit servo;
const int pos_1 = 10;
const int pos_2 = 180;

const byte servoPin    = 0;
const byte switchPin2  = 1;
const byte switchPin1  = 2;
const byte led1gPin    = 3;
const byte led1rPin    = 4;

void setup() {
  servo.attach(servoPin);
  servo.write(pos_1);
  pinMode(switchPin1, INPUT_PULLUP);
  pinMode(switchPin2, INPUT_PULLUP);
  pinMode(led1gPin, OUTPUT);
  pinMode(led1rPin, OUTPUT);
}

void loop() {
  if (digitalRead(switchPin1) == LOW) {
    digitalWrite(switchPin2, LOW);   // WHY WOULD YOU DO THAT ??
    digitalWrite(switchPin1, HIGH);  // WHY WOULD YOU DO THAT ??
    digitalWrite(led1gPin, HIGH);
    digitalWrite(led1rPin, LOW);
    servo.write(pos_1);
  }

  if (digitalRead(switchPin2) == LOW) {
    digitalWrite(switchPin1, LOW);   // WHY WOULD YOU DO THAT ??
    digitalWrite(switchPin2, HIGH);  // WHY WOULD YOU DO THAT ??
    digitalWrite(led1rPin, HIGH);
    digitalWrite(led1gPin, LOW);
    servo.write (pos_2);
  }
}

why are you messing up (digitalWrite ) with switchPin1 and switchPin2??

it is line 2 and three below the void loop

what are you saying?

sorry read the reply wrong. not sure i can try to take it out and see what happens. let u know

this requires some thinking:

My thought is this I am using a spdt momentary sw. also I want the ldrs to be able to trigger in inputs

I don't understand what you mean. if the switch is an INPUT_PULLUP, why would you try to digitalWrite() the input pin connected to the switch?

can you post a drawing of your circuit with details about your components / power?

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

where is the ldr in the code?
still not an answer to the question
please draw the circuit and post a picture

will draw one up and post it will take a while

is there a way to read the cone on a programed attiny85.. have one that works perfectly

it is diner time and grandkids are coming over so will do the diagram in the morning

Bon appétit :wink:
It’s midnight heee

No. Once the sketch is compiled into binary there is no way back. It's like trying to turn scrambled egg back into an egg.

Yes, you could copy the code from one attiny85 to another.

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.

#include <Servo8Bit.h>
Servo8Bit Switch1Servo;

const int Switch1LeftPosition = 10;
const int Switch1RightPosition = 180;

const byte Switch1ServoPin    = 0;
const byte Switch1LeftPin     = 1;
const byte Switch1RightPin    = 2;
const byte Switch1GreenLEDPin = 3;
const byte Switch1RedLEDPin   = 4;

void setup()
{
  Switch1Servo.attach(Switch1ServoPin);
  Switch1Servo.write(Switch1LeftPosition);
  pinMode(Switch1RightPin, INPUT_PULLUP);
  pinMode(Switch1LeftPin, INPUT_PULLUP);
  pinMode(Switch1GreenLEDPin, OUTPUT);
  pinMode(Switch1RedLEDPin, OUTPUT);
}

void loop()
{
  static boolean lastLeftState = false;
  static boolean lastRightState = false;

  boolean leftState = digitalRead(Switch1LeftPin) == LOW;
  boolean rightState = digitalRead(Switch1RightPin) == LOW;

  if (leftState != lastLeftState)
  {
    // State Change Detected
    lastLeftState = leftState;

    if (leftState)
    {
      digitalWrite(Switch1GreenLEDPin, HIGH);
      digitalWrite(Switch1RedLEDPin, LOW);
      Switch1Servo.write(Switch1LeftPosition);
    }
  }

  if (rightState != lastRightState)
  {
    // State Change Detected
    lastRightState = rightState;

    if (rightState)
    {
      digitalWrite(Switch1GreenLEDPin, LOW);
      digitalWrite(Switch1RedLEDPin, HIGH);
      Switch1Servo.write(Switch1RightPosition);
    }
  }
}

thanks. i will try it. sounds like just what i was trying to do.
It is difficult when one does not fully understand how to write code

Even if you fully understand how to write code, you also need to fully understand what you want the sketch to do.

It all gets easier with practice.