Pages: [1]   Go Down
Author Topic: need help with 1 master arduino and 2 slave for car wiring harness  (Read 1312 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
  }
}

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 642
Posts: 50365
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Each slave registers using an address. The master talks to one slave at a time.
Code:
   Wire.beginTransmission(BOTH);
won't work unless some slave registers with address 4, which would imply a 3rd slave.

Code:
   Wire.write(last_state);
    Wire.write(last_state);
    Wire.write(last_state);
    Wire.write(last_state);
Exactly how is the slave supposed to figure out what to do if all you send it is 0 or 1? Turn headlights on or off with 0 or 1. Turn left blinker on or off with 0 or 1. Turn right blinker on or off with 0 or 1. Turn the fan on or off with 0 or 1.

God help the poor slaves that turn the headlight off when the fan should have been. I can just imagine what will happen when the turn signals are supposed to start blinking. The headlight will be going on and off at the same rate as the blinker on the left, which will be keeping time with the one on the right, which will be in sync with the fan.

You're not in the US, I hope.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 642
Posts: 50365
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
lets say i have a buttons connected to pins 2,3,4,5,6,7,8,9,10,11,12,13 on the master
A is singular. Buttons is plural. Which is it?

Buttons go on shirts. Switches go on Arduinos.

Quote
pin 8,11,12,13 will light up when the corresponding button on the master is pressed and held down
Not automatically. You need to send something when the switch on pin N is pressed. What is that something going to be?

Quote
pin 9,10 will blink when the corresponding button on the master is pressed and held down
Again, not automatically. Blinking pins is even harder, because blinking requires turning a pin on for a while, then turning the pin off for a while, then repeating. You can't use delay() for this.

Quote
im not to sure on how to put it into a sketch i can connect an led and button and get them to work on a arduino but i cant figure uot how to get the master to tell the slave the button has been pushed to turn on led
You need to define a set of commands that the master sends, and implement them on the slave. Send something like "<S,10,0>" to have the slave set pin 10 to 0 (LOW). I'm sure that you can figure out what to send to turn a pin on. Then, all you need to do is define what the master should send so the slave knows to start blinking and to stop blinking a pin.
Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 46
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi,

i dono how far i can help u....... but i have a simple serial communication programs between two arduino's. i hope it will help u. u can easily modify for ur use.

Master sketch
Code:
int i;
char reader[10];

void setup()
{
pinMode(13,OUTPUT);
Serial.begin(9600);
Serial.print("2OFF");

}

void loop()
{
 
  if(Serial.available()>1){
  for(i=0;i<10;i++){
    reader[i]=Serial.read();
    delay(2);
  }
}

Serial.print("2ON");
delay(500);
Serial.print("2OFF");
delay(5000);

for(i=0;i<10;i++)
reader[i]=0;
}


Slave sketch

Code:

int i;
char reader[10];

void setup()
{
pinMode(13,OUTPUT);
Serial.begin(9600);
}

void loop()
{

  if(Serial.available()>1)
  {
    for(i=0;i<10;i++)
    {
      reader[i]=Serial.read();
      delay(2);
    }
  }

if(reader[0]=='2'&&reader[1]=='O'&&reader[2]=='N')
digitalWrite(13,HIGH);


else if(reader[0]=='2'&&reader[1]=='O'&&reader[2]=='F'&&reader[3]=='F')
digitalWrite(13,LOW);



for(i=0;i<10;i++)
reader[i]=0;
}

for more detail see this video 

Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 129
Posts: 8601
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Minor detail

Code:
#define MASTER_PIN 9      // Pin 9 on local
#define MASTER_PIN 10     // Pin 10 on local
#define MASTER_PIN 11     // Pin 11 on local
...

You can't define the same thing (MASTER_PIN) to 50 different values, same with some other defines.

______
Rob

Logged

Rob Gray aka the GRAYnomad www.robgray.com

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 642
Posts: 50365
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
 Wire.beginTransmission(1); // transmit to device #2
  Wire.beginTransmission(2); // transmit to device #2
Make up your mind which value talks to device #2. Talking to device #1 at some point might be useful.

Code:
wiper = digitalRead(WIPERS_PIN);
if ( wiper == HIGH) {      
frontWrite(WIPER_PIN,HIGH);}
else {
frontWrite(WIPER_PIN,LOW);}

As opposed to:
Code:
wiper = digitalRead(WIPERS_PIN);
frontWrite(WIPER_PIN,wiper);
?

You use the same overly complicated code in may other places, too.
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 129
Posts: 8601
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
i did that to help my know what pins im using
OK, a little strange when comments will do that, but whatever.

So what's the problem again?

Are you using I2C to talk around a vehicle? That's not a recipe for a reliable system.

And please use code tags (the # button at the top of the editing area) to display any programs.

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

well i got it all figured out but what do you mean its not a receipt for a reliable system
for master i have
Quote
#include <Wire.h>

#define LIGHTS_PIN 2      // Pin 2 on local
#define FANS_PIN 3      // Pin 3 on local
#define HORNS_PIN 4      // Pin 4 on local
#define WIPERS_PIN 5      // Pin 5 on local
#define WASHERS_PIN 6      // Pin 6 on local
#define FOGS_PIN 7      // Pin 7 on local
#define BRAKELIGHTS_PIN 8      // Pin 8 on local
#define LEFTBLKS_PIN 9      // Pin 9 on local
#define RIGHTBLKS_PIN 10     // Pin 10 on local
#define HAZARDS_PIN 11     // Pin 11 on local
 
#define HEADLIGHT_PIN 102     // Pin 2 on remote
#define FAN_PIN 103     // Pin 3 on remote
#define HORN_PIN 104     // Pin 4 on remote
#define WIPER_PIN 105     // Pin 5 on remote
#define WASHER_PIN 106     // Pin 6 on remote
#define FOG_PIN 107     // Pin 7 on remote
#define RIGHTBLK_PIN 108     // Pin 8 on remote


#define TAILIGHT_PIN 112     // Pin 2 on remote
#define BRAKELIGHT_PIN 113     // Pin 3 on remote
#define LICENESLAMP_PIN 114     // Pin 4 on remote
#define RRIGHTBLK_PIN 115     // Pin 5 on remote
#define RLEFTBLK_PIN 116     // Pin 6 on remote




int light = 0;         // variable for reading the pushbutton status
int fan = 0;
int horn = 0;
int wiper = 0;
int washer = 0;
int fog = 0;
int brakelight = 0;
int rightblk = 0;
int leftblk = 0;
int hazard= 0;

void setup()
{

  Wire.begin(); // join i2c bus
}


void loop()
{

  light = digitalRead(LIGHTS_PIN);
  if (light == HIGH) {       
    frontWrite(HEADLIGHT_PIN,HIGH);
    backWrite(TAILIGHT_PIN,HIGH);
    backWrite(LICENESLAMP_PIN,HIGH);
  }
  else {
    frontWrite(HEADLIGHT_PIN,LOW);
    backWrite(TAILIGHT_PIN,LOW);
    backWrite(LICENESLAMP_PIN,LOW);
  }


  hazard = digitalRead(HAZARDS_PIN);
  if (hazard == HIGH) {       
    frontWrite(RIGHTBLK_PIN ,HIGH);
    frontWrite(LEFTBLK_PIN ,HIGH);
    backWrite(RLEFTBLK_PIN,HIGH);
    backWrite(RRIGHTBLK_PIN,HIGH);
  }
  else {
    frontWrite(RIGHTBLK_PIN,LOW);
    frontWrite(LEFTBLK_PIN ,HIGH);
    backWrite(RLEFTBLK_PIN,HIGH);
    backWrite(RRIGHTBLK_PIN,LOW);
  }



  rightblk = digitalRead(RIGHTBLKS_PIN);
  if (rightblk == HIGH) {       
    frontWrite(RIGHTBLK_PIN ,HIGH);
    backWrite(RRIGHTBLK_PIN,HIGH);
  }
  else {
    frontWrite(RIGHTBLK_PIN,LOW);
    backWrite(RRIGHTBLK_PIN,LOW);
  }



  leftblk = digitalRead(LEFTBLKS_PIN);
  if (leftblk == HIGH) {       
    frontWrite(LEFTBLK_PIN ,HIGH);
    backWrite(RLEFTBLK_PIN,HIGH);
  }
  else {
    frontWrite(LEFTBLK_PIN,LOW);
    backWrite(RLEFTBLK_PIN,LOW);
  }






  fan = digitalRead(FANS_PIN);
  if ( fan == HIGH) {       
    frontWrite(FAN_PIN,HIGH);
  }
  else {
    frontWrite(FAN_PIN,LOW);
  }


  horn = digitalRead(HORNS_PIN);
  if ( horn == HIGH) {       
    frontWrite(HORN_PIN,HIGH);
  }
  else {
    frontWrite(HORN_PIN,LOW);
  }


  wiper = digitalRead(WIPERS_PIN);
  if ( wiper == HIGH) {       
    frontWrite(WIPER_PIN,HIGH);
  }
  else {
    frontWrite(WIPER_PIN,LOW);
  }


  washer = digitalRead(WASHERS_PIN);
  if ( washer == HIGH) {       
    frontWrite(WASHER_PIN,HIGH);
  }
  else {
    frontWrite(WASHER_PIN,LOW);
  }


  fog = digitalRead(FOGS_PIN);
  if ( fog == HIGH) {       
    frontWrite(FOG_PIN,HIGH);
  }
  else {
    frontWrite(FOG_PIN,LOW);
  }


  brakelight = digitalRead(BRAKELIGHTS_PIN);
  if ( brakelight == HIGH) {       
    backWrite(BRAKELIGHT_PIN,HIGH);
  }
  else {
    backWrite(BRAKELIGHT_PIN,LOW);
  }


}
void frontWrite(int pin, int value)
{
  pin = pin-100;  // substracts 100 so it maps to the real ports on the expansion arduino
  Wire.beginTransmission(1); // transmit to device #1
  Wire.write(pin);              // sends one byte stating the pin to be addressed
  Wire.write(value);          // sends the value to be transmitted to the pin selected
  Wire.endTransmission();    // stop transmitting
}

void backWrite(int pin, int value)
{
  pin = pin-110;  // substracts 100 so it maps to the real ports on the expansion arduino
  Wire.beginTransmission(2); // transmit to device #2
  Wire.write(pin);              // sends one byte stating the pin to be addressed
  Wire.write(value);          // sends the value to be transmitted to the pin selected
  Wire.endTransmission();    // stop transmitting
}






and for the front slave



Quote
#include <Wire.h>


void setup()
{
  Wire.begin(1);                // join i2c bus with address #1
  Wire.onReceive(receiveEvent); // register event
}


void loop()
{
    // Whatever. maybe nothing.
}



void receiveEvent(int howMany)
{
  int port = Wire.read();    // receive byte as an integer
  int value = Wire.read();   // receives the byte with the value
  digitalWrite(port,value);         // sets the pin to the desired value
}


its the same for the second but i changed the address


so what would be a more reliable way of doing this 
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 642
Posts: 50365
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  light = digitalRead(LIGHTS_PIN);
  if (light == HIGH) {       
    frontWrite(HEADLIGHT_PIN,HIGH);
    backWrite(TAILIGHT_PIN,HIGH);
    backWrite(LICENESLAMP_PIN,HIGH);
  }
  else {
    frontWrite(HEADLIGHT_PIN,LOW);
    backWrite(TAILIGHT_PIN,LOW);
    backWrite(LICENESLAMP_PIN,LOW);
  }
In all cases, you are calling frontWrite() and backwrite() with the same state as what you read from the pin. This code could be replaced by:
Code:
  light = digitalRead(LIGHTS_PIN);
  frontWrite(HEADLIGHT_PIN, light);
  backWrite(TAILIGHT_PIN, light);
  backWrite(LICENESLAMP_PIN, light);
resulting in a lot less code to maintain.

I2C is meant to be used over very short distances - between two chips on the same bus that are located close to each other. It is not intended to be used from the front of a vehicle to the back, unless that vehicle is a MatchBox car.

Cars are electrically very noisy/dirty environments. Running long wires and trying to exchange critical data along those wires is not a good idea. There will be a large possibility for interference. There are other solutions, like RS485, that are better choices, although they are a bit more costly/complex.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

what all would i need to do to use rs485
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 129
Posts: 8601
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Some RS-485 transceivers, a spare digital IO pin (as well as Tx and Rx, only Tx if the comms is one-way) and modified code.

You could also look at LIN and CAN, both are designed for vehicle use, but 485 is more common I think and will do as well.

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

how exactly would i modify the code
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 129
Posts: 8601
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Use serial or softserial to send the data rather than wire.

Is your comms one way? If you could hang both slaves off the same wire and have them determine if the command is for based on a value in the data you send.

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Pages: [1]   Go Up
Jump to: