Coding Midterm
Coding Midterm
Wiring:
Arduino 1 (Master):
Arduino 2 (Slave):
cpp
Copy code
#include <Wire.h>
void setup() {
pinMode(switchPin, INPUT_PULLUP); // Set switch pin as input
with internal pull-up resistor
Wire.begin(); // join I2C bus as master
Serial.begin(9600);
}
void loop() {
int switchState = digitalRead(switchPin); // Read switch
state
int potValue = analogRead(potPin); // Read potentiometer
value
delay(500);
}
Slave (Arduino 2):
cpp
Copy code
#include <Wire.h>
#include <Servo.h>
int switchState = 0;
int potValue = 0;
void setup() {
pinMode(ledPin, OUTPUT);
myServo.attach(servoPin);
Wire.begin(8); // join I2C bus with address #8
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600);
}
void loop() {
// Control LED based on switch state
if (switchState == HIGH) {
digitalWrite(ledPin, LOW); // LED off
} else {
digitalWrite(ledPin, HIGH); // LED on
}
delay(100);
}