0
Offline
Jr. Member
Karma: 0
Posts: 66
Arduino rocks
|
 |
« Reply #15 on: June 15, 2009, 12:00:29 pm » |
Going above 4 will be scary since the arduino does not support datatyes above 4 bytes. I'm guessing that the number 2828601 is above 4 bytes? because I can't get it to send properly it will send all but 4 bits of data this is what I get: 2828601=1010110010100100111001 it will get: 101011 101001 111001 I don't get why its those bits in between. for the code all i'm having it do is print out the buffer array each cell at a time. any ideas, is this even possible to send?
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Jr. Member
Karma: 0
Posts: 77
Arduino rocks
|
 |
« Reply #16 on: June 15, 2009, 09:37:05 pm » |
There is "long long" introduced in C99 that is required to be at least 64 bits wide.
Serial.println(sizeof(long long)); seems to confirm it's availability in arduino.
|
|
|
|
|
Logged
|
|
|
|
|
Left Coast, CA (USA)
Offline
Brattain Member
Karma: 282
Posts: 15443
Measurement changes behavior
|
 |
« Reply #17 on: June 15, 2009, 10:41:54 pm » |
Is "long long" type really avalible in the Arduino system? It's not listed in the Arduino reference data types.
Lefty
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Jr. Member
Karma: 0
Posts: 77
Arduino rocks
|
 |
« Reply #18 on: June 16, 2009, 06:23:41 am » |
Certainly seems like it. void setup() { Serial.begin(9600); delay(100); }
void loop() { long long x; Serial.println(sizeof x); }
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Jr. Member
Karma: 0
Posts: 66
Arduino rocks
|
 |
« Reply #19 on: June 16, 2009, 07:17:54 pm » |
I cant figure this out. I have to be able to send a value as large as 9999999 but the formating keeps changing and I don't know what to do from here. This is my sending code: #include <Wire.h> #define buzz 11 #define master 1
void setup(){ Serial.begin(9600); Wire.begin(1); Wire.onReceive(inp); // sendData(); }
void loop(){ long long user=4638671; byte* pInt=(byte*) &user; Wire.beginTransmission(2); Wire.send(pInt,sizeof(user)); Wire.endTransmission(); /* Wire.beginTransmission(2); Wire.send(pInt,sizeof(user)); Wire.endTransmission(); */ delay(5000); }
void inp(int count){ int x = Wire.receive(); Serial.println(x); } And this is my receiver code: #include <Wire.h> #define address 2 #define green 13
int inputCount=0; long long result=0; //int x=0; void setup(){ Serial.begin(9600); Serial.println("ready"); Wire.begin(2); Wire.onReceive(test); pinMode(green,OUTPUT); } void loop(){ }
void test(int sizeIn){ Serial.println(sizeIn); int buffer[sizeIn]; while(Wire.available()){ buffer[inputCount]=Wire.receive(); inputCount++; //Serial.println(buffer[inputCount]); } switch(inputCount){ case 1: result = buffer[0]; break; case 2: result = buffer[1]<<8*1|buffer[0]<<8*0; break; case 3: result =buffer[2]<<8*2|buffer[1]<<8*1|buffer[0]<<8*0; break; case 8: result= buffer[2]<<16 | buffer[1]<<8 | buffer[0]; Serial.println(result,BIN); //Serial.println(result); Serial.println(buffer[0],BIN); Serial.println(buffer[1],BIN); Serial.println(buffer[2],BIN); Serial.println(buffer[3],BIN); break; } //Serial.println(result); inputCount=0; result=0;
} I succeeded in getting it to send numbers of the size #### but this is getting more and more complicated and i'm getting more and more confused. any ideas on how to make this simpler? can it be simpler? or does it need to be more complicated?
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 143
Posts: 19365
I don't think you connected the grounds, Dave.
|
 |
« Reply #20 on: June 17, 2009, 01:34:33 am » |
a value as large as 9999999 A range of only 10 million doesn't need a "long long". An ordinary signed long (32 bits) covers the range -2147483648 to +2147483647 decimal. An unsigned long will cover 0..4294967296 decimal. void test(int sizeIn){
What is the value of "sizeIn"? Where does it come from? I don't see why you don't just have a fixed-size receiver buffer, bit enough to contain a single "long".
|
|
|
|
« Last Edit: June 17, 2009, 02:01:42 am by AWOL »
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
0
Offline
Jr. Member
Karma: 0
Posts: 66
Arduino rocks
|
 |
« Reply #21 on: June 17, 2009, 11:20:57 am » |
void test(int sizeIn) This is the function that is called when the arduino starts receiving data. Wire.onReceive(test); I think the reason I didn't use a long buffer is that because the number length may not always be a long. Is that even a problem? Also I'm not sure how I would go about doing that. Is that what I was doing? Is there a way i can just send the digits one at a time and then reassemble them, that seems like it would be the easiest.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 143
Posts: 19365
I don't think you connected the grounds, Dave.
|
 |
« Reply #22 on: June 17, 2009, 11:41:53 am » |
This is the function that is called when the arduino starts receiving data So, each time it gets called, you potentially have a different value of "sizeIn", but you're declaring "buffer" based on that size...see where this is going? "buffer" isn't static.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
0
Offline
Jr. Member
Karma: 0
Posts: 66
Arduino rocks
|
 |
« Reply #23 on: June 17, 2009, 11:49:52 am » |
the buffer doesn't seem to be working properly. I still don't understand this error. 2828601=1010110010100100111001 it will get: 101011 101001 111001
Why is it (dropping?) or not receiving those zeros in between? is it because they are leading zeros and they get truncated off because it assumes that the zeros are irrelevant? can I fix this, but if i fix this how will it change other values? because I've gotten it to successfully send some numbers of the same length but this zero problem would change everything. i think the most reliable way would be to send it bit by bit but I don't know how to do that.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 143
Posts: 19365
I don't think you connected the grounds, Dave.
|
 |
« Reply #24 on: June 17, 2009, 11:54:47 am » |
the buffer doesn't seem to be working properly. I Change the declaration to: "static byte buffer [8];" and try again.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Norway@Oslo
Offline
Edison Member
Karma: 11
Posts: 2033
loveArduino(true);
|
 |
« Reply #25 on: June 17, 2009, 12:20:31 pm » |
This code uses the binary values you posted and builds the correct number: void setup(){ Serial.begin(9600); byte original[] = {B111001,B101001,B101011,B0}; unsigned long target = 0; byte* targetWorker = (byte*)⌖ targetWorker[0] = original[0]; targetWorker[1] = original[1]; targetWorker[2] = original[2]; targetWorker[3] = original[3]; Serial.println(target); }
void loop(){ }
[edit] is it because they are leading zeros and they get truncated off because it assumes that the zeros are irrelevant?
This is correct. (Irrelevant for print, relevant for bit shifting/reconstruction.)[/edit]
|
|
|
|
« Last Edit: June 17, 2009, 12:22:28 pm by AlphaBeta »
|
Logged
|
|
|
|
|
0
Offline
Jr. Member
Karma: 0
Posts: 66
Arduino rocks
|
 |
« Reply #26 on: June 17, 2009, 01:40:54 pm » |
I think that might give me a bit to work with to try to figure this out, The only issue I see is that the value i'm sending will change. The value is coming from a RFID reader.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 143
Posts: 19365
I don't think you connected the grounds, Dave.
|
 |
« Reply #27 on: June 17, 2009, 01:46:41 pm » |
Please, think seriously about this: void test(int sizeIn){ Serial.println(sizeIn); int buffer[sizeIn];
It may work now, but there are no guarantees for the future.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
0
Offline
Jr. Member
Karma: 0
Posts: 66
Arduino rocks
|
 |
« Reply #28 on: June 18, 2009, 09:51:46 pm » |
I think I got it! here is the sender code: #include <Wire.h> #define buzz 11 #define master 1 void setup(){ Serial.begin(9600); Wire.begin(1); Wire.onReceive(inp); // sendData(); } void loop(){ long user=1696; byte* pInt=(byte*) &user; Wire.beginTransmission(2); Wire.send(pInt,sizeof(user)); Wire.endTransmission(); /* Wire.beginTransmission(2); Wire.send(pInt,sizeof(user)); Wire.endTransmission(); */ delay(5000); } void inp(int count){ int x = Wire.receive(); Serial.println(x); } void sendData(){ } And here is the receiver code: #include <Wire.h> #define address 2 #define green 13
int inputCount=0; unsigned long user=0; void setup(){ Serial.begin(9600); Serial.println("ready"); Wire.begin(2); Wire.onReceive(test); pinMode(green,OUTPUT); }
void loop(){ }
void test(int sizeIn){ inputCount=0; byte buffer[sizeIn]; while(Wire.available()){ buffer[inputCount]=Wire.receive(); inputCount++; } byte* targetWorker = (byte*)&user;
for(int i=0;i<=sizeIn;i++){ targetWorker = buffer; } Serial.println(user); } Is there a way to have the sender send it to all many different receiver devices? Or do I have to just have it loop through all of the addresses and send them one at a time?
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 143
Posts: 19365
I don't think you connected the grounds, Dave.
|
 |
« Reply #29 on: June 19, 2009, 01:47:20 am » |
I'm sorry, I'm still not getting this bit: void test(int sizeIn){ inputCount=0; byte buffer[sizeIn]; while(Wire.available()){ buffer[inputCount]=Wire.receive(); inputCount++; } byte* targetWorker = (byte*)&user;
for(int i=0;i<=sizeIn;i++){ targetWorker[i] = buffer[i]; } Serial.println(user); }
Suppose that "test" gets called with "sizeIn" = 1, because the receiver has only received one byte. So, "buffer" is allocated 1 byte. Now, it's quite possible that by the time the "while" loop has read that single character, the next one has arrived, so "available" is true, and that character is read. In fact, looking at the code, this is the only way this can work, unless "test" is always called with the value "4" for "sizeIn" - "test" has to read all four bytes in one hit, otherwise the byte-by-byte assignment to "targetWorker" will fail. But "buffer" was only allocated one byte. Can you confirm the value of "sizeIn"? This strikes me as a real ass-biter - anyone?
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
|