So i’m going to start by saying that I am not well versed in most of what the Arduino UNO can do. What i’m trying to accomplish is having the arduino execute code that will rotate a stepper motor to given position then stop. A ambient light sensor then will activate the motor when a set threshold is reached to rotate back to its starting position. I’m not sure how to code it to were it will loop checking the ambient sensor without repeating the first motor rotation. Then exacting the second rotation with it being thrown into a endless loop that will stop it from repeating the entire program. Here is what I’ve been working with so far. (Sorry for the messy code, I googled most of it) The Prints were me attempting too troubleshoot.
What i’m using: Arduino UNO, 28BYJ-48 stepper motor, ULN2003 Driver Board,and UCNL4010 proximity/light sensor.
#include <Stepper.h>
#include <Wire.h>
#include "Adafruit_VCNL4010.h"
Adafruit_VCNL4010 vcnl;
const int stepsPerRevolution = 2048;
boolean runA = true;
int ambLim = 80;
Stepper myStepper = Stepper(stepsPerRevolution, 8, 10, 9, 11);
void setup() {
myStepper.setSpeed(10);
Serial.begin(9600);
}
void loop() {
Serial.println("program start");
Serial.println(runA);
if (runA == true)
{
delay(2000);
// Step one revolution in one direction:
Serial.println("open");
myStepper.step(stepsPerRevolution * 2);
runA = false;
delay(1000);
Serial.println(runA);
delay(1000);
}
Serial.println("waiting for sensor");
delay(1000);
Serial.print("Ambient: "); Serial.println(vcnl.readAmbient());
delay(1000);
Serial.print("Proximity: "); Serial.println(vcnl.readProximity());
delay(500);
if(vcnl.readAmbient() > ambLim)
{
Serial.println("close");
delay(3000);
myStepper.setSpeed(10);
myStepper.step(-stepsPerRevolution * 2);
}
}