Arduiono IDE and I2C - Page 1
Forum section: electronics / general

author: ssashton
author_rank: Frequent Contributor
timestamp: July 07, 2020, 06:43:31 pm
content: Hi Everyone,I've been working on controlling a board over I2C from an Arduino Uno. I've managed to successfully get it to write a register from the Uno to the slave device with the code below.Code:#include <Wire.h>void setup() {byte subAddress; // 2-byte sub-address (0x0016).subAddress = 0x00;subAddress = 0x16;byte dataValue1; // 4-byte data value.dataValue1 = 0x00;dataValue1 = 0x00;dataValue1 = 0x00;dataValue1 = 0x00;byte dataValue2; // 4-byte data value.dataValue2 = 0x01;dataValue2 = 0x00;dataValue2 = 0x00;dataValue2 = 0x00;Wire.begin(); // join i2c busWire.setClock(400000); // Set 400KHz frequencyWire.beginTransmission(59); // Begin I2C transmission to 7-bit address (0x3B)adds R/W bit automatically.Wire.write(subAddress, 2); // Queue register sub-addressWire.write(dataValue1, 4); // Queue data value. Turn LED on solid.Wire.endTransmission(); // Send data queue and end transmission with stop bit.delay(10000);Wire.beginTransmission(59); // Begin I2C transmission to 7-bit address (0x3B)adds R/W bit automatically.Wire.write(subAddress, 2); // Queue register sub-addressWire.write(dataValue2, 4); // Queue data value. Turn LED on solid.Wire.endTransmission(); // Send data queue and end transmission with stop bit.}void loop() {}When I start writing the proper code (this is just a test) I imagine it would be good to have those registers as global variables. But when I try to declare the byte var before void setup I get the error 'subAddress' does not name a type. Can I get some help to understand why this is?Code:#include <Wire.h>byte subAddress; // 2-byte sub-address (0x0016).subAddress = 0x00;subAddress = 0x16;byte dataValue1; // 4-byte data value.dataValue1 = 0x00;dataValue1 = 0x00;dataValue1 = 0x00;dataValue1 = 0x00;byte dataValue2; // 4-byte data value.dataValue2 = 0x01;dataValue2 = 0x00;dataValue2 = 0x00;dataValue2 = 0x00;void setup() {Wire.begin(); // join i2c busWire.setClock(400000); // Set 400KHz frequencyWire.beginTransmission(59); // Begin I2C transmission to 7-bit address (0x3B)adds R/W bit automatically.Wire.write(subAddress, 2); // Queue register sub-addressWire.write(dataValue1, 4); // Queue data value. Turn LED on solid.Wire.endTransmission(); // Send data queue and end transmission with stop bit.delay(10000);Wire.beginTransmission(59); // Begin I2C transmission to 7-bit address (0x3B)adds R/W bit automatically.Wire.write(subAddress, 2); // Queue register sub-addressWire.write(dataValue2, 4); // Queue data value. Turn LED on solid.Wire.endTransmission(); // Send data queue and end transmission with stop bit.delay(10000); }void loop() {}Thanks

author: ssashton
author_rank: Frequent Contributor
timestamp: July 07, 2020, 07:03:00 pm
content: Haha just after posting I just learned what it was! You can declare a var outside of a function such as:Code:int inputVariable1;but you can't make a statement like:Code:inputVariable1 = 51;

author: mikerj
author_rank: Super Contributor
timestamp: July 07, 2020, 07:11:01 pm
content: You can define variables (including arrays and structs) and provide initialisation values like this:Code:byte subAddress = {0x00, 0x16);