Programming a pin height checker Jig

Hello,

I'm having some problem in writing this program. I cant get the motor to stop when the 'lux' value drops. Here is the code:

#include <AccelStepper.h>
#include <MultiStepper.h>
#include <Wire.h>
#include <BH1750.h>
BH1750 lightMeter;

const int laser_on_off = 12; //laser on
const int limit_SW_L = 3;
const int limit_SW_R = 2;
const int pcb_detect_SW = 4;
const int reset_SW = 5;
const int led_yellow = 6;
const int led_green = 7;
const int led_red = 8;
unsigned long time = millis();

// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button

AccelStepper stepper1(1, 11, 10); // (Type:driver, STEP, DIR)
MultiStepper StepperControl; // Create instance of MultiStepper
long gotoposition[1];

int sliderPos = 0;

byte
currSwitch1,
lastSwitch1;

byte
currSwitch2,
lastSwitch2;

byte
currSwitch3,
lastSwitch3;

unsigned long
timeStart;
unsigned long
timeStart1;
unsigned long
timeStart2;
unsigned long
timeStart3;

bool
bCheckingSwitch1;
bool
bCheckingSwitch2;
bool
bCheckingSwitch3;

void setup()
{
pinMode( limit_SW_L, INPUT_PULLUP );
lastSwitch1 = digitalRead( limit_SW_L );
bCheckingSwitch1 = true;

pinMode( limit_SW_R, INPUT_PULLUP );
lastSwitch2 = digitalRead( limit_SW_R );
bCheckingSwitch2 = true;

pinMode( reset_SW, INPUT_PULLUP );
lastSwitch3 = digitalRead( reset_SW );
bCheckingSwitch3 = false;

pinMode( pcb_detect_SW, INPUT_PULLUP );
lastSwitch3 = digitalRead( pcb_detect_SW );
currSwitch3 = true;

pinMode(laser_on_off, OUTPUT);
digitalWrite(laser_on_off, HIGH);

Serial.begin(9600);
Wire.begin();
lightMeter.begin();
Serial.println(F("BH1750 Test"));

pinMode(laser_on_off, OUTPUT); //laser on
digitalWrite(laser_on_off, HIGH);

}

void loop() {

float lux = lightMeter.readLightLevel(true);
Serial.print("Light: ");
Serial.print(lux);
Serial.println(" lx");
delay(1000);

// read the button input pin:
buttonState = digitalRead(pcb_detect_SW);
// compare the buttonState to its previous state
if (buttonState == LOW && lastButtonState == 0) {
// if the state has changed, increment counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter--;

digitalWrite(laser_on_off, LOW);
// Delay a little bit to avoid bouncing
delay(100);
}

if (lux < 5) { //if light sensor value drops
stepper1.stop(); // stop the motor
}

if (buttonPushCounter % 2 == 0){
delay(30);
StepperControl.addStepper(stepper1);
stepper1.setMaxSpeed(30000);
while (digitalRead(limit_SW_R) != 0) {
stepper1.setSpeed(10000);
stepper1.runSpeed();
}
}
delay(50);
StepperControl.addStepper(stepper1);
stepper1.setMaxSpeed(30000);
while (digitalRead(limit_SW_L) != 0) {
stepper1.setSpeed(-10000);
stepper1.runSpeed();

}
}
}

#include <AccelStepper.h>

#include <MultiStepper.h>
#include <Wire.h>
#include <BH1750.h>
BH1750 lightMeter;

const int laser_on_off = 12; //laser on
const int limit_SW_L = 3;
const int limit_SW_R = 2;
const int pcb_detect_SW = 4;
const int reset_SW = 5;
const int led_yellow = 6;
const int led_green = 7;
const int led_red = 8;
unsigned long time = millis();

// Variables will change:
int buttonPushCounter = 0;  // counter for the number of button presses
int buttonState = 0;        // current state of the button
int lastButtonState = 0;    // previous state of the button

AccelStepper stepper1(1, 11, 10); // (Type:driver, STEP, DIR)
MultiStepper StepperControl;  // Create instance of MultiStepper
long gotoposition[1];

int sliderPos = 0;

byte   
    currSwitch1,
    lastSwitch1;

byte   
    currSwitch2,
    lastSwitch2;

byte   
    currSwitch3,
    lastSwitch3;

unsigned long
    timeStart;
unsigned long
    timeStart1;
unsigned long
    timeStart2;
unsigned long
    timeStart3;
 
bool
bCheckingSwitch1;
bool
bCheckingSwitch2;
bool
bCheckingSwitch3;

void setup()
{
    pinMode( limit_SW_L, INPUT_PULLUP ); 
    lastSwitch1 = digitalRead( limit_SW_L );
    bCheckingSwitch1 = true;
   
    pinMode( limit_SW_R, INPUT_PULLUP ); 
    lastSwitch2 = digitalRead( limit_SW_R );
    bCheckingSwitch2 = true;
 
   
    pinMode( reset_SW, INPUT_PULLUP ); 
    lastSwitch3 = digitalRead( reset_SW );
    bCheckingSwitch3 = false;

pinMode( pcb_detect_SW, INPUT_PULLUP ); 
    lastSwitch3 = digitalRead( pcb_detect_SW );
    currSwitch3 = true;

pinMode(laser_on_off, OUTPUT);
    digitalWrite(laser_on_off, HIGH);

Serial.begin(9600);
  Wire.begin();
  lightMeter.begin();
  Serial.println(F("BH1750 Test"));

pinMode(laser_on_off, OUTPUT); //laser on
    digitalWrite(laser_on_off, HIGH);
 
}

void loop() {

float lux = lightMeter.readLightLevel(true);
  Serial.print("Light: ");
  Serial.print(lux);
  Serial.println(" lx");
  delay(1000);

// read the button input pin:
  buttonState = digitalRead(pcb_detect_SW);
  // compare the buttonState to its previous state
  if (buttonState == LOW && lastButtonState == 0) {
    // if the state has changed, increment counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter--;     
 
  digitalWrite(laser_on_off, LOW);
    // Delay a little bit to avoid bouncing
    delay(100);
  }

if (lux < 5) {  //if light sensor value drops
stepper1.stop();    // stop the motor
}

if (buttonPushCounter % 2 == 0){
    delay(30);
  StepperControl.addStepper(stepper1);
    stepper1.setMaxSpeed(30000); 
  while (digitalRead(limit_SW_R) != 0) {
    stepper1.setSpeed(10000);
    stepper1.runSpeed();
  }
  }
  delay(50);
StepperControl.addStepper(stepper1);
stepper1.setMaxSpeed(30000); 
  while (digitalRead(limit_SW_L) != 0) {
    stepper1.setSpeed(-10000);
    stepper1.runSpeed();

}
}
}

You keep calling setSpeed() everytime through loop() I think, so that calling stop() isn't going to have much effect.

You button logic looks pretty suspect:

  // read the button input pin:
  buttonState = digitalRead(pcb_detect_SW);
  // compare the buttonState to its previous state
  if (buttonState == LOW && lastButtonState == 0) {
    // if the state has changed, increment counter
    if (buttonState == HIGH) {

The second if statement must always fail as you've just checked that buttonState is low...

I'd start by pulling out the button handling into a separate function called by loop(),
and the same for the motor driving, so that its all nice small easily understood functions.

The indentation isn't consistent - that can lead to confusion.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.