Unipolar stepper motor

Hello, I am trying to follow this tutorial from offical arduino website.
http://www.arduino.cc/en/Tutorial/StepperUnipolar
It seems that i made all the wiring connections right as my stepper rotates according to the sample example code(Example #1).
I incorporated only one difference to this sample example, that is using an optical beamsensor.
This is the sketch I came up with to incorporate the beamsensor.

int motorPin1 = 8;
int motorPin2 = 9;
int motorPin3 = 10;
int motorPin4 = 11;
int delayTime = 500;
int beamSensor = 2;
int ledPin = 13;
int val = 0;

void setup() {
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);
  pinMode(beamSensor, OUTPUT);
}

void loop() {
  val = analogRead(beamSensor);
  if (val >  171) {
  digitalWrite(motorPin1, HIGH);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, LOW);
  delay(delayTime);
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, HIGH);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, LOW);
  delay(delayTime);
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, HIGH);
  digitalWrite(motorPin4, LOW);
  delay(delayTime);
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, HIGH);
  delay(delayTime);
  }
}

When I upload this sketch to my arduino and test it, the result is opposite to my expectation.
The stepper motor keeps on turning when the beam is not disturbed.
But, stepper stops to turn when the beam is disturbed.
It’s totally opposite to my intension. ;D
But, it’s also interesting to have such result.
I tried to modify the codes in the “if” statement, but the result is the same whether I change “>” into “<” or change the value.
Could someone please help me to solve this?

In your setup you list

pinMode(beamSensor, OUTPUT;)

Remove that line.

When the Arduino receives input it's well.... input ;). However the reason you can just omit that line is when using the analog input pins for analog input there is no need to make any calls in void setup.

See this link for an example of analog input. http://arduino.cc/en/Tutorial/AnalogInput

Thank you for helping me. It works.
Now, the stepper is moving only when the beam is disturbed.
To control the speed and the steps of my stepper motor, I’ve came up with this sketch.

#include <Stepper.h>
#define motorPin1 8
#define motorPin2 9
#define motorPin3 10
#define motorPin4 11
#define motorSteps 200
#define ledPin 13  
Stepper myStepper(motorSteps, motorPin1, motorPin2, motorPin3, motorPin4); 

int beamSensor = 2;
int val = 0;

void setup() {
 
myStepper.setSpeed(60);
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
blink(3);
}

                                           
void loop() {
  val = analogRead(beamSensor);
  if (val < 100) 
  
  Serial.println("Forward");

  myStepper.step(100);
  delay(500); 
  }  
  // Blink the reset LED:
void blink(int howManyTimes) {
  int i;
  for (i=0; i< howManyTimes; i++) {
    digitalWrite(ledPin, HIGH);
    delay(200);
    digitalWrite(ledPin, LOW);
    delay(200);
  }

}

When I run this code, the stepper turns regardless of the beamsensor just like before.
When the beam is disturbed, however, I do receive serial readings from serial output.
But, the reading is nothing but some kind of ? or x.
Anyway, I get this whenever the beam is disturbed.
Could you advise me which part of the sketch has to be modified to make my stepper motor to turn only when the beam is disturbed?
Thank you.

Hi mingki, You need to have curly brackets around the code following the conditional test. Without them only the first line gets executed when the condition is true, the other lines are always run. In the following loop, code within the inner brackets gets executed only when the val is < 100

void loop() {
  val = analogRead(beamSensor);
  if (val < 100){ // need curly brackets after the conditional test
    Serial.println("Forward");
    myStepper.step(100);
    delay(500);
  }// close bracket here
}

Thank you so much for helping me, mem. I was missing the most basic sketch element ;D Thank you, mem. Have a great day~!