Go Down

Topic: Aurduino IR sensor object counter with a stepper motor and driver (Read 66 times) previous topic - next topic

anks0985

Hi Guys,

I made a simple IR object counter on conveyor, componenets I am using are :

1. Aurduino UNO
2. 16X2 IC
3. 1 IR Sensor.

The problevm I facing in running the stepper motor with IR counting sketch. The complete sketch is below...the IR counter working perfectly fine witout these two line in VOID LOOP

  // step one revolution  in one direction:
   //Serial.println("forward");
  myStepper.step(stepsPerRevolution);




...pls help in correcting the code...


// include the library code:
#include <LiquidCrystal.h>
#include <Stepper.h>

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


// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(13, 12, 5, 4, 3, 2);
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8,9,10,11);


// set up a constant for the tilt switchPin
const int switchPin = 6;

int hits = 0;

// variable to hold the value of the switchPin
int switchState = 0;

// variable to hold previous value of the switchpin
int prevSwitchState = 0;

void setup() { 

  // set the speed at 60 rpm:
myStepper.setSpeed(60);


  // initialize the serial port:
  Serial.begin(9600);
 
  // set up the number of columns and rows on the LCD
  lcd.begin(16, 2);


  // set up the switch pin as an input
  pinMode(switchPin,INPUT);

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Hit the button");
  lcd.setCursor(0, 1);
  lcd.print("to increment");


}


void loop() { 

  // step one revolution  in one direction:
   //Serial.println("forward");
  myStepper.step(stepsPerRevolution);

  // check the status of the switch
  switchState = digitalRead(switchPin);
  Serial.println(switchState);

  // compare the switchState to its previous state
  if (switchState != prevSwitchState) {
    if (switchState == LOW ) {
      hits = hits + 1;
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Hits:");
      lcd.setCursor(0, 1);
      lcd.print(hits);
    }
  }
 
  if(hits == 100){
 myStepper.setSpeed(0);     
  }


  // save the current switch state as the last state
  prevSwitchState = switchState;
}

pert

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. If your browser doesn't show the posting toolbar then you can just manually add the code tags:
[code]// your code is here[/code]
Using code tags and other important information is explained in the How to use this forum post. Please read it.

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you're using the Arduino Web Editor you will not have access to this useful tool but it's still unacceptable to post poorly formatted code. I recommend you to use the standard IDE instead.



This code:
Code: [Select]
  myStepper.step(stepsPerRevolution);
blocks the loop for the time it takes the stepper to execute a full revolution. While it's blocking your switchPin is not getting polled. You could avoid this long blocking delay by only doing one step at a time rather than a full revolution.

Go Up