Go Down

Topic: stepper and laser read photo sensor project (Read 797 times) previous topic - next topic

ez8mm

i have built a simple auto sander using some aluminum construction a air grinder photo sensor laser and a stepper

at first mt plan was to load parts on this machine and have a simple  program that repeats it self with a button push   stepping 1500 steps sanding stepping 1500 steps sanding repeating this a total of 7 times and then unwinding the ball screw by stepping the motor backwards 89,000 steps to return the parts to the home position  using no calibration or laser at the time .. the machine worked well mechanical wise and repeated vary nicely but that only made a new problem for me.. being that with the auto sander repeating so well the parts vareyd and not all of the would clean up so i made a photo sensor with a thin hole for a window in a brass box to use the laser and pick up on every edge before its sanded ... but this program to run the laser and a calibrate the stepper before each sand is a little a head of my knowledge of c code and the arduino code. im going to try to make some thing work but im not sure where to start  i guessing i need a switch case statement to run 7 times ..

ill attach the code i gave a try to see if it would work with the laser i believe if i get my program right it will work fine

first ill try to ex-plane how the program need to work and maybe some one could tell me how i could do it

first i need a button to press to start the process
second after button is pressed i needs to run some kind of loop in the background checking the status of the laser photos sensor reading
the do 1 of 2 things like if or else  to utilize the photo sensor reading

first if the photo sensor attached to analog 7 has a high value then the motor needs to step forward like 1 to 4 steps and make a check again doing this over and over in till the photo sensor  gets a lower value

else if the photo sensor reads low value then we know there the part to sanded is step the 30 steps forward to the position and run the sanding pad across the face of the part the advancing to the next face by adding in 1500 steps to make sure our first read is a high value to allow the photo sensor to calibrate the stepper motor the..
repeat this  process again 7 times 

last after running threw 7 sanded faces  stop this loop and un wind the ball screw 89,000 steps to the home star position

ok here is the code i got to geather to test the laser and photo sensor
Code: [Select]

#include <AccelStepper.h>
#include <AFMotor.h>

#include <Stepper.h>

const int stepsPerRevolution = 140;  // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 30,32,34,36);         // <---- i dont think i need to use this but it was in the example i coppyed for stepper motors and i left it     

AF_Stepper motor1(200, 1);// <--- this ch is hooked to the stepper moveing my X axis to each notch of the part






int sensorValue = A7;// think i need this to map the value to a high low reading for the photo sensor
int lastValue = A7;// was going to try and crate a way to keep track of readings but i may not have to

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Stepper test!");
  pinMode(49, OUTPUT); // pin with realy for air valve
  pinMode(48, OUTPUT); //toggle...laser... save dieode life

  delay(3000);
  int sensorValue = analogRead(A7);
  Serial.println(sensorValue);




  // here is what i think i have to do  each notch needs to cabrate with a laser reading to photo sensor im not sure if i need to be in
  // a case  or while loop ? or if i could use the on that has its own calbrate statment





}

void loop() {
  // put your main code here, to run repeatedly:
  int sensorValue = analogRead(A7);
  Serial.println(sensorValue);



  if (sensorValue < 500 )// compare the sensepin to its previous state then do some thing
    // note on how to use laser read   if laser read is low value laser has been blocked by notch and need to actavate sander to poliah t
    at surfice

  {
    //wait one second for air to exit piston and return sander to home postion

    // using a comand to the stepper as follows will allow the next laser calbration to not have a false reading from the .400 thick high
    spot of notch
      // if it dose not advance forward past the high notch the laser with be geting the same reading from the last time it was checked
    motor1.setSpeed(220);
    motor1.step(10, FORWARD, DOUBLE);
    motor1.step(0, BACKWARD, DOUBLE);//   1500 steps is aproxmitly 0.375 inchs  .025 short of next notch
    motor1.release();


  }
  else  // if the state has changed, increment the steps to leds with the diffrance
  {
    //   high laser read tells me that we have not come to a place we want to sand yet
    motor1.setSpeed(220);
    motor1.step(0, FORWARD, DOUBLE);
    motor1.step(10, BACKWARD, DOUBLE);//4 steps is apromitly .001 of an inch for a move/ every time we get an else  do to a high read  we need to move
    // foward just a little bit to find the hight spot in the notch to get a low read on laser
    motor1.release();


  }

}





EZ8MMPRO

ez8mm

SOLVED !!!!

thank fully i figured this one out on my own but thank all of you who tried to give me some derction

i used a series on while statements to calibrate each move  i tried this before but some thing was wrong at the end of the while statement i used it was not checking my sensor to brake its self from the loop but adding a check inside the while statement seemed to clear up my bug i think the code is a mess at this point but it is working just as i had hoped few more tweaks and it should be smooth sailing ...

if i get a video posted ill link it when i got a sec

the code as it is now ill share here.
EZ8MMPRO

ez8mm

Code: [Select]

#include <AccelStepper.h>
#include <AFMotor.h>
#include <Stepper.h>

AF_Stepper motor1(200, 1);// not using this any more
AF_Stepper motor2(200, 2);// <--- this ch is hooked to the stepper moveing my X axis to each notch of the part





const int  D1 = A7;//< photo sensor pin call out for laser detection
int sensorValue = A7;// think i need this to map the value to a high low reading for the photo sensor
int lastValue = A7;

void setup() {
 Serial.begin(9600);           // set up Serial library at 9600 bps
 Serial.println("Stepper test!");
 pinMode(49, OUTPUT); // pin with realy for air valve
 pinMode(48, OUTPUT); //toggle...laser... save dieode life
 //spacer for laser below
 motor1.setSpeed(178);
 motor1.step(8, FORWARD, DOUBLE);
 motor1.step(4, BACKWARD, DOUBLE);//   1500 steps is aproxmitly 0.375 inchs  .025 short of next notch
 motor1.release();

 // spaser for laser above
 delay(300);
 int sensorValue = analogRead(A7);


 lastValue = map(sensorValue, 0, 600, 0, 600);
 Serial.println(sensorValue);
 sensorValue = analogRead(A7);

 

 sensorValue = analogRead(A7);
 while (sensorValue > 500){
   motor1.setSpeed(170);
   motor1.step(1, FORWARD, DOUBLE);
   motor1.step(0, BACKWARD, DOUBLE);//   1500 steps is aproxmitly 0.375 inchs  .025 short of next notch
   motor1.release();
   sensorValue = analogRead(A7);
 }

 Serial.println(sensorValue);
 delay(100);

 //spacer for laser below
 motor1.setSpeed(178);
 motor1.step(100, FORWARD, DOUBLE);
 motor1.step(0, BACKWARD, DOUBLE);//   1500 steps is aproxmitly 0.375 inchs  .025 short of next notch
 motor1.release();

 digitalWrite(49, HIGH);   // set the LED on to toggle realy to air valve
 delay(800);              // wait for a second for air to reach max psi in sander and piston
 digitalWrite(49, LOW);    // set the LED off relase air valve
 delay(1000);      //wait one second for air to exit piston and return sander to home postion

 // using a comand to the stepper as follows will allow the next laser calbration to not have a false reading from the .400 thick high

 // if it dose not advance forward past the high notch the laser with be geting the same reading from the last time it was checked
 motor1.setSpeed(220);
 motor1.step(1000, FORWARD, DOUBLE);
 motor1.step(0, BACKWARD, DOUBLE);//   1500 steps is aproxmitly 0.375 inchs  .025 short of next notch
 motor1.release();




 sensorValue = analogRead(A7);
 while (sensorValue > 500){
   motor1.setSpeed(170);
   motor1.step(1, FORWARD, DOUBLE);
   motor1.step(0, BACKWARD, DOUBLE);//   1500 steps is aproxmitly 0.375 inchs  .025 short of next notch
   motor1.release();
   sensorValue = analogRead(A7);
 }

 Serial.println(sensorValue);
 delay(100);

 //spacer for laser below
 motor1.setSpeed(178);
 motor1.step(100, FORWARD, DOUBLE);
 motor1.step(0, BACKWARD, DOUBLE);//   1500 steps is aproxmitly 0.375 inchs  .025 short of next notch
 motor1.release();

 digitalWrite(49, HIGH);   // set the LED on to toggle realy to air valve
 delay(800);              // wait for a second for air to reach max psi in sander and piston
 digitalWrite(49, LOW);    // set the LED off relase air valve
 delay(1000);      //wait one second for air to exit piston and return sander to home postion

 // using a comand to the stepper as follows will allow the next laser calbration to not have a false reading from the .400 thick high

 // if it dose not advance forward past the high notch the laser with be geting the same reading from the last time it was checked
 motor1.setSpeed(220);
 motor1.step(1000, FORWARD, DOUBLE);
 motor1.step(0, BACKWARD, DOUBLE);//   1500 steps is aproxmitly 0.375 inchs  .025 short of next notch
 motor1.release();


 sensorValue = analogRead(A7);
 while (sensorValue > 500){
   motor1.setSpeed(170);
   motor1.step(1, FORWARD, DOUBLE);
   motor1.step(0, BACKWARD, DOUBLE);//   1500 steps is aproxmitly 0.375 inchs  .025 short of next notch
   motor1.release();
   sensorValue = analogRead(A7);
 }

 Serial.println(sensorValue);
 delay(170);

 //spacer for laser below
 motor1.setSpeed(178);
 motor1.step(100, FORWARD, DOUBLE);
 motor1.step(0, BACKWARD, DOUBLE);//   1500 steps is aproxmitly 0.375 inchs  .025 short of next notch
 motor1.release();

 digitalWrite(49, HIGH);   // set the LED on to toggle realy to air valve
 delay(800);              // wait for a second for air to reach max psi in sander and piston
 digitalWrite(49, LOW);    // set the LED off relase air valve
 delay(1000);      //wait one second for air to exit piston and return sander to home postion

 // using a comand to the stepper as follows will allow the next laser calbration to not have a false reading from the .400 thick high

 // if it dose not advance forward past the high notch the laser with be geting the same reading from the last time it was checked
 motor1.setSpeed(220);
 motor1.step(0, FORWARD, DOUBLE);
 motor1.step(10050, BACKWARD, DOUBLE);//   1500 steps is aproxmitly 0.375 inchs  .025 short of next notch
 motor1.release();


}

void loop() {
 // no loop for this in till i figue out how to drop all this in to one large if statement for a push button switch:
delay(1000);
}
EZ8MMPRO

Go Up