Skip to content

Commit ec5e190

Browse files
authored
Merge pull request #8 from sparkfun/v1.3.0
V1.3.0
2 parents d775bfb + 325af6f commit ec5e190

File tree

3 files changed

+69
-69
lines changed

3 files changed

+69
-69
lines changed

Examples/Example6_Change_I2C_Address/Example6_Change_I2C_Address.ino

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
/*
2-
This example code demonstrates how to change the address of the Single or
3-
Quad Qwiic Relay to one of your choosing. There is a set range of available
4-
addresses from 0x07 to 0x78, so make sure your chosen address falls within
5-
this range. The next thing to note is that when you change the address you'll
6-
need to call an instance of the Qwiic_Relay class that includes your new
7-
address: "Qwiic_Relay relay(YOUR_NEW_ADDRESS_HERE);" so that the new address
8-
is fed to all the available functions. Finally if for some reason you've
9-
forgotten your new address. No big deal, load up the I2C scanner example code
10-
and find out where your product's address is at.
11-
By: Elias Santistevan
12-
SparkFun Electronics
13-
Date: July 2019
14-
15-
License: This code is public domain but you buy me a beer if you use
16-
this and we meet someday (Beerware license).
2+
Example 6 Change I2C Address
3+
4+
This example code demonstrates how to change the address of the Single or
5+
Quad Qwiic Relay to one of your choosing. There is a set range of available
6+
addresses from 0x07 to 0x78, so make sure your chosen address falls within
7+
this range. The next thing to note is that when you change the address you'll
8+
need to call an instance of the Qwiic_Relay class that includes your new
9+
address: "Qwiic_Relay relay(YOUR_NEW_ADDRESS_HERE);" so that the new address
10+
is fed to all the available functions. Finally if for some reason you've
11+
forgotten your new address. No big deal, load up the I2C scanner example code
12+
and find out where your product's address is at.
13+
By: Elias Santistevan
14+
SparkFun Electronics
15+
Date: July 2019
16+
17+
License: This code is public domain but you buy me a beer if you use
18+
this and we meet someday (Beerware license).
1719
*/
1820

1921
#include <Wire.h>
@@ -23,33 +25,41 @@
2325
// you want to access the alternate addresses.
2426
#define SINGLE_DEFAULT_ADDR 0x18 // Alternate jumper address 0x19
2527
#define QUAD_DEFAULT_ADDR 0x6D // Alternate jumper address 0x6C
26-
28+
#define NEW_ADDR 0x09
2729

2830
// After changing the address you'll need to apply that address to a new
2931
// instance of the Qwiic_Relay class: "Qwiic_Relay relay(YOUR_NEW_ADDRESS_HERE)".
30-
Qwiic_Relay relay(QUAD_DEFAULT_ADDR); // Change to Single Relay Address if using Quad Relay
32+
Qwiic_Relay relay(QUAD_DEFAULT_ADDR); // Change to Single Relay Address if using single relay
3133

3234
void setup()
3335
{
34-
Wire.begin();
35-
Serial.begin(115200);
36-
37-
// Let's see.....
38-
if(relay.begin())
39-
Serial.println("Ready to flip some switches.");
40-
else
41-
Serial.println("Check connections to Qwiic Relay.");
42-
43-
// There is a not so limited but still limited range of
44-
// addresses that are available to you 0x07 -> 0x78.
45-
if(relay.changeAddress(0x09)) // Put the address you want here.
46-
Serial.println("Address changed successfully.");
47-
else
48-
Serial.println("Address change failed...");
49-
50-
delay(2000);
51-
52-
36+
Wire.begin();
37+
38+
Serial.begin(115200);
39+
40+
if(!relay.begin())
41+
{
42+
Serial.println("Can't communicate with Relay. Check wiring or that you have the correct I2C address.");
43+
while(1)
44+
;
45+
}
46+
47+
Serial.println("Example 6 Changing the relay's I2C address.");
48+
49+
// There is a not so limited but still limited range of
50+
// addresses that are available to you 0x07 -> 0x78.
51+
52+
// If you're using a single relay, indicate it by adding "true" as a second argument.
53+
//if(relay.changeAddress(NEW_ADDR, true)
54+
55+
if(relay.changeAddress(NEW_ADDR)) // Put the address you want here.
56+
Serial.println("Address changed successfully.");
57+
else
58+
Serial.println("Address change failed...");
59+
60+
delay(2000);
61+
62+
5363
}
5464

5565
void loop()

src/SparkFun_Qwiic_Relay.cpp

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
*/
1111

1212
#include "SparkFun_Qwiic_Relay.h"
13+
#include <stdint.h>
14+
#include <Arduino.h>
1315

1416
Qwiic_Relay::Qwiic_Relay(uint8_t address)
1517
{
@@ -66,7 +68,7 @@ uint8_t Qwiic_Relay::getState()
6668
// This function gets the version number of the SparkFun Single Relay.
6769
float Qwiic_Relay::singleRelayVersion()
6870
{
69-
float version = _readVersion(FIRMWARE_VERSION);
71+
float version = _readVersion(SFE_SINGLE_FIRMWARE_VERSION);
7072
return (version);
7173
}
7274

@@ -160,15 +162,24 @@ uint8_t Qwiic_Relay::getState(uint8_t relay)
160162

161163
// This function changes the I-squared-C address of the Qwiic RFID. The address
162164
// is written to the memory location in EEPROM that determines its address.
163-
bool Qwiic_Relay::changeAddress(uint8_t newAddress)
165+
bool Qwiic_Relay::changeAddress(uint8_t newAddress, bool singleRelay)
164166
{
165167

166168
if (newAddress < 0x07 || newAddress > 0x78) // Range of legal addresses
167169
return false;
168170

169-
_i2cPort->beginTransmission(_address);
170-
_i2cPort->write(ADDRESS_LOCATION);
171-
_i2cPort->write(newAddress);
171+
if(singleRelay)
172+
{
173+
_i2cPort->beginTransmission(_address);
174+
_i2cPort->write(SINGLE_CHANGE_ADDRESS);
175+
_i2cPort->write(newAddress);
176+
}
177+
else
178+
{
179+
_i2cPort->beginTransmission(_address);
180+
_i2cPort->write(QUAD_CHANGE_ADDRESS);
181+
_i2cPort->write(newAddress);
182+
}
172183

173184
if (!_i2cPort->endTransmission())
174185
return true;
@@ -183,13 +194,9 @@ bool Qwiic_Relay::_writeAddress(uint8_t addressToWrite, uint8_t value)
183194
_i2cPort->write(addressToWrite); // Toggle it on....
184195
_i2cPort->write(value);
185196
if (_i2cPort->endTransmission() != 0)
186-
{
187197
return false; // Transaction failed
188-
} // End communcation.
189198
else
190-
{
191199
return true;
192-
}
193200
}
194201
// This function handles I-squared-C write commands for turning the relays on.
195202
// The quad relay relies on the current state of the relay to determine whether
@@ -203,9 +210,7 @@ void Qwiic_Relay::_writeCommandOn(uint8_t _command)
203210
{
204211
_status = _readCommand(RELAY_ONE_STATUS);
205212
if (_status == QUAD_RELAY_ON)
206-
{ // Is it on? Then....
207213
return; // Do nothing....
208-
}
209214
else
210215
{ // Off?
211216
_i2cPort->beginTransmission(_address); // Start communication.
@@ -218,9 +223,7 @@ void Qwiic_Relay::_writeCommandOn(uint8_t _command)
218223
{
219224
_status = _readCommand(RELAY_TWO_STATUS);
220225
if (_status == QUAD_RELAY_ON)
221-
{
222226
return;
223-
}
224227
else
225228
{
226229
_i2cPort->beginTransmission(_address);
@@ -233,9 +236,7 @@ void Qwiic_Relay::_writeCommandOn(uint8_t _command)
233236
{
234237
_status = _readCommand(RELAY_THREE_STATUS);
235238
if (_status == QUAD_RELAY_ON)
236-
{
237239
return;
238-
}
239240
else
240241
{
241242
_i2cPort->beginTransmission(_address);
@@ -248,9 +249,7 @@ void Qwiic_Relay::_writeCommandOn(uint8_t _command)
248249
{
249250
_status = _readCommand(RELAY_FOUR_STATUS);
250251
if (_status == QUAD_RELAY_ON)
251-
{
252252
return;
253-
}
254253
else
255254
{
256255
_i2cPort->beginTransmission(_address);
@@ -289,9 +288,7 @@ void Qwiic_Relay::_writeCommandOff(uint8_t _command)
289288
{
290289
_status = _readCommand(RELAY_ONE_STATUS);
291290
if (_status == QUAD_RELAY_OFF)
292-
{ // Is the board off?
293291
return; // Do nothing...
294-
}
295292
else
296293
{ // Then it must be on...
297294
_i2cPort->beginTransmission(_address); // Start communication.
@@ -304,9 +301,7 @@ void Qwiic_Relay::_writeCommandOff(uint8_t _command)
304301
{
305302
_status = _readCommand(RELAY_TWO_STATUS);
306303
if (_status == QUAD_RELAY_OFF)
307-
{
308304
return;
309-
}
310305
else
311306
{
312307
_i2cPort->beginTransmission(_address); // Start communication.
@@ -319,9 +314,7 @@ void Qwiic_Relay::_writeCommandOff(uint8_t _command)
319314
{
320315
_status = _readCommand(RELAY_THREE_STATUS);
321316
if (_status == QUAD_RELAY_OFF)
322-
{
323317
return;
324-
}
325318
else
326319
{
327320
_i2cPort->beginTransmission(_address); // Start communication.
@@ -334,9 +327,7 @@ void Qwiic_Relay::_writeCommandOff(uint8_t _command)
334327
{
335328
_status = _readCommand(RELAY_FOUR_STATUS);
336329
if (_status == QUAD_RELAY_OFF)
337-
{
338330
return;
339-
}
340331
else
341332
{
342333
_i2cPort->beginTransmission(_address); // Start communication.
@@ -362,7 +353,7 @@ uint8_t Qwiic_Relay::_readCommand(uint8_t _command)
362353
_i2cPort->write(_command);
363354
_i2cPort->endTransmission();
364355

365-
_i2cPort->requestFrom(_address, 1);
356+
_i2cPort->requestFrom(_address, (uint8_t)1);
366357
uint8_t status = _i2cPort->read();
367358
return (status);
368359
}
@@ -374,7 +365,7 @@ float Qwiic_Relay::_readVersion(uint8_t _command)
374365
_i2cPort->write(_command);
375366
_i2cPort->endTransmission();
376367

377-
_i2cPort->requestFrom(_address, 2);
368+
_i2cPort->requestFrom(_address, (uint8_t)2);
378369
float _versValue = _i2cPort->read();
379370
_versValue += (float)_i2cPort->read() / 10.0;
380371
return (_versValue);

src/SparkFun_Qwiic_Relay.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#ifndef _SPARKFUN_QWIIC_RELAY_H_
2-
#define _SPARKFUN_QWIIC_RELAY_H_
1+
#pragma once
32

43
#include <Arduino.h>
54
#include <Wire.h>
@@ -46,7 +45,7 @@ enum SF_SINGLE_RELAY_COMMANDS
4645

4746
TURN_RELAY_OFF = 0x00,
4847
TURN_RELAY_ON,
49-
FIRMWARE_VERSION = 0x04,
48+
SFE_SINGLE_FIRMWARE_VERSION = 0x04,
5049
MYSTATUS
5150
};
5251

@@ -67,7 +66,8 @@ enum SF_SINGLE_RELAY_STATUS
6766
#define DUAL_SSR_DEFAULT_ADDRESS 0x0A
6867
#define DUAL_SSR_ALTERNATE_ADDRESS 0x0B
6968

70-
#define ADDRESS_LOCATION 0xC7
69+
#define QUAD_CHANGE_ADDRESS 0xC7
70+
#define SINGLE_CHANGE_ADDRESS 0x03
7171
#define INCORR_PARAM 0xFF
7272

7373
class Qwiic_Relay
@@ -136,7 +136,7 @@ class Qwiic_Relay
136136

137137
// This function changes the I-squared-C address of the Qwiic RFID. The address
138138
// is written to the memory location in EEPROM that determines its address.
139-
bool changeAddress(uint8_t newAddress);
139+
bool changeAddress(uint8_t newAddress, bool singleRelay = false);
140140

141141
private:
142142
uint8_t _address;
@@ -169,4 +169,3 @@ class Qwiic_Relay
169169

170170
TwoWire *_i2cPort;
171171
};
172-
#endif

0 commit comments

Comments
 (0)