Examples: mass code format. See example_formatter.conf
This commit is contained in:
parent
c13cf02651
commit
b5a130afb5
|
@ -10,8 +10,7 @@
|
||||||
|
|
||||||
#include <EEPROM.h>
|
#include <EEPROM.h>
|
||||||
|
|
||||||
void setup()
|
void setup() {
|
||||||
{
|
|
||||||
|
|
||||||
/***
|
/***
|
||||||
Iterate through each byte of the EEPROM storage.
|
Iterate through each byte of the EEPROM storage.
|
||||||
|
@ -25,11 +24,14 @@ void setup()
|
||||||
This will make your code portable to all AVR processors.
|
This will make your code portable to all AVR processors.
|
||||||
***/
|
***/
|
||||||
|
|
||||||
for ( int i = 0 ; i < EEPROM.length() ; i++ )
|
for (int i = 0 ; i < EEPROM.length() ; i++) {
|
||||||
EEPROM.write(i, 0);
|
EEPROM.write(i, 0);
|
||||||
|
}
|
||||||
|
|
||||||
// turn the LED on when we're done
|
// turn the LED on when we're done
|
||||||
digitalWrite(13, HIGH);
|
digitalWrite(13, HIGH);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop(){ /** Empty loop. **/ }
|
void loop() {
|
||||||
|
/** Empty loop. **/
|
||||||
|
}
|
||||||
|
|
|
@ -10,7 +10,7 @@
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <EEPROM.h>
|
#include <EEPROM.h>
|
||||||
|
|
||||||
void setup(){
|
void setup() {
|
||||||
|
|
||||||
//Start serial
|
//Start serial
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
|
@ -19,18 +19,20 @@ void setup(){
|
||||||
}
|
}
|
||||||
|
|
||||||
//Print length of data to run CRC on.
|
//Print length of data to run CRC on.
|
||||||
Serial.print( "EEPROM length: " );
|
Serial.print("EEPROM length: ");
|
||||||
Serial.println( EEPROM.length() );
|
Serial.println(EEPROM.length());
|
||||||
|
|
||||||
//Print the result of calling eeprom_crc()
|
//Print the result of calling eeprom_crc()
|
||||||
Serial.print( "CRC32 of EEPROM data: 0x" );
|
Serial.print("CRC32 of EEPROM data: 0x");
|
||||||
Serial.println( eeprom_crc(), HEX );
|
Serial.println(eeprom_crc(), HEX);
|
||||||
Serial.print( "\n\nDone!" );
|
Serial.print("\n\nDone!");
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop(){ /* Empty loop */ }
|
void loop() {
|
||||||
|
/* Empty loop */
|
||||||
|
}
|
||||||
|
|
||||||
unsigned long eeprom_crc( void ){
|
unsigned long eeprom_crc(void) {
|
||||||
|
|
||||||
const unsigned long crc_table[16] = {
|
const unsigned long crc_table[16] = {
|
||||||
0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
|
0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
|
||||||
|
@ -41,9 +43,9 @@ unsigned long eeprom_crc( void ){
|
||||||
|
|
||||||
unsigned long crc = ~0L;
|
unsigned long crc = ~0L;
|
||||||
|
|
||||||
for( int index = 0 ; index < EEPROM.length() ; ++index ){
|
for (int index = 0 ; index < EEPROM.length() ; ++index) {
|
||||||
crc = crc_table[( crc ^ EEPROM[index] ) & 0x0f] ^ (crc >> 4);
|
crc = crc_table[(crc ^ EEPROM[index]) & 0x0f] ^ (crc >> 4);
|
||||||
crc = crc_table[( crc ^ ( EEPROM[index] >> 4 )) & 0x0f] ^ (crc >> 4);
|
crc = crc_table[(crc ^ (EEPROM[index] >> 4)) & 0x0f] ^ (crc >> 4);
|
||||||
crc = ~crc;
|
crc = ~crc;
|
||||||
}
|
}
|
||||||
return crc;
|
return crc;
|
||||||
|
|
|
@ -17,20 +17,20 @@
|
||||||
|
|
||||||
#include <EEPROM.h>
|
#include <EEPROM.h>
|
||||||
|
|
||||||
void setup(){
|
void setup() {
|
||||||
|
|
||||||
float f = 0.00f; //Variable to store data read from EEPROM.
|
float f = 0.00f; //Variable to store data read from EEPROM.
|
||||||
int eeAddress = 0; //EEPROM address to start reading from
|
int eeAddress = 0; //EEPROM address to start reading from
|
||||||
|
|
||||||
Serial.begin( 9600 );
|
Serial.begin(9600);
|
||||||
while (!Serial) {
|
while (!Serial) {
|
||||||
; // wait for serial port to connect. Needed for Leonardo only
|
; // wait for serial port to connect. Needed for Leonardo only
|
||||||
}
|
}
|
||||||
Serial.print( "Read float from EEPROM: " );
|
Serial.print("Read float from EEPROM: ");
|
||||||
|
|
||||||
//Get the float data from the EEPROM at position 'eeAddress'
|
//Get the float data from the EEPROM at position 'eeAddress'
|
||||||
EEPROM.get( eeAddress, f );
|
EEPROM.get(eeAddress, f);
|
||||||
Serial.println( f, 3 ); //This may print 'ovf, nan' if the data inside the EEPROM is not a valid float.
|
Serial.println(f, 3); //This may print 'ovf, nan' if the data inside the EEPROM is not a valid float.
|
||||||
|
|
||||||
/***
|
/***
|
||||||
As get also returns a reference to 'f', you can use it inline.
|
As get also returns a reference to 'f', you can use it inline.
|
||||||
|
@ -45,22 +45,24 @@ void setup(){
|
||||||
secondTest(); //Run the next test.
|
secondTest(); //Run the next test.
|
||||||
}
|
}
|
||||||
|
|
||||||
struct MyObject{
|
struct MyObject {
|
||||||
float field1;
|
float field1;
|
||||||
byte field2;
|
byte field2;
|
||||||
char name[10];
|
char name[10];
|
||||||
};
|
};
|
||||||
|
|
||||||
void secondTest(){
|
void secondTest() {
|
||||||
int eeAddress = sizeof(float); //Move address to the next byte after float 'f'.
|
int eeAddress = sizeof(float); //Move address to the next byte after float 'f'.
|
||||||
|
|
||||||
MyObject customVar; //Variable to store custom object read from EEPROM.
|
MyObject customVar; //Variable to store custom object read from EEPROM.
|
||||||
EEPROM.get( eeAddress, customVar );
|
EEPROM.get(eeAddress, customVar);
|
||||||
|
|
||||||
Serial.println( "Read custom object from EEPROM: " );
|
Serial.println("Read custom object from EEPROM: ");
|
||||||
Serial.println( customVar.field1 );
|
Serial.println(customVar.field1);
|
||||||
Serial.println( customVar.field2 );
|
Serial.println(customVar.field2);
|
||||||
Serial.println( customVar.name );
|
Serial.println(customVar.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop(){ /* Empty loop */ }
|
void loop() {
|
||||||
|
/* Empty loop */
|
||||||
|
}
|
|
@ -19,7 +19,7 @@ void setup() {
|
||||||
Iterate the EEPROM using a for loop.
|
Iterate the EEPROM using a for loop.
|
||||||
***/
|
***/
|
||||||
|
|
||||||
for( int index = 0 ; index < EEPROM.length() ; index++ ){
|
for (int index = 0 ; index < EEPROM.length() ; index++) {
|
||||||
|
|
||||||
//Add one to each cell in the EEPROM
|
//Add one to each cell in the EEPROM
|
||||||
EEPROM[ index ] += 1;
|
EEPROM[ index ] += 1;
|
||||||
|
@ -31,7 +31,7 @@ void setup() {
|
||||||
|
|
||||||
int index = 0;
|
int index = 0;
|
||||||
|
|
||||||
while( index < EEPROM.length() ){
|
while (index < EEPROM.length()) {
|
||||||
|
|
||||||
//Add one to each cell in the EEPROM
|
//Add one to each cell in the EEPROM
|
||||||
EEPROM[ index ] += 1;
|
EEPROM[ index ] += 1;
|
||||||
|
@ -44,14 +44,14 @@ void setup() {
|
||||||
|
|
||||||
int idx = 0; //Used 'idx' to avoid name conflict with 'index' above.
|
int idx = 0; //Used 'idx' to avoid name conflict with 'index' above.
|
||||||
|
|
||||||
do{
|
do {
|
||||||
|
|
||||||
//Add one to each cell in the EEPROM
|
//Add one to each cell in the EEPROM
|
||||||
EEPROM[ idx ] += 1;
|
EEPROM[ idx ] += 1;
|
||||||
idx++;
|
idx++;
|
||||||
}while( idx < EEPROM.length() );
|
} while (idx < EEPROM.length());
|
||||||
|
|
||||||
|
|
||||||
} //End of setup function.
|
} //End of setup function.
|
||||||
|
|
||||||
void loop(){}
|
void loop() {}
|
|
@ -16,13 +16,13 @@
|
||||||
|
|
||||||
#include <EEPROM.h>
|
#include <EEPROM.h>
|
||||||
|
|
||||||
struct MyObject{
|
struct MyObject {
|
||||||
float field1;
|
float field1;
|
||||||
byte field2;
|
byte field2;
|
||||||
char name[10];
|
char name[10];
|
||||||
};
|
};
|
||||||
|
|
||||||
void setup(){
|
void setup() {
|
||||||
|
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
while (!Serial) {
|
while (!Serial) {
|
||||||
|
@ -34,7 +34,7 @@ void setup(){
|
||||||
|
|
||||||
|
|
||||||
//One simple call, with the address first and the object second.
|
//One simple call, with the address first and the object second.
|
||||||
EEPROM.put( eeAddress, f );
|
EEPROM.put(eeAddress, f);
|
||||||
|
|
||||||
Serial.println("Written float data type!");
|
Serial.println("Written float data type!");
|
||||||
|
|
||||||
|
@ -49,8 +49,10 @@ void setup(){
|
||||||
|
|
||||||
eeAddress += sizeof(float); //Move address to the next byte after float 'f'.
|
eeAddress += sizeof(float); //Move address to the next byte after float 'f'.
|
||||||
|
|
||||||
EEPROM.put( eeAddress, customVar );
|
EEPROM.put(eeAddress, customVar);
|
||||||
Serial.print( "Written custom data type! \n\nView the example sketch eeprom_get to see how you can retrieve the values!" );
|
Serial.print("Written custom data type! \n\nView the example sketch eeprom_get to see how you can retrieve the values!");
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop(){ /* Empty loop */ }
|
void loop() {
|
||||||
|
/* Empty loop */
|
||||||
|
}
|
|
@ -12,8 +12,7 @@
|
||||||
int address = 0;
|
int address = 0;
|
||||||
byte value;
|
byte value;
|
||||||
|
|
||||||
void setup()
|
void setup() {
|
||||||
{
|
|
||||||
// initialize serial and wait for port to open:
|
// initialize serial and wait for port to open:
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
while (!Serial) {
|
while (!Serial) {
|
||||||
|
@ -21,8 +20,7 @@ void setup()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop()
|
void loop() {
|
||||||
{
|
|
||||||
// read a byte from the current address of the EEPROM
|
// read a byte from the current address of the EEPROM
|
||||||
value = EEPROM.read(address);
|
value = EEPROM.read(address);
|
||||||
|
|
||||||
|
@ -43,8 +41,9 @@ void loop()
|
||||||
This will make your code portable to all AVR processors.
|
This will make your code portable to all AVR processors.
|
||||||
***/
|
***/
|
||||||
address = address + 1;
|
address = address + 1;
|
||||||
if(address == EEPROM.length())
|
if (address == EEPROM.length()) {
|
||||||
address = 0;
|
address = 0;
|
||||||
|
}
|
||||||
|
|
||||||
/***
|
/***
|
||||||
As the EEPROM sizes are powers of two, wrapping (preventing overflow) of an
|
As the EEPROM sizes are powers of two, wrapping (preventing overflow) of an
|
||||||
|
|
|
@ -16,10 +16,11 @@
|
||||||
/** the current address in the EEPROM (i.e. which byte we're going to write to next) **/
|
/** the current address in the EEPROM (i.e. which byte we're going to write to next) **/
|
||||||
int address = 0;
|
int address = 0;
|
||||||
|
|
||||||
void setup(){ /** EMpty setup **/ }
|
void setup() {
|
||||||
|
/** EMpty setup **/
|
||||||
|
}
|
||||||
|
|
||||||
void loop()
|
void loop() {
|
||||||
{
|
|
||||||
/***
|
/***
|
||||||
need to divide by 4 because analog inputs range from
|
need to divide by 4 because analog inputs range from
|
||||||
0 to 1023 and each byte of the EEPROM can only hold a
|
0 to 1023 and each byte of the EEPROM can only hold a
|
||||||
|
@ -55,8 +56,9 @@ void loop()
|
||||||
This will make your code portable to all AVR processors.
|
This will make your code portable to all AVR processors.
|
||||||
***/
|
***/
|
||||||
address = address + 1;
|
address = address + 1;
|
||||||
if(address == EEPROM.length())
|
if (address == EEPROM.length()) {
|
||||||
address = 0;
|
address = 0;
|
||||||
|
}
|
||||||
|
|
||||||
/***
|
/***
|
||||||
As the EEPROM sizes are powers of two, wrapping (preventing overflow) of an
|
As the EEPROM sizes are powers of two, wrapping (preventing overflow) of an
|
||||||
|
|
|
@ -11,10 +11,11 @@
|
||||||
/** the current address in the EEPROM (i.e. which byte we're going to write to next) **/
|
/** the current address in the EEPROM (i.e. which byte we're going to write to next) **/
|
||||||
int addr = 0;
|
int addr = 0;
|
||||||
|
|
||||||
void setup(){ /** Empty setup. **/}
|
void setup() {
|
||||||
|
/** Empty setup. **/
|
||||||
|
}
|
||||||
|
|
||||||
void loop()
|
void loop() {
|
||||||
{
|
|
||||||
/***
|
/***
|
||||||
Need to divide by 4 because analog inputs range from
|
Need to divide by 4 because analog inputs range from
|
||||||
0 to 1023 and each byte of the EEPROM can only hold a
|
0 to 1023 and each byte of the EEPROM can only hold a
|
||||||
|
@ -43,8 +44,9 @@ void loop()
|
||||||
This will make your code portable to all AVR processors.
|
This will make your code portable to all AVR processors.
|
||||||
***/
|
***/
|
||||||
addr = addr + 1;
|
addr = addr + 1;
|
||||||
if(addr == EEPROM.length())
|
if (addr == EEPROM.length()) {
|
||||||
addr = 0;
|
addr = 0;
|
||||||
|
}
|
||||||
|
|
||||||
/***
|
/***
|
||||||
As the EEPROM sizes are powers of two, wrapping (preventing overflow) of an
|
As the EEPROM sizes are powers of two, wrapping (preventing overflow) of an
|
||||||
|
|
|
@ -85,7 +85,7 @@ void loop() {
|
||||||
}
|
}
|
||||||
|
|
||||||
//Read from or write to register from the SCP1000:
|
//Read from or write to register from the SCP1000:
|
||||||
unsigned int readRegister(byte thisRegister, int bytesToRead ) {
|
unsigned int readRegister(byte thisRegister, int bytesToRead) {
|
||||||
byte inByte = 0; // incoming byte from the SPI
|
byte inByte = 0; // incoming byte from the SPI
|
||||||
unsigned int result = 0; // result to return
|
unsigned int result = 0; // result to return
|
||||||
Serial.print(thisRegister, BIN);
|
Serial.print(thisRegister, BIN);
|
||||||
|
@ -117,7 +117,7 @@ unsigned int readRegister(byte thisRegister, int bytesToRead ) {
|
||||||
// take the chip select high to de-select:
|
// take the chip select high to de-select:
|
||||||
digitalWrite(chipSelectPin, HIGH);
|
digitalWrite(chipSelectPin, HIGH);
|
||||||
// return the result:
|
// return the result:
|
||||||
return(result);
|
return (result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -36,7 +36,7 @@ const int slaveSelectPin = 10;
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
// set the slaveSelectPin as an output:
|
// set the slaveSelectPin as an output:
|
||||||
pinMode (slaveSelectPin, OUTPUT);
|
pinMode(slaveSelectPin, OUTPUT);
|
||||||
// initialize SPI:
|
// initialize SPI:
|
||||||
SPI.begin();
|
SPI.begin();
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,8 +29,7 @@
|
||||||
|
|
||||||
SoftwareSerial mySerial(10, 11); // RX, TX
|
SoftwareSerial mySerial(10, 11); // RX, TX
|
||||||
|
|
||||||
void setup()
|
void setup() {
|
||||||
{
|
|
||||||
// Open serial communications and wait for port to open:
|
// Open serial communications and wait for port to open:
|
||||||
Serial.begin(57600);
|
Serial.begin(57600);
|
||||||
while (!Serial) {
|
while (!Serial) {
|
||||||
|
@ -45,11 +44,12 @@ void setup()
|
||||||
mySerial.println("Hello, world?");
|
mySerial.println("Hello, world?");
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() // run over and over
|
void loop() { // run over and over
|
||||||
{
|
if (mySerial.available()) {
|
||||||
if (mySerial.available())
|
|
||||||
Serial.write(mySerial.read());
|
Serial.write(mySerial.read());
|
||||||
if (Serial.available())
|
}
|
||||||
|
if (Serial.available()) {
|
||||||
mySerial.write(Serial.read());
|
mySerial.write(Serial.read());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -42,8 +42,7 @@ SoftwareSerial portOne(10, 11);
|
||||||
// on the Mega, use other pins instead, since 8 and 9 don't work on the Mega
|
// on the Mega, use other pins instead, since 8 and 9 don't work on the Mega
|
||||||
SoftwareSerial portTwo(8, 9);
|
SoftwareSerial portTwo(8, 9);
|
||||||
|
|
||||||
void setup()
|
void setup() {
|
||||||
{
|
|
||||||
// Open serial communications and wait for port to open:
|
// Open serial communications and wait for port to open:
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
while (!Serial) {
|
while (!Serial) {
|
||||||
|
@ -56,8 +55,7 @@ void setup()
|
||||||
portTwo.begin(9600);
|
portTwo.begin(9600);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop()
|
void loop() {
|
||||||
{
|
|
||||||
// By default, the last intialized port is listening.
|
// By default, the last intialized port is listening.
|
||||||
// when you want to listen on a port, explicitly select it:
|
// when you want to listen on a port, explicitly select it:
|
||||||
portOne.listen();
|
portOne.listen();
|
||||||
|
|
|
@ -12,16 +12,14 @@
|
||||||
|
|
||||||
#include <Wire.h>
|
#include <Wire.h>
|
||||||
|
|
||||||
void setup()
|
void setup() {
|
||||||
{
|
|
||||||
Wire.begin(); // join i2c bus (address optional for master)
|
Wire.begin(); // join i2c bus (address optional for master)
|
||||||
Serial.begin(9600); // start serial communication at 9600bps
|
Serial.begin(9600); // start serial communication at 9600bps
|
||||||
}
|
}
|
||||||
|
|
||||||
int reading = 0;
|
int reading = 0;
|
||||||
|
|
||||||
void loop()
|
void loop() {
|
||||||
{
|
|
||||||
// step 1: instruct sensor to read echoes
|
// step 1: instruct sensor to read echoes
|
||||||
Wire.beginTransmission(112); // transmit to device #112 (0x70)
|
Wire.beginTransmission(112); // transmit to device #112 (0x70)
|
||||||
// the address specified in the datasheet is 224 (0xE0)
|
// the address specified in the datasheet is 224 (0xE0)
|
||||||
|
@ -44,8 +42,7 @@ void loop()
|
||||||
Wire.requestFrom(112, 2); // request 2 bytes from slave device #112
|
Wire.requestFrom(112, 2); // request 2 bytes from slave device #112
|
||||||
|
|
||||||
// step 5: receive reading from sensor
|
// step 5: receive reading from sensor
|
||||||
if (2 <= Wire.available()) // if two bytes were received
|
if (2 <= Wire.available()) { // if two bytes were received
|
||||||
{
|
|
||||||
reading = Wire.read(); // receive high byte (overwrites previous reading)
|
reading = Wire.read(); // receive high byte (overwrites previous reading)
|
||||||
reading = reading << 8; // shift high byte to be high 8 bits
|
reading = reading << 8; // shift high byte to be high 8 bits
|
||||||
reading |= Wire.read(); // receive low byte as lower 8 bits
|
reading |= Wire.read(); // receive low byte as lower 8 bits
|
||||||
|
|
|
@ -14,15 +14,13 @@
|
||||||
|
|
||||||
#include <Wire.h>
|
#include <Wire.h>
|
||||||
|
|
||||||
void setup()
|
void setup() {
|
||||||
{
|
|
||||||
Wire.begin(); // join i2c bus (address optional for master)
|
Wire.begin(); // join i2c bus (address optional for master)
|
||||||
}
|
}
|
||||||
|
|
||||||
byte val = 0;
|
byte val = 0;
|
||||||
|
|
||||||
void loop()
|
void loop() {
|
||||||
{
|
|
||||||
Wire.beginTransmission(44); // transmit to device #44 (0x2c)
|
Wire.beginTransmission(44); // transmit to device #44 (0x2c)
|
||||||
// device address is specified in datasheet
|
// device address is specified in datasheet
|
||||||
Wire.write(byte(0x00)); // sends instruction byte
|
Wire.write(byte(0x00)); // sends instruction byte
|
||||||
|
@ -30,8 +28,7 @@ void loop()
|
||||||
Wire.endTransmission(); // stop transmitting
|
Wire.endTransmission(); // stop transmitting
|
||||||
|
|
||||||
val++; // increment value
|
val++; // increment value
|
||||||
if (val == 64) // if reached 64th position (max)
|
if (val == 64) { // if reached 64th position (max)
|
||||||
{
|
|
||||||
val = 0; // start over from lowest value
|
val = 0; // start over from lowest value
|
||||||
}
|
}
|
||||||
delay(500);
|
delay(500);
|
||||||
|
|
|
@ -12,18 +12,15 @@
|
||||||
|
|
||||||
#include <Wire.h>
|
#include <Wire.h>
|
||||||
|
|
||||||
void setup()
|
void setup() {
|
||||||
{
|
|
||||||
Wire.begin(); // join i2c bus (address optional for master)
|
Wire.begin(); // join i2c bus (address optional for master)
|
||||||
Serial.begin(9600); // start serial for output
|
Serial.begin(9600); // start serial for output
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop()
|
void loop() {
|
||||||
{
|
|
||||||
Wire.requestFrom(8, 6); // request 6 bytes from slave device #8
|
Wire.requestFrom(8, 6); // request 6 bytes from slave device #8
|
||||||
|
|
||||||
while (Wire.available()) // slave may send less than requested
|
while (Wire.available()) { // slave may send less than requested
|
||||||
{
|
|
||||||
char c = Wire.read(); // receive a byte as character
|
char c = Wire.read(); // receive a byte as character
|
||||||
Serial.print(c); // print the character
|
Serial.print(c); // print the character
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,15 +12,13 @@
|
||||||
|
|
||||||
#include <Wire.h>
|
#include <Wire.h>
|
||||||
|
|
||||||
void setup()
|
void setup() {
|
||||||
{
|
|
||||||
Wire.begin(); // join i2c bus (address optional for master)
|
Wire.begin(); // join i2c bus (address optional for master)
|
||||||
}
|
}
|
||||||
|
|
||||||
byte x = 0;
|
byte x = 0;
|
||||||
|
|
||||||
void loop()
|
void loop() {
|
||||||
{
|
|
||||||
Wire.beginTransmission(8); // transmit to device #8
|
Wire.beginTransmission(8); // transmit to device #8
|
||||||
Wire.write("x is "); // sends five bytes
|
Wire.write("x is "); // sends five bytes
|
||||||
Wire.write(x); // sends one byte
|
Wire.write(x); // sends one byte
|
||||||
|
|
|
@ -12,24 +12,20 @@
|
||||||
|
|
||||||
#include <Wire.h>
|
#include <Wire.h>
|
||||||
|
|
||||||
void setup()
|
void setup() {
|
||||||
{
|
|
||||||
Wire.begin(8); // join i2c bus with address #8
|
Wire.begin(8); // join i2c bus with address #8
|
||||||
Wire.onReceive(receiveEvent); // register event
|
Wire.onReceive(receiveEvent); // register event
|
||||||
Serial.begin(9600); // start serial for output
|
Serial.begin(9600); // start serial for output
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop()
|
void loop() {
|
||||||
{
|
|
||||||
delay(100);
|
delay(100);
|
||||||
}
|
}
|
||||||
|
|
||||||
// function that executes whenever data is received from master
|
// function that executes whenever data is received from master
|
||||||
// this function is registered as an event, see setup()
|
// this function is registered as an event, see setup()
|
||||||
void receiveEvent(int howMany)
|
void receiveEvent(int howMany) {
|
||||||
{
|
while (1 < Wire.available()) { // loop through all but the last
|
||||||
while (1 < Wire.available()) // loop through all but the last
|
|
||||||
{
|
|
||||||
char c = Wire.read(); // receive byte as a character
|
char c = Wire.read(); // receive byte as a character
|
||||||
Serial.print(c); // print the character
|
Serial.print(c); // print the character
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,21 +12,18 @@
|
||||||
|
|
||||||
#include <Wire.h>
|
#include <Wire.h>
|
||||||
|
|
||||||
void setup()
|
void setup() {
|
||||||
{
|
|
||||||
Wire.begin(8); // join i2c bus with address #8
|
Wire.begin(8); // join i2c bus with address #8
|
||||||
Wire.onRequest(requestEvent); // register event
|
Wire.onRequest(requestEvent); // register event
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop()
|
void loop() {
|
||||||
{
|
|
||||||
delay(100);
|
delay(100);
|
||||||
}
|
}
|
||||||
|
|
||||||
// function that executes whenever data is requested by master
|
// function that executes whenever data is requested by master
|
||||||
// this function is registered as an event, see setup()
|
// this function is registered as an event, see setup()
|
||||||
void requestEvent()
|
void requestEvent() {
|
||||||
{
|
|
||||||
Wire.write("hello "); // respond with message of 6 bytes
|
Wire.write("hello "); // respond with message of 6 bytes
|
||||||
// as expected by master
|
// as expected by master
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue