- const int DIR = 8;
- const int STEP = 9;
- void setup() {
- pinMode(DIR, OUTPUT);
- pinMode(STEP, OUTPUT);
- digitalWrite(DIR, LOW);
- digitalWrite(STEP, LOW);
-
- // put your setup code here, to run once:
- Serial.begin(9600);
- }
- void loop() {
- // put your main code here, to run repeatedly:
- int val=0;
- val=analogRead(1);
- Serial.println(val);
- delay(500);
- if(val<200){
- //もしvalが200より小さかったら、モーターは回らない
- for (int i=0; i <= 50; i++){
- clockwise(0);
- }
- for (int i=0; i <= 50; i++){
- counter_clockwise(0);
- }
- }
-
- else{
- //そうじゃなかったらモーターは回る
- for (int i=0; i <= 50; i++){
- clockwise(5000);
- }
- for (int i=0; i <= 50; i++){
- counter_clockwise(5000);
- }
- }
- }
- void clockwise(int delaytime){
- digitalWrite(DIR, HIGH);//HIGHは時計回り
- digitalWrite(STEP, HIGH);
- delayMicroseconds(delaytime);
- digitalWrite(STEP, LOW);
- delayMicroseconds(delaytime);
- }
- void counter_clockwise(int delaytime){
- digitalWrite(DIR, LOW);//はLOWは反時計回り
- digitalWrite(STEP, HIGH);
- delayMicroseconds(delaytime);
- digitalWrite(STEP, LOW);
- delayMicroseconds(delaytime);
- }
暗くしたら周る。