Controlling 12v DC motor with photosensor

Greetings,

I have been able to successfully control my linear actuator (12 volt motor with 2 relay module switch) in and out based on run-time. What I would like to do next is control the actuator motor using my adjustable photosensor module.
I am assuming I need some sort of "if" statements, but I am unsure beyond that.
I would appreciate some specific guidance on this one if possible.

I thank you in advance for your time and expertise.

Here is the code so far:

#define CW 7 //CW is defined as pin #7//
#define CCW 8 //CCW is defined as pin #8//

void setup() { //Setup runs once//

pinMode(CW, OUTPUT); //Set CW as an output//
pinMode(CCW, OUTPUT); //Set CCW as an output//

}

void loop() { //Loop runs forever//
digitalWrite(CW,HIGH); //Motor runs clockwise//
delay(50000); //for 50 second//
digitalWrite(CW, LOW); //Motor stops//
digitalWrite(CCW, HIGH);//Motor runs counter-clockwise//
delay(50000); //For 50 second//
digitalWrite(CCW, LOW); //Motor stops//
}

What photosensor do you have? Is it analog or digital?

You can test it by using the Digital Read Serial Example or the Analog Read Serial Example.

Then yes, you can use an if-statement with the results of your light sensor. It the sensor puts-out an analog value you'll probably want to use greater-than or less-than with the if-statement.

Since there are 3 conditions, I assume you have some other switch or end-sensors, or something?

You need to define the problem better. What do you mean by control the actuator with your photosensor? Are you trying to read the photosensor? Is it an analog or digital input? Or are you referring to some sort of limit switch so you can tell when you are at the end of your travel?

Thanks guys, and apologies for the lack of info.
My module has both digital and analog outputs. I am assuming I would want to use the analog output for this, correct? That would then give me the measurable variables to use for my if statement.

Yes, I want to read the photosensor to control the actuator motor based on the amount of light available.

My linear actuator already has build-in limit switches (stops) at the end of its travel, which I am happy to use, so I do not need to concern myself with other limit switches.

Hoping somebody can help a noob who has made it surprisingly far with this, but just needs a little extra help getting the project finished.

I believe I have correctly wired my linear actuator to be controlled by the arduino. Using the below code I am able to move the actuator inside and out, for a specified time:

#define CW 7 //CW is defined as pin #7//
#define CCW 8 //CCW is defined as pin #8//

void setup() { //Setup runs once//

pinMode(CW, OUTPUT); //Set CW as an output//
pinMode(CCW, OUTPUT); //Set CCW as an output//

}

void loop() { //Loop runs forever//
digitalWrite(CW,HIGH); //Motor runs clockwise//
delay(50000); //for 50 second//
digitalWrite(CW, LOW); //Motor stops//
digitalWrite(CCW, HIGH);//Motor runs counter-clockwise//
delay(50000); //For 50 second//
digitalWrite(CCW, LOW); //Motor stops//
}

However, I would now like the actuator to move in an out based on detected light levels. (CW when dark, CCW when light).
I understand I will need to use an if statements that account for light levels. But I am at a loss in understanding how to write that section of the code. The instructions I have read show how to control an LED, but nothing about bi-directional motors.

I am using the following:
-UNO
-2 relay module
-Photoresistor
-12 volt linear actuator, with built in limit switches which I intend to use as stops

Thanks to the forum

snoutstalker:
The instructions I have read show how to control an LED, but nothing about bi-directional motors.

Do you understand what those examples are doing? It should be trivial to distinguish the part that is the if statement from the part that turns on the LED.

If I handed you a CD player and you asked me to show you how to work it and I played a Led Zeppelin CD for you, would you look at me and say, OK now show me how to play a Michael Jackson one?

At the very least you need to make an effort and post your code here. We can't help much without that.

Why would I choose MJ over Led Zeppelin Shannon? I love the rock!

If I understood all of these details I wouldn't be asking for assistance. And please don't assume I haven't made any effort to learn this stuff, because I have spent hours and hours reading and experimenting, but this stuff doesn't always come as easily as listening to a CD for some of us. Further, did I not include my code?

Responses such as yours make it even more intimidating for beginners to ask for help.

snoutstalker:
Why would I choose MJ over Led Zeppelin Shannon? I love the rock!

If I understood all of these details I wouldn't be asking for assistance. And please don't assume I haven't made any effort to learn this stuff, because I have spent hours and hours reading and experimenting, but this stuff doesn't always come as easily as listening to a CD for some of us. Further, did I not include my code?

Responses such as yours make it even more intimidating for beginners to ask for help.

If you're so timid as to be intimidated by that, then the internet is going to be a scary scary place... If you want someone to help you learn to code I'm your guy. But it's probably going to be short and curt and to the point. I'm not much on the feels. If you need someone to help you through some feelings try here.

Further, did I not include my code?

Can you please format it like the forum rules ask you to?

You included the code that works. Is that what you want help with? Or do you want help with the code that involves the linear actuator. It isn't mentioned at all in the code you improperly posted.

And BTW: My name isn't Shannon. That's my membership level. You can call me Delta_G. Or just DG for short.

How to use this forum - please read

DG, I have no issues with being to the point, as I am truly grateful for the help. I would appreciate direction with the coding of the actuator. I don't necessarily care if it stems from my sketch or not, I would just be tickled pink to see the actuator move based on light levels.

(fingers crossed my code pastes in properly now)

#define CW 7 //CW is defined as pin #7//
#define CCW 8 //CCW is defined as pin #8//


void setup() { //Setup runs once//

  pinMode(CW, OUTPUT); //Set CW as an output//
  pinMode(CCW, OUTPUT); //Set CCW as an output// 

}

void loop() { //Loop runs forever//
  digitalWrite(CW,HIGH); //Motor runs clockwise// 
  delay(50000);            //for 50 second// 
  digitalWrite(CW, LOW); //Motor stops//
  digitalWrite(CCW, HIGH);//Motor runs counter-clockwise// 
  delay(50000);            //For 50 second//
  digitalWrite(CCW, LOW); //Motor stops// 
}

If you wish to use the analog output of your module, it needs to put out 0-5V for the Arduino to read it. If that's the case, then you just need to read the value and based on what it is, adjust your actuator accordingly.

Again, there's nothing there about the light levels.

Look, if you tried and failed then post your failure here and we can help you get it right. If you want to know how if statement works then hit the reference area on this site. If you want a tutorial on C++ then there are plenty all over and they usually cover if statements pretty early. Nobody needs to write a special one here just for you.

Please just post the code with the if statements that you need help with. Take a stab at it and I'll help you get it right. But you gotta take the first step.

If you want someone to just write some code for you there is a section called "Gigs and Collaborations" where you can hire someone.

Cross posted at : [MERGED] Control Linear Actuator with Photoresistor - Programming Questions - Arduino Forum

Cross post reported. Please, one thread per topic. You waste people's time when they end up giving you help you already have on another thread.

Cross post here: Controlling 12v DC motor with photosensor - Programming Questions - Arduino Forum

Reported.

Threads merged.