// Raspberry Pi Pico + Stepper Motor Example

#define DIR_PIN 2
#define STEP_PIN 3
#define PHOTO_PIN 28
#define POT_PIN 26
#define SW_PIN 5

const int stepsPerRevolution = 200;

void setup() {
  pinMode(STEP_PIN, OUTPUT);
  pinMode(DIR_PIN, OUTPUT);
  pinMode(SW_PIN, INPUT_PULLUP);
  digitalWrite(STEP_PIN, LOW);
  Serial.begin(9600);
}

void loop() {
  // Read the photosensor value
  int photoValue = analogRead(PHOTO_PIN);
  Serial.print("Photosensor Value: ");
  Serial.println(photoValue);

  // Read the potentiometer value (0 to 4095 for 12-bit ADC)
  int potValue = analogRead(POT_PIN);

  // Map the potentiometer value to a range of 0 to 360 degrees
  int angle = map(potValue, 0, 4095, 0, 360);
  Serial.print("Angle: ");
  Serial.println(angle);

  // Calculate the number of steps for the desired angle
  int steps = (angle * stepsPerRevolution) / 360;

  Serial.print("Direction: ");
  // Set direction based on switch
  if (digitalRead(SW_PIN)) {
    digitalWrite(DIR_PIN, LOW);
    Serial.println("left");
  } else {
    digitalWrite(DIR_PIN, HIGH);
    Serial.println("right");
  }
  
  

  // Rotate CW by the calculated steps
  for (int i = 0; i < steps; i++) {
    digitalWrite(STEP_PIN, HIGH);
    delayMicroseconds(1000); // Adjust for speed
    digitalWrite(STEP_PIN, LOW);
    delayMicroseconds(1000);
  }

  delay(500); // Wait half a second before reading again
}
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT
A4988