[SOVLED] Toggling a Lightswitch

Hello, I am fairly new to the Arduino platform and I need some help with figuring out how to toggle a switch with a servo. I have spent quite awhile trying to figure it out and I could not find resources elsewhere to help me with my problem.

The issue is the program only runs turnOff(), but never turnOn().

#include <Servo.h>
Servo servo;

void setup(){
  pinMode(13, INPUT);
  servo.attach(9);
}

void turnOn(){
  int lightSwitch = 1;
  servo.write(80);  //turns switch on
  delay(1000);
  servo.write(90);  //returns servo to neutral
}

void turnOff(){
  int lightSwitch = 0;
  servo.write(70);  //turns switch off
  delay(1000);
  servo.write(90);  //returns servo to neutral
}

void loop(){
  int buttonState, lightSwitch;
  buttonState = digitalRead(13);
  if ((buttonState == LOW) && (lightSwitch = 0)){
    turnOn();}
  if ((buttonState == LOW) && (lightSwitch = 1)){
    turnOff();}
  }

I apologize if my code is not very clean and I would love any tips on how to make it easier to follow as well.

void turnOff(){
  int lightSwitch = 0;

That’s a different “lightSwitch” to the one declared in “loop()” and is only in scope for “turnOff”.
Lose the “int”, and move the declaration to global scope.

Okay, so I did what you said to do and I read more about scope. I modified my code but I am still having the same problem.

#include <Servo.h>
Servo servo;

int lightSwitch;

void setup(){
  pinMode(13, INPUT);
  servo.attach(9);
  turnOn();
}

void turnOn(){
  lightSwitch = 1;
  servo.write(80);
  delay(1000);
  servo.write(90);
}

void turnOff(){
  lightSwitch = 0;
  servo.write(70);
  delay(1000);
  servo.write(90);
}

void loop(){
  int buttonState;
  buttonState = digitalRead(13);
  if ((buttonState == LOW) && (lightSwitch = 0)){
    turnOn();}
  if ((buttonState == LOW) && (lightSwitch = 1)){
    turnOff();}
  }

if ((buttonState == LOW) && (lightSwitch = 0)){ turnOn();}

You are doing a test of equality AND and assignment in the same line, and I don't think that's what you intend to do.

And put the closing brace '}' on a separate line. It doesn't affect the code, just makes it easier to read.

If you're having trouble seeing what's happening, I'd recommend adding some Serial.print() statements. I'll suggest printing a message each time turnOn() or turnOff() is entered, and each time the button is found low. I'll suggest that those messages state the value of lightSwitch, too. You'll learn a lot about what's going on in the program.

You may want to reexamine your servo angles, too. It looks like both deflections are in the same direction from the neutral position, which doesn't sound right for a toggle switch.

dannable:

if ((buttonState == LOW) && (lightSwitch = 0)){ turnOn();}

You are doing a test of equality AND and assignment in the same line, and I don't think that's what you intend to do.

And put the closing brace '}' on a separate line. It doesn't affect the code, just makes it easier to read.

I'm pretty sure the code is correct in that line, but thanks for giving me the tip about the brace.

KuphJr: I'm pretty sure the code is correct in that line, but thanks for giving me the tip about the brace.

It isn't.

= (assignment) and == (equality) operators aren't interchangeable.

Test code you can try.

//zoomkat servo button toggle test 4-28-2012

#include <Servo.h>
int button = 5; //button pin, connect to ground to move servo
int press = 0;
Servo servo;
boolean toggle = true;

void setup()
{
  pinMode(button, INPUT); //arduino monitor pin state
  servo.attach(7); //pin for servo control signal
  digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
  press = digitalRead(button);
  if (press == LOW)
  {
    if(toggle)
    {
      servo.write(160);
      toggle = !toggle;
    }
    else
    {
      servo.write(20);
      toggle = !toggle;
    }
  }
  delay(500);  //delay for debounce
}

Thank you all for your help. I have figured out by using a boolean statement. You have made my first post on the forum a very positive experience!