Complete the code to set the motor speed using PWM on pin 9.
int motorPin = 9; void setup() { pinMode(motorPin, OUTPUT); } void loop() { analogWrite(motorPin, [1]); delay(1000); }
The analogWrite function sets the PWM duty cycle. 255 means full speed.
Complete the code to gradually increase motor speed from 0 to 255.
int motorPin = 6; void setup() { pinMode(motorPin, OUTPUT); } void loop() { for (int speed = 0; speed <= 255; speed++) { analogWrite(motorPin, [1]); delay(10); } }
The variable speed changes from 0 to 255 to increase PWM duty cycle gradually.
Fix the error in the code to set motor speed using PWM on pin 3.
int motorPin = 3; void setup() { pinMode(motorPin, OUTPUT); } void loop() { analogWrite([1], 128); delay(500); }
The first argument of analogWrite must be the pin number variable motorPin.
Fill both blanks to create a dictionary that maps motor pins to their PWM speeds if speed is above 100.
int motorPins[] = {5, 6, 9};
int speeds[] = {90, 150, 200};
void setup() {
for (int i = 0; i < 3; i++) {
pinMode(motorPins[i], OUTPUT);
if (speeds[i] [1] 100) {
analogWrite(motorPins[i], [2]);
}
}
}
void loop() {}The condition checks if speed is greater than 100, then writes PWM speed from speeds[i] to the motor pin.
Fill all three blanks to read an analog sensor, map its value to PWM range, and set motor speed.
int sensorPin = A0; int motorPin = 10; void setup() { pinMode(motorPin, OUTPUT); } void loop() { int sensorValue = analogRead([1]); int motorSpeed = map(sensorValue, 0, 1023, [2], [3]); analogWrite(motorPin, motorSpeed); delay(100); }
The sensor is read from sensorPin. The value is mapped from 0-1023 to 0-255 for PWM.
