joao
New Member
Posts: 31
|
wire.h
Jan 10, 2017 13:13:56 GMT
Post by joao on Jan 10, 2017 13:13:56 GMT
Hello, I just received my Shieldbuddy today and i am using it with Arduino IDE 1.6.13.
I think i followed all instalation steps.
I am getting a problem with wire.h l
#include <Wire.h>
/*** Don't worry, the normal Arduino setup() and loop() are below this block! ***/
/* LMU uninitialised data */
StartOfUninitialised_LMURam_Variables
/* Put your LMU RAM fast access variables that have no initial values here e.g. uint32 LMU_var; */
EndOfUninitialised_LMURam_Variables
/* LMU uninitialised data */
StartOfInitialised_LMURam_Variables
int Cabine; EndOfInitialised_LMURam_Variables
void setup() {
// put your setup code for core 0 here, to run once:
SerialASC.begin(250000);
Cabine = 100;
Wire.begin();
}
void loop() {
SerialASC.println(Cabine);
Wire.beginTransmission(Cabine);
delay(1000);
}
i am trying some very simple examples but at this point i already get an error:
C:\Users\joao\Documents\Arduino\sketch_jan10c\sketch_jan10c.ino: In function 'void loop()':
sketch_jan10c:34: error: 'Wire' was not declared in this scope
exit status 1
'Wire' was not declared in this scope
Can someone tell me what is wrong?
|
|
|
wire.h
Jan 10, 2017 13:59:40 GMT
Post by sparrowdclxvi on Jan 10, 2017 13:59:40 GMT
You haven't instantiated an instance of Wire anywhere.
|
|
|
wire.h
Jan 10, 2017 14:05:59 GMT
Post by mjb on Jan 10, 2017 14:05:59 GMT
Try:
#include "Wire.h"
TwoWire TWI; // Instantiate TwoWire class
void setup(void){
TWI.begin();
|
|
joao
New Member
Posts: 31
|
wire.h
Jan 10, 2017 14:09:34 GMT
Post by joao on Jan 10, 2017 14:09:34 GMT
Sorry but i am not following, shouldnt the wire.h functions work when i add the library to the code?
|
|
|
wire.h
Jan 10, 2017 14:13:28 GMT
Post by mjb on Jan 10, 2017 14:13:28 GMT
This should be OK:
#include <Wire.h>
/*** Don't worry, the normal Arduino setup() and loop() are below this block! ***/
/* LMU uninitialised data */
StartOfUninitialised_LMURam_Variables
/* Put your LMU RAM fast access variables that have no initial values here e.g. uint32 LMU_var; */
EndOfUninitialised_LMURam_Variables
/* LMU uninitialised data */
StartOfInitialised_LMURam_Variables
int Cabine; EndOfInitialised_LMURam_Variables
TwoWire Wire;
void setup() {
// put your setup code for core 0 here, to run once:
SerialASC.begin(250000);
Cabine = 100;
Wire.begin();
}
void loop() {
SerialASC.println(Cabine);
Wire.beginTransmission(Cabine);
delay(1000);
}
|
|
joao
New Member
Posts: 31
|
wire.h
Jan 10, 2017 15:05:50 GMT
Post by joao on Jan 10, 2017 15:05:50 GMT
That seems to work, i dont fully understand why the need to "TwoWire Wire;" but solves the wire declaration problem, so thanks for that.
when i keep with the code i found more problems.
void loop() {
conversion1();
Wire.beginTransmission(Cabine); // transmit to device #100
Wire.write(myArray1,32); // sends 32 bytes
Wire.endTransmission(); // stop transmitting
delay(50); I run conversion1() to fill one array of values and then i transmit the array to a slave; so star i am not sure the Wire finctions are working because the code seems to stop in Wire.endTransmission();
any idea?
|
|
|
wire.h
Jan 10, 2017 16:03:54 GMT
Post by mjb on Jan 10, 2017 16:03:54 GMT
This can happen when the I2C device does not generate an ACK. What device is on the I2C bus?
|
|
joao
New Member
Posts: 31
|
wire.h
Jan 10, 2017 16:09:10 GMT
Post by joao on Jan 10, 2017 16:09:10 GMT
One arduino Mega, previously i had Two Megas communicating both ways.
|
|
|
wire.h
Jan 10, 2017 16:18:44 GMT
Post by mjb on Jan 10, 2017 16:18:44 GMT
Is the ShieldBuddy the master or the slave?
|
|
joao
New Member
Posts: 31
|
wire.h
Jan 10, 2017 16:24:03 GMT
Post by joao on Jan 10, 2017 16:24:03 GMT
Is the ShieldBuddy the master or the slave? The shieldbuddy is the Master, the slave one arduino mega in adress Cabine=100;
|
|
|
wire.h
Jan 10, 2017 17:27:18 GMT
Post by Admin on Jan 10, 2017 17:27:18 GMT
Please can you attach the sketch for the slave on the Mega so that we can test against the ShieldBuddy.
Tnx
|
|
joao
New Member
Posts: 31
|
wire.h
Jan 11, 2017 8:16:48 GMT
Post by joao on Jan 11, 2017 8:16:48 GMT
Please can you attach the sketch for the slave on the Mega so that we can test against the ShieldBuddy. Tnx Hello here are all the codes, the ones i was using with the 2 Megas and the Shielbuddy code Master Code Mega #include "Wire.h"
byte Command;
byte Cabine;
long a1=10001;
long a2=10002;
long a3=10003;
long a4=10004;
long a5=10005;
long a6=10006;
long a7=10007;
byte myArray1[32];
byte buffer[32];
void setup(){
Wire.begin();
Serial.begin(250000);
Command=0;
Cabine = 100;
Serial.println("setup");
}
int clearbuffer(){
byte i=0;
while (i<32){
buffer[i]=0;
i++;
}
return(0);
}
int conversion1(){ //Packet 1 - send 7 * 4Bytes
myArray1[0] = (a1 >> 24) & 0xFF;
myArray1[1] = (a1 >> 16) & 0xFF;
myArray1[2] = (a1 >> 8) & 0xFF;
myArray1[3] = a1 & 0xFF;
myArray1[4] = (a2 >> 24) & 0xFF;
myArray1[5] = (a2 >> 16) & 0xFF;
myArray1[6] = (a2 >> 8) & 0xFF;
myArray1[7] = a2 & 0xFF;
myArray1[8] = (a3 >> 24) & 0xFF;
myArray1[9] = (a3 >> 16) & 0xFF;
myArray1[10] = (a3 >> 8) & 0xFF;
myArray1[11] = a3 & 0xFF;
myArray1[12] = (a4 >> 24) & 0xFF;
myArray1[13] = (a4 >> 16) & 0xFF;
myArray1[14] = (a4 >> 8) & 0xFF;
myArray1[15] = a4 & 0xFF;
myArray1[16] = (a5 >> 24) & 0xFF;
myArray1[17] = (a5 >> 16) & 0xFF;
myArray1[18] = (a5 >> 8) & 0xFF;
myArray1[19] = a5 & 0xFF;
myArray1[20] = (a6 >> 24) & 0xFF;
myArray1[21] = (a6 >> 16) & 0xFF;
myArray1[22] = (a6 >> 8) & 0xFF;
myArray1[23] = a6 & 0xFF;
myArray1[24] = (a7 >> 24) & 0xFF;
myArray1[25] = (a7 >> 16) & 0xFF;
myArray1[26] = (a7 >> 8) & 0xFF;
myArray1[27] = a7 & 0xFF;
myArray1[28] = 0;
myArray1[29] = 0;
myArray1[30] = 0;
myArray1[31] = 1;
byte i=0;
while(i<32){
Serial.println(myArray1[i]);
i++;
}
return(0);
}
void loop() {
conversion1();
Wire.beginTransmission(100); // transmit to device #100
Wire.write(myArray1,32); // sends 32 bytes
Wire.endTransmission(); // stop transmitting
delay(50);
clearbuffer();
a7++;
delay(950);
}
The slave code in de Mega #include <Wire.h>
byte Command;
byte Flag_com;
byte buffer[32];
byte x[32];
long a1=1;
long a2=1;
long a3=1;
long a4=1;
long a5=1;
long a6=1;
long a7=1;
long a8=1;
void setup() {
Wire.begin(100); // join i2c bus with address #100
Wire.onReceive(receiveEvent); // register event
Serial.begin(250000); // start serial for output
Command = 0;
Flag_com=0;
byte i=0;
while(i<32){
buffer[i]=0;
i++;
}
}
int clearbuffer(){
byte i=0;
while (i<32){
buffer[i]=0;
i++;
}
return(0);
}
int conversion1(){
a1 = buffer[0];
a1 = (a1 << 8) | buffer[1];
a1 = (a1 << 8) | buffer[2];
a1 = (a1 << 8) | buffer[3];
a2 = buffer[4];
a2 = (a2 << 8) | buffer[5];
a2 = (a2 << 8) | buffer[6];
a2 = (a2 << 8) | buffer[7];
a3 = buffer[8];
a3 = (a3 << 8) | buffer[9];
a3 = (a3 << 8) | buffer[10];
a3 = (a3 << 8) | buffer[11];
a4 = buffer[12];
a4 = (a4 << 8) | buffer[13];
a4 = (a4 << 8) | buffer[14];
a4 = (a4 << 8) | buffer[15];
a5 = buffer[16];
a5 = (a5 << 8) | buffer[17];
a5 = (a5 << 8) | buffer[18];
a5 = (a5 << 8) | buffer[19];
a6 = buffer[20];
a6 = (a6 << 8) | buffer[21];
a6 = (a6 << 8) | buffer[22];
a6 = (a6 << 8) | buffer[23];
a7 = buffer[24];
a7 = (a7 << 8) | buffer[25];
a7 = (a7 << 8) | buffer[26];
a7 = (a7 << 8) | buffer[27];
a8 = buffer[28];
a8 = (a8 << 8) | buffer[29];
a8 = (a8 << 8) | buffer[30];
a8 = (a8 << 8) | buffer[31];
//clearbuffer();
return(0);
}
void loop(){
conversion1();
clearbuffer();
Serial.println(a1);
Serial.println(a2);
Serial.println(a3);
Serial.println(a4);
Serial.println(a5);
Serial.println(a6);
Serial.println(a7);
Serial.println(a8);
delay(500);
}
void receiveEvent(int howmany) {
if(howmany==32){
buffer[0]=Wire.read();
buffer[1]=Wire.read();
buffer[2]=Wire.read();
buffer[3]=Wire.read();
buffer[4]=Wire.read();
buffer[5]=Wire.read();
buffer[6]=Wire.read();
buffer[7]=Wire.read();
buffer[8]=Wire.read();
buffer[9]=Wire.read();
buffer[10]=Wire.read();
buffer[11]=Wire.read();
buffer[12]=Wire.read();
buffer[13]=Wire.read();
buffer[14]=Wire.read();
buffer[15]=Wire.read();
buffer[16]=Wire.read();
buffer[17]=Wire.read();
buffer[18]=Wire.read();
buffer[19]=Wire.read();
buffer[20]=Wire.read();
buffer[21]=Wire.read();
buffer[22]=Wire.read();
buffer[23]=Wire.read();
buffer[24]=Wire.read();
buffer[25]=Wire.read();
buffer[26]=Wire.read();
buffer[27]=Wire.read();
buffer[28]=Wire.read();
buffer[29]=Wire.read();
buffer[30]=Wire.read();
buffer[31]=Wire.read();
Command=buffer[31];
}
}
and the Shield Buddy as Master #include "Wire.h"
/* LMU uninitialised data */
StartOfUninitialised_LMURam_Variables
/* Put your LMU RAM fast access variables that have no initial values here e.g. uint32 LMU_var; */
EndOfUninitialised_LMURam_Variables
/* LMU uninitialised data */
StartOfInitialised_LMURam_Variables
/* Put your LMU RAM fast access variables that have an initial value here e.g. uint32 LMU_var_init = 1; */
EndOfInitialised_LMURam_Variables
byte Command;
byte Cabine;
long a1=10001;
long a2=10002;
long a3=10003;
long a4=10004;
long a5=10005;
long a6=10006;
long a7=10007;
byte myArray1[32];
byte buffer[32];
TwoWire Wire;
void setup(){
Wire.begin();
SerialASC.begin(250000);
Command=0;
Cabine = 100;
SerialASC.println("setup");
}
int clearbuffer(){
byte i=0;
while (i<32){
buffer[i]=0;
i++;
}
return(0);
}
int conversion1(){ //Packet 1 - send 7 * 4Bytes
myArray1[0] = (a1 >> 24) & 0xFF;
myArray1[1] = (a1 >> 16) & 0xFF;
myArray1[2] = (a1 >> 8) & 0xFF;
myArray1[3] = a1 & 0xFF;
myArray1[4] = (a2 >> 24) & 0xFF;
myArray1[5] = (a2 >> 16) & 0xFF;
myArray1[6] = (a2 >> 8) & 0xFF;
myArray1[7] = a2 & 0xFF;
myArray1[8] = (a3 >> 24) & 0xFF;
myArray1[9] = (a3 >> 16) & 0xFF;
myArray1[10] = (a3 >> 8) & 0xFF;
myArray1[11] = a3 & 0xFF;
myArray1[12] = (a4 >> 24) & 0xFF;
myArray1[13] = (a4 >> 16) & 0xFF;
myArray1[14] = (a4 >> 8) & 0xFF;
myArray1[15] = a4 & 0xFF;
myArray1[16] = (a5 >> 24) & 0xFF;
myArray1[17] = (a5 >> 16) & 0xFF;
myArray1[18] = (a5 >> 8) & 0xFF;
myArray1[19] = a5 & 0xFF;
myArray1[20] = (a6 >> 24) & 0xFF;
myArray1[21] = (a6 >> 16) & 0xFF;
myArray1[22] = (a6 >> 8) & 0xFF;
myArray1[23] = a6 & 0xFF;
myArray1[24] = (a7 >> 24) & 0xFF;
myArray1[25] = (a7 >> 16) & 0xFF;
myArray1[26] = (a7 >> 8) & 0xFF;
myArray1[27] = a7 & 0xFF;
myArray1[28] = 0;
myArray1[29] = 0;
myArray1[30] = 0;
myArray1[31] = 1;
byte i=0;
while(i<32){
SerialASC.println(myArray1[i]);
i++;
}
return(0);
}
void loop() {
SerialASC.println("loop");
conversion1();
Wire.beginTransmission(100);
Wire.write(myArray1,32);
Wire.endTransmission();
delay(50);
clearbuffer();
a7++;
delay(950);
}
So the code works pretty fine with the 2 Megas but when i try the Shieldbuddy the loop stops in Wire.endTransmission(); Also the manual says that i can use the arduino libraries such as wire.h so why do i need to use "TwoWire Wire;" to solve the "Wire not declared" problem and i need to thank the users in the forum for this help specially mjb for the help on this one. The connections are the same as the Megas, Shared Ground, 5V Supply from USB, SDA-SDA, SCL-SCL, the controllers are side by side. Please help me.
|
|
|
wire.h
Jan 11, 2017 8:26:50 GMT
Post by mjb on Jan 11, 2017 8:26:50 GMT
Thanks. It may take a day or two to sort this out! BTW: the I2C on the ShieldBuddy is on pin 7 (SCL) and 6 (SDA) as this is where it is on bit-bashed Arduino Unos. It has been tested in Master mode only so far. We may change to using the pin 21 (SCL) and pin 20 (SDA) like on the Mega and Due.
|
|
joao
New Member
Posts: 31
|
wire.h
Jan 11, 2017 8:53:17 GMT
Post by joao on Jan 11, 2017 8:53:17 GMT
Thanks. It may take a day or two to sort this out! BTW: the I2C on the ShieldBuddy is on pin 7 (SCL) and 6 (SDA) as this is where it is on bit-bashed Arduino Unos. It has been tested in Master mode only so far. We may change to using the pin 21 (SCL) and pin 20 (SDA) like on the Mega and Due. Thanks, i fill a little dumb right now, i was using 20/21 pins, i changed and it started working immediately, still i dont find any reference in the manual to that. and in the board, 6/7 are mentioned as PWM ports also. That also brings me the question of wich PWM ports can i actually use? Once again thank you for the help, you saved me. i will give feedback of my progress. _________________________________________________________ I was able to do I2C communication with a Arduino Mega as Slave, both ways, sending and asking data is working properly. In a very near future i will implement I2C buffers so that i can go Bigger distances, i will need at least 10m.
|
|
|
wire.h
Jan 13, 2017 11:26:35 GMT
Post by mjb on Jan 13, 2017 11:26:35 GMT
|
|