Programming assignment
Programming assignment
• The given Arduino code will print numbers from 1 to 10 on the serial monitor,
each on a separate line.
• This code will control a motor connected to pins 11 and 10 of the Arduino
based on the distance measured by the HC-SR04 sensor. If the distance is
greater than 10 inches, the motor will rotate in one direction (assumed
forward). If the distance is less than 2 inches, the motor will rotate in the
opposite direction. Otherwise, the motor will stop.
void setup() {
// Set pin modes
pinMode(greenLedPin, OUTPUT); // Green LED pin as output
pinMode(redLedPin, OUTPUT); // Red LED pin as output
pinMode(buttonPin, INPUT); // Button pin as input
// Begin serial communication
Serial.begin(9600);
}
void loop() {
// Read voltage at pin 9
int buttonState = digitalRead(buttonPin);