Examples: mass code format. See example_formatter.conf
This commit is contained in:
parent
c13cf02651
commit
b5a130afb5
|
@ -10,26 +10,28 @@
|
|||
|
||||
#include <EEPROM.h>
|
||||
|
||||
void setup()
|
||||
{
|
||||
void setup() {
|
||||
|
||||
/***
|
||||
Iterate through each byte of the EEPROM storage.
|
||||
|
||||
Iterate through each byte of the EEPROM storage.
|
||||
|
||||
Larger AVR processors have larger EEPROM sizes, E.g:
|
||||
- Arduno Duemilanove: 512b EEPROM storage.
|
||||
- Arduino Uno: 1kb EEPROM storage.
|
||||
- Arduino Mega: 4kb EEPROM storage.
|
||||
|
||||
|
||||
Rather than hard-coding the length, you should use the pre-provided length function.
|
||||
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);
|
||||
}
|
||||
|
||||
// turn the LED on when we're done
|
||||
digitalWrite(13, HIGH);
|
||||
}
|
||||
|
||||
void loop(){ /** Empty loop. **/ }
|
||||
void loop() {
|
||||
/** Empty loop. **/
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/***
|
||||
Written by Christopher Andrews.
|
||||
CRC algorithm generated by pycrc, MIT licence ( https://github.com/tpircher/pycrc ).
|
||||
|
||||
|
||||
A CRC is a simple way of checking whether data has changed or become corrupted.
|
||||
This example calculates a CRC value directly on the EEPROM values.
|
||||
The purpose of this example is to highlight how the EEPROM object can be used just like an array.
|
||||
|
@ -10,8 +10,8 @@
|
|||
#include <Arduino.h>
|
||||
#include <EEPROM.h>
|
||||
|
||||
void setup(){
|
||||
|
||||
void setup() {
|
||||
|
||||
//Start serial
|
||||
Serial.begin(9600);
|
||||
while (!Serial) {
|
||||
|
@ -19,31 +19,33 @@ void setup(){
|
|||
}
|
||||
|
||||
//Print length of data to run CRC on.
|
||||
Serial.print( "EEPROM length: " );
|
||||
Serial.println( EEPROM.length() );
|
||||
|
||||
Serial.print("EEPROM length: ");
|
||||
Serial.println(EEPROM.length());
|
||||
|
||||
//Print the result of calling eeprom_crc()
|
||||
Serial.print( "CRC32 of EEPROM data: 0x" );
|
||||
Serial.println( eeprom_crc(), HEX );
|
||||
Serial.print( "\n\nDone!" );
|
||||
Serial.print("CRC32 of EEPROM data: 0x");
|
||||
Serial.println(eeprom_crc(), HEX);
|
||||
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] = {
|
||||
0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
|
||||
0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
|
||||
0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
|
||||
0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
|
||||
};
|
||||
|
||||
0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
|
||||
0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
|
||||
0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
|
||||
0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
|
||||
};
|
||||
|
||||
unsigned long crc = ~0L;
|
||||
|
||||
for( int index = 0 ; index < EEPROM.length() ; ++index ){
|
||||
crc = crc_table[( crc ^ EEPROM[index] ) & 0x0f] ^ (crc >> 4);
|
||||
crc = crc_table[( crc ^ ( EEPROM[index] >> 4 )) & 0x0f] ^ (crc >> 4);
|
||||
|
||||
for (int index = 0 ; index < EEPROM.length() ; ++index) {
|
||||
crc = crc_table[(crc ^ EEPROM[index]) & 0x0f] ^ (crc >> 4);
|
||||
crc = crc_table[(crc ^ (EEPROM[index] >> 4)) & 0x0f] ^ (crc >> 4);
|
||||
crc = ~crc;
|
||||
}
|
||||
return crc;
|
||||
|
|
|
@ -1,66 +1,68 @@
|
|||
/***
|
||||
eeprom_get example.
|
||||
|
||||
|
||||
This shows how to use the EEPROM.get() method.
|
||||
|
||||
|
||||
To pre-set the EEPROM data, run the example sketch eeprom_put.
|
||||
This sketch will run without it, however, the values shown
|
||||
This sketch will run without it, however, the values shown
|
||||
will be shown from what ever is already on the EEPROM.
|
||||
|
||||
|
||||
This may cause the serial object to print out a large string
|
||||
of garbage if there is no null character inside one of the strings
|
||||
loaded.
|
||||
|
||||
|
||||
Written by Christopher Andrews 2015
|
||||
Released under MIT licence.
|
||||
Released under MIT licence.
|
||||
***/
|
||||
|
||||
#include <EEPROM.h>
|
||||
|
||||
void setup(){
|
||||
|
||||
void setup() {
|
||||
|
||||
float f = 0.00f; //Variable to store data read from EEPROM.
|
||||
int eeAddress = 0; //EEPROM address to start reading from
|
||||
|
||||
Serial.begin( 9600 );
|
||||
|
||||
Serial.begin(9600);
|
||||
while (!Serial) {
|
||||
; // 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'
|
||||
EEPROM.get( eeAddress, f );
|
||||
Serial.println( f, 3 ); //This may print 'ovf, nan' if the data inside the EEPROM is not a valid float.
|
||||
|
||||
EEPROM.get(eeAddress, f);
|
||||
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.
|
||||
E.g: Serial.print( EEPROM.get( eeAddress, f ) );
|
||||
***/
|
||||
|
||||
/***
|
||||
Get can be used with custom structures too.
|
||||
|
||||
/***
|
||||
Get can be used with custom structures too.
|
||||
I have separated this into an extra function.
|
||||
***/
|
||||
|
||||
|
||||
secondTest(); //Run the next test.
|
||||
}
|
||||
|
||||
struct MyObject{
|
||||
struct MyObject {
|
||||
float field1;
|
||||
byte field2;
|
||||
char name[10];
|
||||
};
|
||||
|
||||
void secondTest(){
|
||||
void secondTest() {
|
||||
int eeAddress = sizeof(float); //Move address to the next byte after float 'f'.
|
||||
|
||||
MyObject customVar; //Variable to store custom object read from EEPROM.
|
||||
EEPROM.get( eeAddress, customVar );
|
||||
|
||||
Serial.println( "Read custom object from EEPROM: " );
|
||||
Serial.println( customVar.field1 );
|
||||
Serial.println( customVar.field2 );
|
||||
Serial.println( customVar.name );
|
||||
EEPROM.get(eeAddress, customVar);
|
||||
|
||||
Serial.println("Read custom object from EEPROM: ");
|
||||
Serial.println(customVar.field1);
|
||||
Serial.println(customVar.field2);
|
||||
Serial.println(customVar.name);
|
||||
}
|
||||
|
||||
void loop(){ /* Empty loop */ }
|
||||
void loop() {
|
||||
/* Empty loop */
|
||||
}
|
|
@ -1,12 +1,12 @@
|
|||
/***
|
||||
eeprom_iteration example.
|
||||
|
||||
|
||||
A set of example snippets highlighting the
|
||||
simplest methods for traversing the EEPROM.
|
||||
|
||||
Running this sketch is not necessary, this is
|
||||
|
||||
Running this sketch is not necessary, this is
|
||||
simply highlighting certain programming methods.
|
||||
|
||||
|
||||
Written by Christopher Andrews 2015
|
||||
Released under MIT licence.
|
||||
***/
|
||||
|
@ -18,40 +18,40 @@ void setup() {
|
|||
/***
|
||||
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
|
||||
EEPROM[ index ] += 1;
|
||||
}
|
||||
|
||||
|
||||
/***
|
||||
Iterate the EEPROM using a while loop.
|
||||
***/
|
||||
|
||||
|
||||
int index = 0;
|
||||
|
||||
while( index < EEPROM.length() ){
|
||||
|
||||
|
||||
while (index < EEPROM.length()) {
|
||||
|
||||
//Add one to each cell in the EEPROM
|
||||
EEPROM[ index ] += 1;
|
||||
EEPROM[ index ] += 1;
|
||||
index++;
|
||||
}
|
||||
|
||||
|
||||
/***
|
||||
Iterate the EEPROM using a do-while loop.
|
||||
***/
|
||||
|
||||
|
||||
int idx = 0; //Used 'idx' to avoid name conflict with 'index' above.
|
||||
|
||||
do{
|
||||
|
||||
|
||||
do {
|
||||
|
||||
//Add one to each cell in the EEPROM
|
||||
EEPROM[ idx ] += 1;
|
||||
EEPROM[ idx ] += 1;
|
||||
idx++;
|
||||
}while( idx < EEPROM.length() );
|
||||
|
||||
|
||||
} while (idx < EEPROM.length());
|
||||
|
||||
|
||||
} //End of setup function.
|
||||
|
||||
void loop(){}
|
||||
void loop() {}
|
|
@ -1,28 +1,28 @@
|
|||
/***
|
||||
eeprom_put example.
|
||||
|
||||
|
||||
This shows how to use the EEPROM.put() method.
|
||||
Also, this sketch will pre-set the EEPROM data for the
|
||||
Also, this sketch will pre-set the EEPROM data for the
|
||||
example sketch eeprom_get.
|
||||
|
||||
|
||||
Note, unlike the single byte version EEPROM.write(),
|
||||
the put method will use update semantics. As in a byte
|
||||
will only be written to the EEPROM if the data is actually
|
||||
different.
|
||||
|
||||
Written by Christopher Andrews 2015
|
||||
Released under MIT licence.
|
||||
Released under MIT licence.
|
||||
***/
|
||||
|
||||
#include <EEPROM.h>
|
||||
|
||||
struct MyObject{
|
||||
struct MyObject {
|
||||
float field1;
|
||||
byte field2;
|
||||
char name[10];
|
||||
};
|
||||
|
||||
void setup(){
|
||||
void setup() {
|
||||
|
||||
Serial.begin(9600);
|
||||
while (!Serial) {
|
||||
|
@ -31,15 +31,15 @@ void setup(){
|
|||
|
||||
float f = 123.456f; //Variable to store in EEPROM.
|
||||
int eeAddress = 0; //Location we want the data to be put.
|
||||
|
||||
|
||||
|
||||
|
||||
//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!");
|
||||
|
||||
|
||||
/** Put is designed for use with custom structures also. **/
|
||||
|
||||
|
||||
//Data to store.
|
||||
MyObject customVar = {
|
||||
3.14f,
|
||||
|
@ -48,9 +48,11 @@ void setup(){
|
|||
};
|
||||
|
||||
eeAddress += sizeof(float); //Move address to the next byte after float 'f'.
|
||||
|
||||
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!" );
|
||||
|
||||
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!");
|
||||
}
|
||||
|
||||
void loop(){ /* Empty loop */ }
|
||||
void loop() {
|
||||
/* Empty loop */
|
||||
}
|
|
@ -12,8 +12,7 @@
|
|||
int address = 0;
|
||||
byte value;
|
||||
|
||||
void setup()
|
||||
{
|
||||
void setup() {
|
||||
// initialize serial and wait for port to open:
|
||||
Serial.begin(9600);
|
||||
while (!Serial) {
|
||||
|
@ -21,8 +20,7 @@ void setup()
|
|||
}
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
void loop() {
|
||||
// read a byte from the current address of the EEPROM
|
||||
value = EEPROM.read(address);
|
||||
|
||||
|
@ -32,24 +30,25 @@ void loop()
|
|||
Serial.println();
|
||||
|
||||
/***
|
||||
Advance to the next address, when at the end restart at the beginning.
|
||||
|
||||
Advance to the next address, when at the end restart at the beginning.
|
||||
|
||||
Larger AVR processors have larger EEPROM sizes, E.g:
|
||||
- Arduno Duemilanove: 512b EEPROM storage.
|
||||
- Arduino Uno: 1kb EEPROM storage.
|
||||
- Arduino Mega: 4kb EEPROM storage.
|
||||
|
||||
|
||||
Rather than hard-coding the length, you should use the pre-provided length function.
|
||||
This will make your code portable to all AVR processors.
|
||||
This will make your code portable to all AVR processors.
|
||||
***/
|
||||
address = address + 1;
|
||||
if(address == EEPROM.length())
|
||||
if (address == EEPROM.length()) {
|
||||
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
|
||||
EEPROM address is also doable by a bitwise and of the length - 1.
|
||||
|
||||
|
||||
++address &= EEPROM.length() - 1;
|
||||
***/
|
||||
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
/***
|
||||
EEPROM Update method
|
||||
|
||||
|
||||
Stores values read from analog input 0 into the EEPROM.
|
||||
These values will stay in the EEPROM when the board is
|
||||
turned off and may be retrieved later by another sketch.
|
||||
|
||||
|
||||
If a value has not changed in the EEPROM, it is not overwritten
|
||||
which would reduce the life span of the EEPROM unnecessarily.
|
||||
|
||||
|
||||
Released using MIT licence.
|
||||
***/
|
||||
|
||||
|
@ -16,10 +16,11 @@
|
|||
/** the current address in the EEPROM (i.e. which byte we're going to write to next) **/
|
||||
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
|
||||
0 to 1023 and each byte of the EEPROM can only hold a
|
||||
|
@ -33,35 +34,36 @@ void loop()
|
|||
turned off.
|
||||
***/
|
||||
EEPROM.update(address, val);
|
||||
|
||||
|
||||
/***
|
||||
The function EEPROM.update(address, val) is equivalent to the following:
|
||||
|
||||
|
||||
if( EEPROM.read(address) != val ){
|
||||
EEPROM.write(address, val);
|
||||
}
|
||||
***/
|
||||
|
||||
|
||||
|
||||
/***
|
||||
Advance to the next address, when at the end restart at the beginning.
|
||||
|
||||
Advance to the next address, when at the end restart at the beginning.
|
||||
|
||||
Larger AVR processors have larger EEPROM sizes, E.g:
|
||||
- Arduno Duemilanove: 512b EEPROM storage.
|
||||
- Arduino Uno: 1kb EEPROM storage.
|
||||
- Arduino Mega: 4kb EEPROM storage.
|
||||
|
||||
|
||||
Rather than hard-coding the length, you should use the pre-provided length function.
|
||||
This will make your code portable to all AVR processors.
|
||||
This will make your code portable to all AVR processors.
|
||||
***/
|
||||
address = address + 1;
|
||||
if(address == EEPROM.length())
|
||||
if (address == EEPROM.length()) {
|
||||
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
|
||||
EEPROM address is also doable by a bitwise and of the length - 1.
|
||||
|
||||
|
||||
++address &= EEPROM.length() - 1;
|
||||
***/
|
||||
|
||||
|
|
|
@ -9,18 +9,19 @@
|
|||
#include <EEPROM.h>
|
||||
|
||||
/** 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
|
||||
0 to 1023 and each byte of the EEPROM can only hold a
|
||||
value from 0 to 255.
|
||||
***/
|
||||
|
||||
|
||||
int val = analogRead(0) / 4;
|
||||
|
||||
/***
|
||||
|
@ -28,28 +29,29 @@ void loop()
|
|||
these values will remain there when the board is
|
||||
turned off.
|
||||
***/
|
||||
|
||||
|
||||
EEPROM.write(addr, val);
|
||||
|
||||
/***
|
||||
Advance to the next address, when at the end restart at the beginning.
|
||||
|
||||
Advance to the next address, when at the end restart at the beginning.
|
||||
|
||||
Larger AVR processors have larger EEPROM sizes, E.g:
|
||||
- Arduno Duemilanove: 512b EEPROM storage.
|
||||
- Arduino Uno: 1kb EEPROM storage.
|
||||
- Arduino Mega: 4kb EEPROM storage.
|
||||
|
||||
|
||||
Rather than hard-coding the length, you should use the pre-provided length function.
|
||||
This will make your code portable to all AVR processors.
|
||||
This will make your code portable to all AVR processors.
|
||||
***/
|
||||
addr = addr + 1;
|
||||
if(addr == EEPROM.length())
|
||||
if (addr == EEPROM.length()) {
|
||||
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
|
||||
EEPROM address is also doable by a bitwise and of the length - 1.
|
||||
|
||||
|
||||
++addr &= EEPROM.length() - 1;
|
||||
***/
|
||||
|
||||
|
|
|
@ -85,7 +85,7 @@ void loop() {
|
|||
}
|
||||
|
||||
//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
|
||||
unsigned int result = 0; // result to return
|
||||
Serial.print(thisRegister, BIN);
|
||||
|
@ -117,7 +117,7 @@ unsigned int readRegister(byte thisRegister, int bytesToRead ) {
|
|||
// take the chip select high to de-select:
|
||||
digitalWrite(chipSelectPin, HIGH);
|
||||
// return the result:
|
||||
return(result);
|
||||
return (result);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -36,7 +36,7 @@ const int slaveSelectPin = 10;
|
|||
|
||||
void setup() {
|
||||
// set the slaveSelectPin as an output:
|
||||
pinMode (slaveSelectPin, OUTPUT);
|
||||
pinMode(slaveSelectPin, OUTPUT);
|
||||
// initialize SPI:
|
||||
SPI.begin();
|
||||
}
|
||||
|
|
|
@ -29,8 +29,7 @@
|
|||
|
||||
SoftwareSerial mySerial(10, 11); // RX, TX
|
||||
|
||||
void setup()
|
||||
{
|
||||
void setup() {
|
||||
// Open serial communications and wait for port to open:
|
||||
Serial.begin(57600);
|
||||
while (!Serial) {
|
||||
|
@ -45,11 +44,12 @@ void setup()
|
|||
mySerial.println("Hello, world?");
|
||||
}
|
||||
|
||||
void loop() // run over and over
|
||||
{
|
||||
if (mySerial.available())
|
||||
void loop() { // run over and over
|
||||
if (mySerial.available()) {
|
||||
Serial.write(mySerial.read());
|
||||
if (Serial.available())
|
||||
}
|
||||
if (Serial.available()) {
|
||||
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
|
||||
SoftwareSerial portTwo(8, 9);
|
||||
|
||||
void setup()
|
||||
{
|
||||
void setup() {
|
||||
// Open serial communications and wait for port to open:
|
||||
Serial.begin(9600);
|
||||
while (!Serial) {
|
||||
|
@ -56,8 +55,7 @@ void setup()
|
|||
portTwo.begin(9600);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
void loop() {
|
||||
// By default, the last intialized port is listening.
|
||||
// when you want to listen on a port, explicitly select it:
|
||||
portOne.listen();
|
||||
|
|
|
@ -12,16 +12,14 @@
|
|||
|
||||
#include <Wire.h>
|
||||
|
||||
void setup()
|
||||
{
|
||||
void setup() {
|
||||
Wire.begin(); // join i2c bus (address optional for master)
|
||||
Serial.begin(9600); // start serial communication at 9600bps
|
||||
}
|
||||
|
||||
int reading = 0;
|
||||
|
||||
void loop()
|
||||
{
|
||||
void loop() {
|
||||
// step 1: instruct sensor to read echoes
|
||||
Wire.beginTransmission(112); // transmit to device #112 (0x70)
|
||||
// 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
|
||||
|
||||
// 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 = reading << 8; // shift high byte to be high 8 bits
|
||||
reading |= Wire.read(); // receive low byte as lower 8 bits
|
||||
|
|
|
@ -14,15 +14,13 @@
|
|||
|
||||
#include <Wire.h>
|
||||
|
||||
void setup()
|
||||
{
|
||||
void setup() {
|
||||
Wire.begin(); // join i2c bus (address optional for master)
|
||||
}
|
||||
|
||||
byte val = 0;
|
||||
|
||||
void loop()
|
||||
{
|
||||
void loop() {
|
||||
Wire.beginTransmission(44); // transmit to device #44 (0x2c)
|
||||
// device address is specified in datasheet
|
||||
Wire.write(byte(0x00)); // sends instruction byte
|
||||
|
@ -30,8 +28,7 @@ void loop()
|
|||
Wire.endTransmission(); // stop transmitting
|
||||
|
||||
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
|
||||
}
|
||||
delay(500);
|
||||
|
|
|
@ -12,18 +12,15 @@
|
|||
|
||||
#include <Wire.h>
|
||||
|
||||
void setup()
|
||||
{
|
||||
void setup() {
|
||||
Wire.begin(); // join i2c bus (address optional for master)
|
||||
Serial.begin(9600); // start serial for output
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
void loop() {
|
||||
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
|
||||
Serial.print(c); // print the character
|
||||
}
|
||||
|
|
|
@ -12,15 +12,13 @@
|
|||
|
||||
#include <Wire.h>
|
||||
|
||||
void setup()
|
||||
{
|
||||
void setup() {
|
||||
Wire.begin(); // join i2c bus (address optional for master)
|
||||
}
|
||||
|
||||
byte x = 0;
|
||||
|
||||
void loop()
|
||||
{
|
||||
void loop() {
|
||||
Wire.beginTransmission(8); // transmit to device #8
|
||||
Wire.write("x is "); // sends five bytes
|
||||
Wire.write(x); // sends one byte
|
||||
|
|
|
@ -12,24 +12,20 @@
|
|||
|
||||
#include <Wire.h>
|
||||
|
||||
void setup()
|
||||
{
|
||||
void setup() {
|
||||
Wire.begin(8); // join i2c bus with address #8
|
||||
Wire.onReceive(receiveEvent); // register event
|
||||
Serial.begin(9600); // start serial for output
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
void loop() {
|
||||
delay(100);
|
||||
}
|
||||
|
||||
// function that executes whenever data is received from master
|
||||
// this function is registered as an event, see setup()
|
||||
void receiveEvent(int howMany)
|
||||
{
|
||||
while (1 < Wire.available()) // loop through all but the last
|
||||
{
|
||||
void receiveEvent(int howMany) {
|
||||
while (1 < Wire.available()) { // loop through all but the last
|
||||
char c = Wire.read(); // receive byte as a character
|
||||
Serial.print(c); // print the character
|
||||
}
|
||||
|
|
|
@ -12,21 +12,18 @@
|
|||
|
||||
#include <Wire.h>
|
||||
|
||||
void setup()
|
||||
{
|
||||
void setup() {
|
||||
Wire.begin(8); // join i2c bus with address #8
|
||||
Wire.onRequest(requestEvent); // register event
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
void loop() {
|
||||
delay(100);
|
||||
}
|
||||
|
||||
// function that executes whenever data is requested by master
|
||||
// this function is registered as an event, see setup()
|
||||
void requestEvent()
|
||||
{
|
||||
void requestEvent() {
|
||||
Wire.write("hello "); // respond with message of 6 bytes
|
||||
// as expected by master
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue