Avoids delay() to allow other tasks to run simultaneously.
const int ledPin = 13; int ledState = LOW; unsigned long previousMillis = 0; const long interval = 1000; // 1 second void setup() pinMode(ledPin, OUTPUT);
Components: 10kΩ potentiometer. Connect middle pin to A0, outer pins to 5V and GND.
void loop() for (pos = 0; pos <= 180; pos++) myservo.write(pos); delay(15);
3.1 Pin Mode and Digital I/O Pins must be configured as INPUT or OUTPUT using pinMode() .
| Library | Purpose | |---------|---------| | LiquidCrystal.h | Control LCD displays (16x2, 20x4) | | Servo.h | Control up to 12 servos | | Stepper.h | Control stepper motors | | DHT.h | Read temperature/humidity sensors | | SPI.h / Wire.h | SPI and I2C communication | 6. Debugging and Serial Communication The Serial Monitor (Tools → Serial Monitor) is essential for debugging.
void loop() unsigned long currentMillis = millis();
const int buttonPin = 7; const int ledPin = 13; int buttonState = 0; void setup() pinMode(buttonPin, INPUT); pinMode(ledPin, OUTPUT); Serial.begin(9600); // Initialize serial monitor
#include <Servo.h> Servo myservo; int pos = 0;
if (currentMillis - previousMillis >= interval) previousMillis = currentMillis; ledState = !ledState; digitalWrite(ledPin, ledState);
if (buttonState == HIGH) digitalWrite(ledPin, HIGH); Serial.println("Button pressed"); else digitalWrite(ledPin, LOW);