Go Down

Topic: Set arduino slave address using dip switch (I2C) (Read 1 time) previous topic - next topic

Dec 16, 2011, 10:19 am Last Edit: Dec 16, 2011, 10:34 am by tsudakazuki Reason: 1
hi,

i have do some program to make the slave read the dip switch, to set the slave address using dip switch.
but nothing happen at slave (it's like the slave not read from dip switch for the address setting).

how to set the arduino slave address using dip switch?
thank you

slave program:-
Code: [Select]


#include <Wire.h>

#define LED_OUT 13
#define ADD 10                                                            // address initial
int ADDRESS;

int a = 0;                                                                      // pin that configure 1 or 0 for hardware setting
int button;

void setup() {
 pinMode(LED_OUT, OUTPUT);
 digitalWrite(LED_OUT,LOW);  
 
 pinMode(a, INPUT);                                                      // read from dip switch, for address hardware setting
 button = digitalRead(a);
 ADDRESS = ADD + button;
 
 Serial.begin(9600);
 Wire.begin(ADDRESS);
 Wire.onReceive(receiveEvent);
}


void loop() {

}

void receiveEvent(int howMany){
 while (Wire.available() > 0){
   boolean b = Wire.receive();
   Serial.print(b, DEC);
   digitalWrite(LED_OUT, b);
 }
 Serial.println();
}


master program:-
Code: [Select]


#include <Wire.h>

#define BUTTON 10
#define ADDRESS 11                                            //address setting for slave,

void setup() {
 Serial.begin(9600);           // start serial for output
 pinMode(BUTTON, INPUT);
 digitalWrite(ADDRESS, HIGH);
 Wire.begin();
}

boolean last_state = HIGH;                                        
void loop() {  
int button_state = digitalRead(BUTTON);                                                      
 if (button_state != last_state){                    
   last_state = button_state;
   Serial.println("Start");
   Wire.beginTransmission(ADDRESS);
   Serial.println("Beginning transmission");
   Wire.send(button_state);
   Serial.println("Sent Data");
   Wire.endTransmission();
   Serial.println("Ended transmission");
 }
}  

Nick Gammon

How have you wired up this switch, exactly?
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

PaulS

Quote
i just use one dip switch only at slave for testing.
pin 0 = for set the add "1" or "0" at slave address.

Pin 0 is one of the serial pins. You also have Serial.begin() in setup(). I know that the Serial.begin() comes after you read the switch, but, it is better if you use some other pin.

Nick Gammon

PaulS is right. Pin 0 has a pull-up on it from the USB chip. Better choose D2 onwards.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

i already change the pin to pin D3,

the situation is :-

1- the dip switch on at first starting:-  the Address=11, ADD(contain 10) will + with 1 bit from dip switch, so the led will on when the push button is press at arduino master, and the led will off when button released at arduino master. But when the dip switch if change to off condition the led still can on/off even the bit value turn to 0 because the dip switch off.

2- the dip switch off at first starting:-  the Address=10,ADD(contain 10) will + with 0 from dip switch, the led will off at first, but cannot turn on even the dip switch change to on

it's mean the arduino master/slave will remember the first memory address, and cannot change even we change the value,

Nick Gammon


so the led will on when the push button is press at arduino master, and the led will off when button released at arduino master. But when the dip switch if change to off condition the led still can on/off even the bit value turn to 0 because the dip switch off.


You've lost me there. You only test the switch once.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

PaulS

Quote
And now, can i change the input bit to binary?

What does this mean? You read a value from a digital pin. You got either 0 or 1. Looks like binary to me.

Code: [Select]
int btn1;          //button 1
What does this mean? Is this a pin number? It doesn't make sense as the value of a pin. Use a more meaningful name.

Code: [Select]
int ADDRESS;
By convention, all capital letters in a name mean that the name was #define'd into existence.

Code: [Select]
//#define ADD 10
  ADDRESS = ADD + btn1;

This isn't going to compile. Don't post code that won't compile. It wastes everyone's time.

Erk

Below is an example of reading a hex switch, this can be duplicated with 4 switches to count up to 16 (0-15 or 0x00 to 0x0F) Probably a better way of doing it but it works.
(switches are pulled to 5v when "off")
Code: [Select]
const byte Mode_Sel_1 = 2;
const byte Mode_Sel_2 = 3;
const byte Mode_Sel_3 = 4;
const byte Mode_Sel_4 = 5;

byte getMODE(){
  boolean _1;
  boolean _2;
  boolean _3;
  boolean _4;
  _1 = digitalRead(Mode_Sel_1);
  _2 = digitalRead(Mode_Sel_2);
  _3 = digitalRead(Mode_Sel_3);
  _4 = digitalRead(Mode_Sel_4);

  if (_1 && _2 && _3 && _4){
    return 0x00;
  }
  else if (!_1 && _2 && _3 && _4){
    return 0x01;
  }
  else if (_1 && !_2 && _3 && _4){
    return 0x02;
  }
  else if (!_1 && !_2 && _3 && _4){
    return 0x03;
  }
  else if (_1 && _2 && !_3 && _4){
    return 0x04;
  }
  else if (!_1 && _2 && !_3 && _4){
    return 0x05;
  }
  else if (_1 && !_2 && !_3 && _4){
    return 0x06;
  }
  else if (!_1 && !_2 && !_3 && _4){
    return 0x07;
  }
  else if (_1 && _2 && _3 && !_4){
    return 0x08;
  }
  else if (!_1 && _2 && _3 && !_4){
    return 0x09;
  }
  else if (_1 && !_2 && _3 && !_4){
    return 0x0A;
  }
  else if (!_1 && !_2 && _3 && !_4){
    return 0x0B;
  }
  else if (_1 && _2 && !_3 && !_4){
    return 0x0C;
  }
  else if (!_1 && _2 && !_3 && !_4){
    return 0x0D;
  }
  else if (_1 && !_2 && !_3 && !_4){
    return 0x0E;
  }
  else if (!_1 && !_2 && !_3 && !_4){
    return 0x0F;
  }
}

Go Up