If statement error and general comments/advise - DIY Motorized Window Blinds

Hi All,

I am new to this and trying to learn all I can, so I apologize for my lack of knowledge!! Any help or advise would be greatly appreciated!

Anyway, I am working to add a stepper motor to my blinds to control the up and down movement. I have code the works but it is not doing what I need and I am not sure how to handle it. Ideally I would like three specific positions for the blinds: Open, Closed and Half Open. To make it similar I have commented the Half Open code out and I am focusing on Open and Close.

My first issue is with my if statements, I get "exit status 1 lvalue required as left operand of assignment". I added two variables Open and Closed to identify the position of the blinds. Before the setup loop I declared them and gave a value of 0 = Open and 1 = Closed. This is so I can change these when the code runs and so the blind doesn't try to open when it is already open. I tried == and = and either work and I still get this same error. If I take out the last two arguments it works no problem.

If anyone can help me out with this error and if you have any advise or comments on how to better handle this I would be GREATLY APPRECIATED!! Thanks in advance!

Thanks,
Brian

//Libraries to Include
#include <Stepper.h>

//Variables, Pins
#define STEPS 32 //Number of steps per revolution of internal shaft
int Steps2Take; // 2048 = 1 Revolution;
int Open = 0;
int Closed = 1;
//int HalfOpen = 0 ;

//Declare objects
//In1, In2, In3, In4 in the sequence 1-3-2-4
//initialize the stepper library on pins 8 through 11:
Stepper myStepper(STEPS, 8, 10, 9, 11);

void setup() {
  
//Start Serial Connection
Serial.begin(9600);

//Configure Pins
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
//pinMode(4, INPUT_PULLUP);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
//pinMode(7, OUTPUT);

}

void loop() {
//Read the pushbutton value into a varible
int sensorVal1 = digitalRead(2);
int sensorVal2 = digitalRead(3);
//int sensorVal3 = digitalRead(4);


//Print the sensor values
Serial.println(Open);
Serial.println("Open value");
Serial.println(Closed);
Serial.println("Closed value");
//Serial.println(HalfOpen);
Serial.println(sensorVal1);
Serial.println(" - S1, ");
Serial.println(sensorVal2);
Serial.println(" - S2, ");
//Serial.println(sensorVal3);
//Serial.println(" - S3, ");

if (sensorVal1 == 0 && sensorVal2 == 1 && Open = 0 && Closed = 1){
    digitalWrite(5, HIGH);
    Serial.print("Blinds OPENING CW, ");
    
    //Set the speed at 500 rpm(max):
    myStepper.setSpeed(500);
    //Set Steps2Take at one revolution (2048 steps)
    Steps2Take = 2048;
    //Tell stepper to rotate CW the number of steps = Steps2Take
    myStepper.step(Steps2Take);
    Open = 1;
    Closed = 0;
    Serial.print("Blinds OPENED, ");
    delay(1000);
}
if (sensorVal2 == 0 && sensorVal1 == 1 && Open = 1 && Closed = 0){
    digitalWrite(6, HIGH);
    Serial.print("Blinds Closing CCW, ");
    
    //Set the speed at 500 rpm(max):
    myStepper.setSpeed(500);
    //Set Steps2Take at one revolution (2048 steps)
    Steps2Take = -2048;
    //Tell stepper to rotate CW the number of steps = Steps2Take
    myStepper.step(Steps2Take);
    Closed = 1;
    Open = 0;
    Serial.print("Blinds Closed CCW, ");
    delay(1000);
}
//if (sensorVal3 == 0 && sensorVal1 == 1 && sensorVal2 == 1){
    //digitalWrite(7, HIGH);
    //Serial.print("Blinds to HALF OPEN CW, ");
    
    //Set the speed at 500 rpm(max):
    //myStepper.setSpeed(500);
    //Set Steps2Take at one revolution (2048 steps)
    //Steps2Take = 1024;
    //Tell stepper to rotate CW the number of steps = Steps2Take
    //myStepper.step(Steps2Take);
    //Serial.print("Blinds HALF OPENED, ");
    //delay(1000);
//}
}

Stepper_Motor_with_three_buttons.ino (2.44 KB)

Now if you not only commented out the code but also removed it, then do a CTRL-T to fix your indentation. That'd be a great improvement for us to read it.

#define STEPS 32 //Number of steps per revolution of internal shaft
int Steps2Take; // 2048 = 1 Revolution;
int Open = 0;
int Closed = 1;
//int HalfOpen = 0 ;

//Declare objects
//In1, In2, In3, In4 in the sequence 1-3-2-4
//initialize the stepper library on pins 8 through 11:
Stepper myStepper(STEPS, 8, 10, 9, 11);

A few things here.
Don't use multiple variables for the status, that's added complication. If only open/close I'd use a single bool: true is open, false is closed. As you have three unique states to keep track of, I'd do it a bit different.

#define OPEN 0
#define HALFOPEN 1
#define CLOSED 2
byte blindsStatus = OPEN;

Now you can refer to the status by name, and it's still in a single variable.

I hate names as "myStepper" - that's fine for generic example code, better give it a more descriptive name.

if (sensorVal1 == 0 && sensorVal2 == 1 && Open = 0 && Closed = 1){

= is assignment
== is boolean comparison

Commonly you'd use HIGH and LOW for signals, but that's just an alias of 1 and 0.

So your statement could be:

if (sensorVal1 == LOW && sensorVal2 == HIGH && blindStatus == CLOSED){
  openBlinds();
}

Then the problem of trying to open when already open: you'll probably need limit switches here, unless you can be very sure of your stepper motors placing your blinds in the exact correct position. Same for fully closing the blinds.

Hi wvmarle,

Thank you so much for the help!!! I will review tonight and put it to use. I also wanted to use variables to determine the position. Those were the Open, Closed and Halfopen variables but I wasn't sure if they were setup correctly. Other programming languages ive had to define what kind of variable it was but couldn't find much information for arduino ide. So again thank you so much for the help!!!

Thanks,
Brian

All variables have to be defined by type, including constants.

I was using a #define macro here - which means the word is replaced by the content of the macro upon compile time (mostly used for values but you can also use it for functions, such as the F() macro).

Thank you so much, I made the suggested changes and it works perfect now. I just need to figure out the limit switches since Id rather not have to use these so I am trying to figure out a way without the need for these. Thanks again!!