As the subject states i want to use a phototransistor to start a motor and when the motor starts it’s supposed to stay on for 7 seconds. And if the phototransistor is affected during the 7 seconds it should ignore that. the whole project is basically when the phototransistor reaches a certain value in my room the motor is suppose to go on for 7 seconds and then shut itself of. Hope you understand what i mean. Would be happy if anyone can point me in the right direction.
the code below is what i got so far
int sensePin =0;
int motorPin = 12;
void setup (){
analogReference(DEFAULT);
pinMode(motorPin, OUTPUT);
}
void loop () {
int val = analogRead(sensePin);
if(val < 100) digitalWrite(motorPin,LOW);
else digitalWrite(motorPin, HIGH);
}
If you really are doing nothing more than turning on the motor and then turning it off after 7 secs, why not ;
- read the analog voltage
- decide if it's over the threshold (I would add and over for long enough)
- if over threshold
* turn on the motor
* delay 7 secs ( delay(7000); )
* turn the motor off
- go back to top of loop
There are other ways but the above is the simplest and good enough if that’s all you’re doing.
http://arduino.cc/en/Reference/Delay
The problem is if i use delay it will turn it of for only 7 seconds and then turn the motor back on. Its only supposed to start one time once the value on the phototransistor has been reached.
OK so it’s a one time only deal … presumably until the Arduino is reset or power removed ?
Use a variable, motorRun, and initialize it to 0. When the motor runs, set it = 1. The motor has run. Now add an additional statement as in :
- if over the threshold and if motorRun = 0
* then run the motor for 7 secs
* motorRun = 1
* etc, etc
That way the motor will run only when it hasn’t run before.
“presumably until the Arduino is reset or power removed ?” Yes
I still don’t get it.
I made changed my code now and this is the result.
int sensePin =0;
int motorPin = 12;
void setup (){
analogReference(DEFAULT);
pinMode(motorPin, OUTPUT);
}
void loop () {
int val = analogRead(sensePin);
if(val < 100) digitalWrite(motorPin,LOW);
else digitalWrite(motorPin, HIGH);
int motorRun = digitalRead(motorPin);
if(motorRun == HIGH) digitalWrite(motorPin,LOW);
else delay(7000); digitalWrite(motorPin,HIGH);
}
So now when i hold my hand over the phototransistor it starts the motor for 7 seconds before turning of. But if i keep my hand over the phototransistor it wont turn of after 7 seconds. I tried doing what you wrote but my arduino programming skills aint that good yet 
I"ll assume 100 is your threshold and a HIGH turns the motor on. If that’s backwards, well you can figure it out from here.
//declare the constants
const int sensePin = 0;
const int motorPin = 12;
//declare the global variables
int motorRun = 0;
int val = 0;
void setup () {
Serial.begin(9600); //initialize serial port to 9600 baud
analogReference(DEFAULT); //not really need but OK
pinMode(motorPin, OUTPUT); //set pin to output
digitalWrite(motorPin,LOW); //make sure motor is off
}
void loop () {
val = analogRead(sensePin);
Serial.print("Sensor reads ");
Serial.println(val);
if( (val >= 100) && (motorRun == 0) ) {
//both above threshold and motor has not run yet
Serial.println("Turning motor on");
digitalWrite(motorPin, HIGH); //turn motor on
motorRun = 1; //memorize that motor has run
delay(7000); //wait 7 secs
Serial.println("Turning motor off");
digitalWrite(motorPin,LOW); //turn motor off
}
}
The above will turn on the motor for 7 secs on the VERY FIRST instance of val >= the threshold. Noise may trigger the motor.
http://arduino.cc/en/Reference/Boolean
ps - what do you have for a motor and how do you have it driven ?
Got some random DC motor(9v) which is connected to my relay.
When i use your code and I check the serial Monitor it says starting motor and it randomly starts and stops. i tried changing the value of the " if( (val <= 100) && (motorRun == 0) " to 300 but did not make any difference.
What do the printout say ? Is the reading from the sensor flucuating ? Does the motor turn off after 7 secs ? Do you see only 1 message to turn the motor on … and off ? Where’s the power for the motor coming from ? Hopefully not from the Arduino. What’s the coil for the relay rated at ? How much current does it draw ? Do you have a suppression diode in place ?
IOW are you seeing a software problem or a hardware problem ?
I think i got it working now. I changed all the HIGH values to LOW and the LOW to HIGH. I think somehow i inverted it when i hooked things up. Big thanks to you Mee_n_Mac. 