Label applicator with bipolar stepper motor, sensor TCST2103 and UNO

Hi All
I’m still a newbie with arduino, and so far succeeding, but with the following code I have picked up a problem. It all works as supposed to Except that when I switch on the whole operation the push button does not jog to feed the label roll in the m/c, ONLY after I pass something through the sensor does it all work. I know its something to how the code is written. Can I get help please. Any suggestions or bad mouthing bad coding is also fine.
Thanks
John

[code]
//*
//    Label Dispencing Driver -  Using Arduino UNO to Program.
//  Control (L298N Driver) of a 4 wire Bipolar stepper motor with optical-sensor (TCST2103),
//  reading labels on a reel (28mm x 32mm) with a spacing of 3mm  between labels. A led confirms
//  sensor status and (JOG/INCH) delay. A pushbutton is used to (JOG/INCH) the motor with a 2 sec
//  delay between each push of the button, to feed the labels in the machine till, the sensor can
//  take over, thus starting the procedure of the labels being applied to the product.
//  A 5K Potentiometer is added to adjust the speed of the motor.
//*

#include <Stepper.h>

const int buttonPin = 2;          // pushbutton (pin 2 arduino - pin 4 ATmega 328P)
const int led = 13;               // led indicator (pin 13 arduino - pin 19 ATmega 328)
const int sensor = 3;             // sensor (TCST2103)-(pin 3 arduino - pin 5 ATmega 328)
const int stepsPerRevolution = 200;     // 1.8 degree step motor
int state = LOW;                  // default, no motion detected
int val = 0;                      // variable to store the sensor status (value)

// initialize the stepper library on pins 9 through 12.(L298N Driver)
Stepper myStepper(stepsPerRevolution, 9, 10, 11, 12);  //(pin 9-12 arduino - pin 15-18 ATmega 328)

int stepCount = 0;              // number of steps the motor has taken
int buttonState = 0;            // variable for reading the pushbutton status

void setup() {
  pinMode(led, OUTPUT);         // initialize LED as an output
  pinMode(sensor, INPUT);       // initialize sensor as an input
  pinMode(buttonPin, INPUT);    // initialize pushbutton pin as an input:
}

void loop() {

  val = digitalRead(sensor);           // read sensor value
  if (val == LOW)                      // sensor value low
    buttonState = digitalRead(buttonPin);  // read button value
  if (buttonState == HIGH)    {        // button state high
    //val = digitalRead(sensor);         // read sensor value
    //if (val == LOW)                    // sensor value low
    digitalWrite(led, HIGH);           // turn led on
    myStepper.step(200);               // motor run
    delay (2000);                      // delay 2sec between each push
  }

  {
    val = digitalRead(sensor);           // read sensor value
    if (val == LOW)                      // sensor value low
      buttonState = digitalRead(buttonPin);  // read button value
    if (buttonState == LOW)              // button state low
      digitalWrite(led, LOW);            // turn led off
    myStepper.step(0);                   // stop motor
    val = digitalRead(sensor);           // read sensor value
    if (val == LOW)                      // sensor value low
      digitalWrite(led, LOW);            // turn led off
    val = digitalRead(sensor);           // read sensor value
    if (val == HIGH)   {                 // sensor value high
      digitalWrite(led, HIGH);           // turn led on

      int sensorReading = analogRead(A0);   // pot to adjust motor speed (0v-AO-5v)
      // map it to a range from 100 to 255:
      int motorSpeed = map(sensorReading, 0, 1023, 100, 255);
      if (motorSpeed > 100)
        myStepper.setSpeed(motorSpeed);     // set the motor speed:
      // step 1/200 of a revolution:
      myStepper.step(stepsPerRevolution / 200); // step 1/200 of a revolution:

    }
  }
}

[/code]

Hi,
I don't see any code to activate pull_up resistors on your button and you look for button state HIGH.
So you must be switching to 5V, do you have a PULL_DOWN resistor fitted to pull the input pin down when the button is OFF?

Tom... :slight_smile:

Hi and thanks,
I’ll put my project board to show. I made it this way so I could test each section one at a time and then connect all up and try it. Like I said- when I power up and push tact switch motor won’t turn, if I pass something through the sensor the motor turns and after this when I press the tact the motor will turn every time I press the tact for the 200 steps as set in code. Does it make sense my explanation ?

Label application driver with Bipolar Stepper.pdf (137 KB)

Hi,
Thanks for PCB pattern, but can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

The schematic will be easier to read.
Good to see you are doing it in stages.

Thanks.. Tom.. :slight_smile:

Hi There
Thanks again I'll post a simple basic circuit of the Part I think you want to see, its RIMU hope you can see it - load it. Nearly broke my arm doin it. :slight_smile:

John

Hi Here’s circuit
john

Label Applicator Circuit.pdf (144 KB)

Hi,
You should be able to export an image in jpg.

Tom... :slight_smile:

Hi,
Have you put bypass caps around the regulator that they recommend in the data sheet?

Tom... :slight_smile:

Hi Tom
If U can please tell me how to export to jpg I'll be grateful I'm using win 7 ultimate
john

Hi
Yes there's two 104 ceramic caps over the regulator and one 104 over the 328 power pins plus the power supply is well regulated.
john