ok so im building a wiring harness for my car using 1 master arduino which will be under the dash and 2 slaves one in the rear and one in the front of the car. all of the inputs will go to the master arduino and all outputs will be conected to the closest slave ex headlight on the front and tail lights on the back arduino im using logic level fetts to power the outputs the code i have so far im not sure i have been doing this right or if theres an easer way to do it but for the blinkers im not sure how to do pwm to get them to blink in the slave code
master
#include <Wire.h>
//all the inputs on the master arduino
#define HEADLIGHT 2
#define LEFTBLK 3
#define RIGHTBLK 4
#define FAN 5
#define WIPER 6
#define HORN 7
#define FRONT 2 //front arduino
#define REAR 3 //rear arduino
#define BOTH 4 // both arduinos front and rear for blinkers
void setup() {
Serial.begin(112500);
pinMode(HEADLIGHT, INPUT); // all inputs for arduinos
digitalWrite(HEADLIGHT, HIGH);
pinMode(LEFTBLK, INPUT);
digitalWrite(LEFTBLK, HIGH);
pinMode(RIGHTBLK, INPUT);
digitalWrite(RIGHTBLK, HIGH);
pinMode(FAN, INPUT);
digitalWrite(FAN, HIGH);
pinMode(WIPER, INPUT);
digitalWrite(WIPER, HIGH);
pinMode(HORN, INPUT);
digitalWrite(HORN, HIGH);
Wire.begin();
}
boolean last_state = HIGH;
void loop() {
if (digitalRead(HEADLIGHT) != last_state){
last_state = digitalRead(HEADLIGHT);
Serial.println("Start");
Wire.beginTransmission(FRONT);
Wire.write(last_state);
Wire.endTransmission();
if (digitalRead(LEFTBLK) != last_state)
last_state = digitalRead(LEFTBLK);
Serial.println("Start");
Wire.beginTransmission(BOTH);
Serial.println("Beginning transmission");
Wire.write(last_state);
Serial.println("Sent Data");
Wire.endTransmission();
Serial.println("Ended transmission");
if (digitalRead(RIGHTBLK) != last_state)
last_state = digitalRead(RIGHTBLK);
Serial.println("Start");
Wire.beginTransmission(BOTH);
Serial.println("Beginning transmission");
Wire.write(last_state);
Serial.println("Sent Data");
Wire.endTransmission();
Serial.println("Ended transmission");
if (digitalRead(FAN) != last_state)
last_state = digitalRead(FAN);
Serial.println("Start");
Wire.beginTransmission(FRONT);
Serial.println("Beginning transmission");
Wire.write(last_state);
Serial.println("Sent Data");
Wire.endTransmission();
Serial.println("Ended transmission");
}
}
in not sure how to design the slave codes but i have been going off of this sketch which is not mine
#include <Wire.h>
#define HEADLIGHT 9 // headlight connected to pin 9 through fett
#define FRONT // address i think should be for the slave to only read whats coming from the outputs on the master designated to go to the front
void setup() {
pinMode(HEADLIGHT, OUTPUT);
digitalWrite(HEADLIGHT, LOW);
Serial.begin(112500);
Wire.begin(FRONT);
Wire.onReceive(receiveEvent);
}
void loop() {
}
void receiveEvent(int howMany){ // i have no idea what any of this is
while (Wire.available() > 0){
boolean b = Wire.receive();
digitalWrite(HEADLIGHT, !b);
}
}