Curtain Project

Hello All!

I am currently completing a school project where I am using a stepper motor to move a curtain up and down based on a light sensor. I am using a stepper because I already had the motors handy. This project is basically a basic mechatronics program where we need to utilize a microcontroller, a sensor, and an actuator. The high level basis of the project is to simulate an automatic window curtain.

I am using an Arduino Uno with a stepper motor shield provided by adafruit (Arduino v2 Kit - v2.3). I used the example code provided by adafruit to get the stepper to spin in a loop.

My goal is to use a photocell provided with the complete arduino kit to control whether the stepper will move up or down. Below is a sequence of what I am trying to accomplish.

Sequence:
Power on
Check if sensor is on or off
If off then
rotate counter clockwise for a set timer
stop
If on then
rotate clockwise for a set timer
stop

I also would like the above to only start spinning if the sensor changes, I don't want a loop to check if the sensor is on/off because (in my mind) that means the stepper would constantly be spinning.

I am completely new to coding in general (I am a mechanical engineering student) so trying to figure out how to piece all of the information together has been difficult to say the least.

Any advice, any demonstrations, code examples, explanations, written code would be very helpful.

If providing code, please explain the pieces of it so I can logically wrap my head around the concept in order to utilize it.

Thank you for reading!

For reference below is the code I am currently working with for the stepper motor and shield.

#include <AFMotor.h>
 
 
AF_Stepper motor(48, 2);
 
 
void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Stepper test!");
 
  motor.setSpeed(10);  // 10 rpm   
 
  motor.step(100, FORWARD, SINGLE); 
  motor.release();
  delay(1000);
}
 
void loop() {
  motor.step(100, FORWARD, SINGLE); 
  motor.step(100, BACKWARD, SINGLE); 
 
  motor.step(100, FORWARD, DOUBLE); 
  motor.step(100, BACKWARD, DOUBLE);
 
  motor.step(100, FORWARD, INTERLEAVE); 
  motor.step(100, BACKWARD, INTERLEAVE); 
 
  motor.step(100, FORWARD, MICROSTEP); 
  motor.step(100, BACKWARD, MICROSTEP); 
}

I am completely new to coding in general (I am a mechanical engineering student) so trying to figure out how to piece all of the information together has been difficult to say the least.

I don't understand that statement. You had to assemble the curtain, tracks, stepper motors, wiring, etc., too, didn't you? You didn't just throw it all on the table, and it magically assembled itself. Why do you expect writing code to be any different?

Where do you read a photocell? Why don't you? That is one of the pieces of the project, isn't it?

An if statement - if it is dark enough, close the curtain - is trivial. Adding an else block, to close the curtain is not difficult.

Put the code to open the curtain in a function. Put the code to close the curtain in another function. Then, opening and closing the curtain will be trivial.

Except, that just spinning the motor a fixed number of steps, and hoping that that results in an open or closed curtain is silly. Change the curtain size, and rewrite the code? In your dreams. That's what limit switches are for.

PaulS:
I don't understand that statement. You had to assemble the curtain, tracks, stepper motors, wiring, etc., too, didn't you? You didn't just throw it all on the table, and it magically assembled itself. Why do you expect writing code to be any different?

I don't expect coding to be different, I have 0 experience with coding and was given a week and a half to figure out how to do it. I apologize for reaching out to see if I can get some specified help in regards to the project. I already tried to do the research. The wiring and coding are not done, I do not know how to do them, I am continuing my research in parallel to posting here.

The mechanical setup is only a 4 plank wood frame with a bearing, aluminum rod, and a curtain mounted to the end of the shaft (this isn't rocket science). This is obviously the easiest part due to the complexity and my major.

PaulS:
Where do you read a photocell? Why don't you? That is one of the pieces of the project, isn't it?

The piece of the project is to use a sensor. I picked the photocell because it was available in my kit. There was no instructions or direction on what project to pick or what sensors to use.

PaulS:
An if statement - if it is dark enough, close the curtain - is trivial. Adding an else block, to close the curtain is not difficult.

Please provide an example of what you mean so I can have a better understanding..

PaulS:
Put the code to open the curtain in a function. Put the code to close the curtain in another function. Then, opening and closing the curtain will be trivial.

Except, that just spinning the motor a fixed number of steps, and hoping that that results in an open or closed curtain is silly. Change the curtain size, and rewrite the code? In your dreams. That's what limit switches are for.

I appreciate the advice, but the tone and the way it was presented was a bit rude. I completely understand I am new, and that I should be doing my own research to figure it out on my own. I am not trying to have someone write me the whole code, I am just trying to do a basic project and trying to find all the pieces to put this together.

I considered a limit switch after to make it easier, but I don't have one handy to do this.

drixy:
I don't expect coding to be different, I have 0 experience with coding

If that's the case, the only way for someone to "help" you is to give you a complete code. That's not going to happen, the purpose of this forum is to help folks with code they've written.

You're gonna have to learn some basic coding and the structure of an Arduino sketch. So, start up the Arduino IDE (which by now you should have installed on a computer), and click on "File -> Examples". Start at the beginning "01.Basics"

There are also numerous online tutorials.

The mechanical setup is only a 4 plank wood frame with a bearing, aluminum rod, and a curtain mounted to the end of the shaft (this isn't rocket science).

Neither is coding. You weren't born knowing how to assemble the frame, what bearing to use, what diameter rod to use, that there needs to be some relationship between the bearing diameter and the rod diameter, or how to attach the current to the rod. You were (presumably) able to learn all that. Learn to write code, too.

but the tone and the way it was presented was a bit rude.

Get a thicker skin. You ain't seen rude yet. Try posting piss-poorly formatted code without using code tags, if you want to see rude.

I considered a limit switch after to make it easier, but I don't have one handy to do this.

Be sure to include, in the project write up, that you couldn't be bothered getting two limit switches, to make the project realistic.

Thanks for nothing!

I'll figure it out on my own.

Post once about a project, I can clearly see why most people post elsewhere due to the lack of useful information and the mentality of the users here.

Thanks for nothing!

That seems to be about the level of effort you have put into learning programming.

Photocells are pretty easy to work with, check out the Adafruit tutorial to read all about them.

The thing that is a bit tricky is their reading is just a number 0-1023, it's up to you to determine what that means in your setup. Maybe in the place your school project is in the highest value will be about 600 - that means the project needs to treat 600 as full sunlight. Let's say for the purpose of this post the curtain only has three states: full down, halfway up, full up.

If this was my project the first thing I would do is hook up the photocell and just print the readings to the serial monitor. Based on the numbers I would determine at what reading range the curtain should be full up, halfway up and full down.

The next thing I would do is begin with the curtain fully down and spin the motor to see how long it takes to get half way up and full up.

Then I would put it together:

  • read the sensor
  • based on the reading, where should the curtain be?
  • where is the curtain now?
  • move it to where it should be if it's not already there

With more time and resources it would be better to always get the curtain in a known state before reading the photocell, but you should make something that is realistic in the time you have.

Remember to make good use of the serial monitor, printing out what's happening so you know what's going on under the hood (and write good comments).

drixy:
Sequence:
Power on
Check if sensor is on or off
If off then
rotate counter clockwise for a set timer
stop
If on then
rotate clockwise for a set timer
stop

I also would like the above to only start spinning if the sensor changes, I don't want a loop to check if the sensor is on/off because (in my mind) that means the stepper would constantly be spinning.

You started ! 10 days may not be quite enough to get there though, but don't let Paul put you off. You do want to get some stopper switches, magnetic i think, you can imagine what happens when if the motor keeps pulling beyond it's limits But with the exception of the limiting switches. So you could open and close the curtain using switches at the moment. Now the electronics part needs to be done also, connecting the motor straight onto the Arduino is not a good plan, even a relay is usually a bit much for the pins, but a relay-board or an Open-collector or transistor to drive the relay(s) should be fine. Do you have all that ? Now you were talking about a kit, so i figure you do have some LED's and resistors they may come in handy while you are testing your program. 10 days should be enough depending a little bit on how much time you have a day (and your IQ) This part of the forum is not about doing the work for you, for that you should try Gigs & Collabs, but we can help you find mistakes.