/* Stepper Motor Control - one revolution This program drives a unipolar or bipolar stepper motor. The motor is attached to digital pins 8 - 11 of the Arduino. The motor should revolve one revolution in one direction, then one revolution in the other direction. Created 11 Mar. 2007 Modified 30 Nov. 2009 by Tom Igoe */ #include #include Servo myservo1; // create servo object to control a servo Servo myservo2; Servo myservo3; Servo myservo4; Servo myservo5; // twelve servo objects can be created on most boards const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution // for your motor // Define pin connections & motor's steps per revolution const int dirPin = 8; const int stepPin = 7; const int Lswitch = 11; const int Lswitch2 = 12; int x = 0; // x steps per loop int led = 13; //Output Opto interruptor status int shade1 = 2; //Connect Opto interruptor sensor pin (#2) int shade3= 3; //Connect Opto interruptor sensor pin (#3) int shade5= 4; //Connect Opto interruptor sensor pin (#5) int shade4 = 5; //Connect Opto interruptor sensor pin (#4) int shade2 = 6; //Connect Opto interruptor sensor pin (#2) int val1; //variable setting int val2; //variable setting int val3; //variable setting int val4; //variable setting int val5; //variable setting int pos = 0; // variable to store the servo position boolean move = true; //moving status = True, stopped status = False boolean swon = false; //Swich On to activate Stepper myStepper(stepsPerRevolution, dirPin, stepPin); void setup() { // set the speed at 500 rpm: myStepper.setSpeed(500); // initialize the serial port: Serial.begin(9600); x = stepsPerRevolution/100; pinMode(Lswitch, INPUT_PULLUP); //pinMode(Lswitch2, INPUT_PULLUP); pinMode(led, OUTPUT); //#13pin as OUTPUT pinMode(shade1, INPUT); //#2pin as OUTPUT pinMode(shade2, INPUT); //#6pin as OUTPUT pinMode(shade3, INPUT); //#3pin as OUTPUT pinMode(shade4, INPUT); //#5pin as OUTPUT pinMode(shade5, INPUT); //#4pin as OUTPUT Serial.begin(115200); myservo1.attach(A3); // attaches the servo on pin A3 to the servo object myservo2.attach(A5); // attaches the servo on pin A5 to the servo object myservo3.attach(A4); // attaches the servo on pin A4 to the servo object myservo4.attach(A2); // attaches the servo on pin A2 to the servo object myservo5.attach(A1); // attaches the servo on pin A1 to the servo object } void loop() { // step one revolution in one direction: // Serial.println("clockwise"); //myStepper.step(stepsPerRevolution*4); //delay(500); if( digitalRead(Lswitch) == LOW) { if (swon) { swon=false; Serial.println("Swich_Off"); }else{ swon =true; Serial.println("Swich_On"); } delay(500); } if (swon) { myStepper.step(x); val1 = digitalRead(shade1) ; //Read #2's status val2 = digitalRead(shade2) ; //Read #6's status val3= digitalRead(shade3) ; //Read #3's status val4 = digitalRead(shade4) ; //Read #'s status val5 = digitalRead(shade5) ; //Read #4's status if ((val1 == HIGH) && (val2 == HIGH) && (val3 == HIGH) && (val4 == HIGH) && (val5 == HIGH)){ //全てのピンが"HIGH"遮蔽がない場合 digitalWrite(led, HIGH); //Output"HIGH" to all pins//LED on } else if ((val1 == LOW) || (val2 == LOW) || (val3 == LOW) || (val4 == LOW) || (val5 == LOW)){ //いずれかのピンが"LOW"遮蔽がある場合 digitalWrite(led, LOW); //#13pin output "LOW"//LED off } if (val1 == LOW){ Serial.println(shade1); move = false; for (pos = 90; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees myservo1.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } delay(5000); //Duration time of Fan blowing a wand for (pos = 0; pos <= 90; pos += 1) { // goes from 0 degrees to 180 degrees // in steps of 1 degree myservo1.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } } if (val2 == LOW){ Serial.println(shade2); move = false; for (pos = 90; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees myservo2.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } delay(5000); //Duration time of Fan blowing a wand for (pos = 0; pos <= 90; pos += 1) { // goes from 0 degrees to 180 degrees // in steps of 1 degree myservo2.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } } if (val3 == LOW){ Serial.println(shade3); move = false; for (pos = 90; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees myservo3.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } delay(5000); //Duration time of Fan blowing a wand for (pos = 0; pos <= 90; pos += 1) { // goes from 0 degrees to 180 degrees // in steps of 1 degree myservo3.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } } if (val4 == LOW){ Serial.println(shade4); move = false; for (pos = 90; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees myservo4.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } delay(5000); //Duration time of Fan blowing a wand for (pos = 0; pos <= 90; pos += 1) { // goes from 0 degrees to 180 degrees // in steps of 1 degree myservo4.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } } if (val5 == LOW){ Serial.println(shade5); swon=false; move = false; for (pos = 90; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees myservo5.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } delay(5000); //Duration time of Fan blowing a wand for (pos = 0; pos <= 90; pos += 1) { // goes from 0 degrees to 180 degrees // in steps of 1 degree myservo5.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } } if (!move){ delay(1000); myStepper.step(x*500); move = true; } } } // step one revolution in the other direction: // Serial.println("counterclockwise"); // myStepper.step(-stepsPerRevolution*4); // delay(500);