Ganti LED dengan MOTOR DC, buatlah Motor DC bergerak searah jarum jam denagn kecepatan sebanding dengan potensiometer
//MASTER
#include <Wire.h>
#define MASTER_ADDR 9
int analogPin = 0; int val = 0;
void setup() {
Wire.begin();
}
void loop() {
delay(50);
val = map (analogRead(analogPin), 0, 1023, 0,255);
Wire.beginTransmission(MASTER_ADDR);
Wire.write(val);
Wire.endTransmission();
}
- SLAVE
//SLAVE
#include <Wire.h>
#define SLAVE_ADDR 9
int motor = 13;
int rd;
int br;
void setup() {
pinMode(motor, OUTPUT);
Wire.begin(SLAVE_ADDR);
Wire.onReceive(receiveEvent);
}
void receiveEvent(){
rd = Wire.read();
}
void loop() {
delay(50);
br = map(rd, 0, 255, 1000, 2000);
if (br == 2000){
delay(1);
digitalWrite(motor, HIGH);
}else if (br == 1000){
delay(1);
digitalWrite(motor, LOW);
}
}
No comments:
Post a Comment