I have a light sensor device (LM393 3-pin device) that provides a 0/1 output. Can you recommend a sketch example that I can modify to incorporate into my sketch to start a program loop when there is light and stop the loop when there is no light.
It seems simple, but I couldn't find one in the Arduino IDE 2.3.4 Library Manager search. Thank you for the help.
just test the state of the sensor at the beginning of the loop, the loop isn't stopped, your code is just not executed
assuming the sensor being HIGH means there is light
const byte lightSensorPin = 2;
void setup() {
pinMode(lightSensorPin, INPUT); // if it really gives HIGH and LOW (not even necessary as pins are input by default)
}
void loop() {
if (digitalRead(lightSensorPin) == LOW) {
// what do you want to do when there is no light
} else {
// what do you want to do when there is light
}
}
Have a look at the Button example in the IDE
Thank you. For the "what do you want to do when ........." , what is the command to proceed to the start of the loop and to not proceed?
Have you got the code for the loop that you are referring to ?
return; will end the loop and return to the main function which will call the loop() function again. That’s why it’s called a loop
Just let the loop spin empty if you don’t want to do anything
Side note: not sure you fully grasped the setup and loop concepts . May be spend time to get the basics of C++ language, functions etc
yes. thank you. Here is the code I want to start after light is detected, a 1, and not to run when light goes to 0. thanks,
/*
Owl Stepper Motor Control
This program drives a unipolar or bipolar stepper motor.
The motor is attached to digital pins 8 - 11 of the Arduino.
The motor revolves 1 revolution in one direction, then after 15s or so delay,
3/4 revolution in the other direction.
*/
#include <Stepper.h>
const int stepsPerRevolution = 2048; // change this to fit the number of steps per revolution
// for your motor 28byj-48 which is 2048
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);
void setup() {
// set the speed at 10 rpm:
myStepper.setSpeed(10);
// initialize the serial port:
Serial.begin(115200);
}
void loop() {
// step one revolution in one direction:
Serial.println("clockwise");
// This is the value for 1 revolution
myStepper.step(stepsPerRevolution * 1.0);
// Turn OFF Motor current so no overhead. No need to hold motor position.
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
// Set this delay for 15 seconds. 1000=1s. 15000=15s, 3600000 is 1hour. .
delay(15000);
// step one revolution in the other direction:
Serial.println("counterclockwise");
// turn ON motor current
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
// 1S delay to allow motor to stabilize
delay (1000);
// and switch directions before starting loop() again for .75 revolution
myStepper.step(-stepsPerRevolution * .5);
// Turn OFF Motor current so no overhead. No need to hold motor position.
//Also allows user to manually postion while motor is off.
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
//Set this delay for 15s. 1s =1000. 15000=15 seconds. and then start over from the top
delay(15000);
}
Start with the sketch from @J-M-L and put the code in your setup() function in his setup()
Add a new function to his sketch named runStepper() and put all of the code in your loop() function in that new function
Add a new global boolean variable named running to the sketch and set it to false
Where you see
// what do you want to do when there is no light
set running to false and where you see
// what do you want to do when there is light
set running to true
at the end of loop() in your code test the value of running and if it is true call runStepper()
There will be details to sort out but I hope that you can see the principle used
Thank you. I'll experiment and see what I can do work.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.