#include <Wire.h>
#include <Adafruit_MotorShield.h>

Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
Adafruit_DCMotor *motor = AFMS.getMotor(1); // Port M1

void setup() {
  Serial.begin(9600);
  Serial.println("Enter PWM value (0-255):");

  motor->run(FORWARD); 
  motor->setSpeed(0);  
}

void loop() {
  if (Serial.available() > 0) {
    // Read the number from the Serial Monitor
    int pwmValue = Serial.parseInt();

    // Ensure the value is within the 0-255 range
    pwmValue = constrain(pwmValue, 0, 255);

    // Apply the speed
    motor->setSpeed(pwmValue);

    Serial.print("Motor speed set to: ");
    Serial.println(pwmValue);
    
    while(Serial.available() > 0) Serial.read();
  }
}
