Pages: 1 ... 4 5 [6] 7 8   Go Down
Author Topic: Declaring digital signal  (Read 6952 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
Offline Offline
Brattain Member
*****
Karma: 484
Posts: 18764
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

After a lot of study on arrays, this is what I have come up with.
Code:
void receiveEvent(int howMany)
{
  char incoming[4]; // don't really know what the size of a g-code would be, so work on this number :P  
...

        if(i > 4){                       //prevent data corruption in case you accidentally receive more than the array can hold
            incoming[4] = '\0';  //put a null char at the end of the array
...
Is this any good or should I start studying again  smiley-confuse

Back to your studies. An array declared of length 4 like this:

Code:
char incoming[4];

... has 4 bytes:

Code:
incoming[0]
incoming[1]
incoming[2]
incoming[3]

So your write to incoming [4] will corrupt memory.
Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 484
Posts: 18764
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

All right. Let me post the new code that I corrected with a lot of help from the posters here. This is in Reprap.
Code:
...
void loop
{
  Wire.beginTransmission (5);
  Wire.send ("G1");
  Wire.send ("G28");
}

Even if you had the Wire.endTransmission, like this:

Code:
void loop
{
  Wire.beginTransmission (5);
  Wire.send ("G1");
  Wire.send ("G28");
  Wire.endTransmission ();
}

You are now sending 5 bytes ("G1G28"). But your code you recently posted expected 4 (plus a 0x00 at the end). So when you say "don't really know what the size of a g-code would be" you know you have sent 5 bytes, right?
« Last Edit: August 27, 2011, 07:51:46 pm by Nick Gammon » Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 484
Posts: 18764
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

After a lot of study on arrays, this is what I have come up with.
Code:
void receiveEvent(int howMany)
{
  char incoming[4]; // don't really know what the size of a g-code would be, so work on this number :P 
  int length;
  while(0 < Wire.available()) // loop through all
   {
     delay(100);
    length = Wire.available();
    for (int i = 0; i < length; i++) {
        if(i > 4){                       //prevent data corruption in case you accidentally receive more than the array can hold
            incoming[4] = '\0';  //put a null char at the end of the array
            break;
        }
        incoming[i] = Wire.receive();
        }
    }
...
Is this any good or should I start studying again  smiley-confuse

Let's work through what would happen if you got 5 bytes (which in fact you are sending from your test program).

You store the first 4 in "incoming" and then notice that you have more than 4 in the inner loop. But the outer loop still has Wire.available() == 1. So the outer loop keeps looping, and the inner loops keeps doing a "break". So this code will go into an infinite loop.

Why use two loops?
Logged


Southampton, UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

To be very honest, that 4 was just a test number.

Let me make it 128. Problem solved  smiley
Code:
char incoming[128];
char length;
This should do.
All right, the next issue.
Code:
void loop()
{
  Wire.beginTransmission (5);
  Wire.send ("G1");
  delay(1000);
  Wire.send ("G28");
  delay(1000);
  Wire.endTransmission();
}
Before we go any further, I am sensing that I might have done it wrong too (no surprises there  smiley-razz)
I don't want to send G1 and G28 at the same time or after a fixed interval. I want Arduino to know when G1 come (led ON) and when G28 come (led OFF), which will not happen according to what I have done here. So what should I do  smiley-confuse
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 484
Posts: 18764
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


Code:
void loop()
{
  Wire.beginTransmission (5);
  Wire.send ("G1");
  delay(1000);
  Wire.send ("G28");
  delay(1000);
  Wire.endTransmission();
}


Did you understand what I wrote above?

Quote
The Wire library only buffers up data and does the actual sending on the Wire.endTransmission ().

In light of that, what effect do you think delay(1000) will have?
Logged


Southampton, UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can I say something, I am having a feeling that I am back in school  smiley
All in good humor though  smiley

It should go like this
Code:
void loop()
{
  Wire.beginTransmission (5);
  Wire.send ("G1");
  Wire.endTransmission();
  Wire.send ("G28");
  Wire.endTransmission();
}
Is this correct ?
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 613
Posts: 49251
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Is this correct ?
No.

Wire.beginTransmission() and Wire.endTransmission() calls are used in pairs. One start, one end.
Logged

Southampton, UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void loop()
{
  Wire.beginTransmission (5);
  Wire.send ("G1");
  Wire.send ("G28");
  Wire.endTransmission();
}

Then I guess, like this, right !

« Last Edit: August 28, 2011, 10:02:45 am by Javaid » Logged

Southampton, UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The LED is still OFF, like it is not receiving anything  smiley-cry

What should I do  smiley-yell
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 613
Posts: 49251
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
What should I do
Gee, let me think for a minute.

Ooh, I know. POST YOUR CODE!!!
Logged

Southampton, UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Gee, let me think for a minute.

Ooh, I know. POST YOUR CODE!!!
Good idea. Why didn't I think of that  smiley-razz

This is in Reprap
Code:
#include <Wire.h>

//i2c address of the gen 6
int REP_RAP_ADDR = 4;
//my address
int CP_ADDR = 5;
void setup()
{  
  Wire.begin(4);
}
void loop()
{
  Wire.beginTransmission (5);
  Wire.send ("G1");
  Wire.endTransmission();
delay(1000);
Wire.beginTransmission (5);
  Wire.send ("G28");
  Wire.endTransmission();
delay(1000);
}
And this is in Arduino
Code:
#include <Wire.h>

//i2c address of the gen 6
int REP_RAP_ADDR = 4;
//my address
int CP_ADDR = 5;
int ledPin = 2;

void setup()
{
  Wire.begin(CP_ADDR);
  Wire.onReceive(receiveEvent); // register event so that anything received on i2c is sent to the serial
  pinMode(ledPin, OUTPUT);  
}

void receiveEvent(int howMany)
{
  char incoming[128];
  while(Wire.available() < howMany); // do nothing
    for (int i = 0; i < howMany; i++) {
        if(i < 128){                      
            incoming[i] = Wire.receive();  
            incoming[i+1] = '\0';
            Serial.print(incoming);}
        }
    }
    char str1[] = "G1";
 char str2[] = "G28";
  
  if (strcmp(str1, incoming) == 0){
      //turn dispenser on
      digitalWrite(2, HIGH); }
    
    if (strcmp(str2, incoming) == 0) {    
      //turn dispenser off
      digitalWrite(2, LOW);}
}
void loop()
{  
}
« Last Edit: August 28, 2011, 05:44:36 pm by Javaid » Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 613
Posts: 49251
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
        if(i > 128){                       //prevent data corruption in case you accidentally receive more than the array can hold
            incoming[128] = '\0';  //put a null char at the end of the array
So, if the array is full, write beyond the end of it. How's that working for you?

You are using strcmp () to compare what is in incoming to some fixed strings. The strcmp function is one of a family of string functions. Do you know what a string is?

I'll tell you. It's a NULL terminated array of characters. Look through your code. Where do you NULL terminate your array of characters?

If you don't NULL terminate the array, IT IS NOT A STRING (string), so you can't expect proper behavior of the strcmp function.

One of the arguments for the receiveEvent function is the number of characters to expect. They may not all have arrived by the time the function is called. Why are you not reading that number of characters?

Code:
  while(Wire.available() < howMany); // do nothing

Then, read the characters that Wire sent:
Code:
    for (int i = 0; i < howMany; i++)
    {
        if(i < 128)
        {
            incoming[i] = Wire.receive();
            incoming[i+1] = '\0';  //put a null char at the end of the array
       }
   }
Then, print what you got, and use it.
Logged

Southampton, UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So it should go like this, right !
I should edit the main code too.
Code:
void receiveEvent(int howMany)
{
  char incoming[128];
  while(Wire.available() < howMany); // do nothing
    for (int i = 0; i < howMany; i++) {
        if(i < 128){                      
            incoming[i] = Wire.receive();  
            incoming[i+1] = '\0';
            Serial.print(incoming);}
        }
    }
Before I write the strcmp(), I have a question to ask. G1 and G28 are g-codes that the Reprap will be sending to Arduino and they are in this form:
G28 X0 Y0
G1 X10 Y10

What do you suggest a proper way of comparing the incoming data would be ?
« Last Edit: August 28, 2011, 03:32:30 pm by Javaid » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26273
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if(i < 128){                      
            incoming[i] = Wire.receive();  
            incoming[i+1] = '\0';
Yous still don't have a "incoming [128]".

Can we ease-up on the "20 questions" style now please?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Southampton, UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

To be very honest, I never follow what you say. And I am pretty sure that the problem is on my end.

Will you please tell me where the problem is and where I went wrong (again  smiley-confuse)
Logged

Pages: 1 ... 4 5 [6] 7 8   Go Up
Jump to: