Questions about Arduino Loop Control

Asked 2 years ago, Updated 2 years ago, 52 views

If the illumination value is greater than or equal to a certain illumination value, rotate 1440 degrees counterclockwise I'm working on a program that rotates 1440 degrees clockwise if it's small If you write the code like this, If the illumination value is above a certain level of illumination, and the illumination value is above a certain level of illumination, it rotates and rotates at 2880 degrees counterclockwise My intention is to write a program that rotates 1440 degrees counterclockwise, stays still until the illuminance value decreases, waits until the illuminance value decreases, and rotates clockwise, how do I modify the code?

if문

2022-09-20 19:31

1 Answers

Please refer to the code below.

#include <Stepper.h>

#define LIGHT A0

constint steps_rotation=2048; // one lap
const int rotation_number=4;
int light=0;
bool clock_mode=true;
Stepper stepper(steps_rotation, 8, 10, 9, 11);

void setup() {
  stepper.setSpeed(5); // rpm
  Serial.begin(9600);
}

void loop() {
  light=analogRead(LIGHT);
  if(clock_mode && light>600)
  {
    stepper.step(steps_rotation*rotation_number);
    clock_mode=false;
    Serial.println(light);
  }
  else if(!clock_mode && light<500)
  {
    stepper.step(-steps_rotation*rotation_number);
    clock_mode=true;
    Serial.println(light);
  }
}


2022-09-20 19:31

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.