Go Down

Topic: Declaring digital signal (Read 7 times) previous topic - next topic

Javaid

It was just a figurative speech  :smiley-red:
I am sorry if I have offended you or any poster in any way. It wasn't my intention.

Javaid

#31
Aug 17, 2011, 03:25 am Last Edit: Aug 17, 2011, 03:36 am by Javaid Reason: 1
All right, this is a very crappy representation, I agree.

Reprap has Gen 6 and its I2C has 4 pins which I have labelled. I have connected 5V of gen 6 with 5V of Arduino, GND with GND, SDA (Gen 6) with pin 4 of Arduino, SCL with pin 5 of Arduino. These are the connections for I2C. According to my current setup, Arduino is the Master. It can send G Code to Reprap according to the code that I posted in the earlier post. But I have not tried sending anything from Reprap to Arduino though...
I also connected the 5V and GND of Arduino to the breadboard.
Then MCP4921 chip is attached to the breadboard. Pin 8 is the output pin where I have attached a LED.
Pin 1       5V
Pin 2       Pin 10 of Arduino
Pin 3       Pin 13 of Arduino
Pin 4       Pin 11 of Arduino
Pin 5       GND
Pin 6       5V
Pin 7       GND
Pin 8       Output (LED connected)

For digital signal, I have connected a LED with pin 4 of Arduino.
Please, if anything is unclear, do let me know :)

Nick Gammon

Right, well forgetting about the code for a moment, how is the Arduino supposed to know the reprap is printing? It needs to know, right, so it can dispense the powder?

Since the Arduino is the I2C master it can tell the reprap things, but how does it know the reprap is printing?
http://www.gammon.com.au/electronics

Javaid

#33
Aug 17, 2011, 03:13 pm Last Edit: Aug 17, 2011, 03:42 pm by Javaid Reason: 1
Quote

Since the Arduino is the I2C master it can tell the reprap things, but how does it know the reprap is printing?

This is the question that has given me a lot of sleepless nights but I still don't have an answer for this  :~

I tried including requestEvent from Slave but I am not so sure how I can tell when the reprap (Slave) is printing to Arduino (Master).

Any suggestions ?

Nick Gammon

I see we have come to the core of the problem. :)

I don't own a reprap, although I would like to, but from what I can gather the controller board controls 4 motors: x, y, z and stock feed.

Now there must be a digital signal from the reprap board into a transistor or something that turns the stock feed motor. You could either grab the digital signal (whatever pin that is) or the actual signal to the motor (which might be too high a voltage, I don't have the circuit here).

It would make sense to me to detect that whenever the reprap board sends a message to the feeder motor (ie. turn on and feed) that this is the time to dispense feed pellets. So you already have a digital signal, you don't need to detect I2C messages.
http://www.gammon.com.au/electronics

Javaid

I will check out the circuit diagram and let you know in a bit.

In the meantime, would you please tell me how I can send some data from Reprap to Arduino. I am trying to make both Arduino and Reprap to be able to send and request data. I tried doing this, just want to be sure before uploading that it will work.
This is the code in Reprap
Code: [Select]

#include <Wire.h>

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

void receiveEvent(int howMany)
{

}

void requestEvent()
{
  Wire.send("start");
}

void setup()
{ Wire.begin(4);
  Wire.onReceive(receiveEvent); // register event
  Wire.onRequest(requestEvent); // register event
}
void loop()
{
  Wire.beginTransmission (CP_ADDR);
  Wire.send ("javaid");
  Wire.endTransmission ();
  delay (1000);
}

This is in Arduino.
Code: [Select]

#include <Wire.h>
int ledPin = 4;

volatile long last_received_time;
//i2c address of the gen 6
int REP_RAP_ADDR = 4;
//my address
int CP_ADDR = 5;

void receiveEvent(int howMany)
{
  while(0 < Wire.available()) // loop through all
  {
    char c = Wire.receive(); // receive byte as a character
    Serial.print(c);         // print the character   
    last_received_time = millis ();  // remember when we last got something   
  }
}

void sendGCode(char* GCode)
{
  Wire.beginTransmission(REP_RAP_ADDR);
  Wire.send(GCode);
  Wire.endTransmission();   
}

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

  //Test gcode, this should send the machine's X, Y and Z to home
  sendGCode("G28 X0 Y0 Z0\n");
}

void loop()
{
// light LED if we received data in the last second

  if (millis () - last_received_time < 1000)  // if something in the last second
    digitalWrite(ledPin, HIGH);
  else
    digitalWrite(ledPin, LOW);

  // any other stuff
 
}

Let me know if there is any discrepancy here.

Nick Gammon

http://www.gammon.com.au/electronics

Javaid

All right then. Next step  :)
I think, I got the motor pin. It is pin 43 (EXTR EN) on the atmega and then the motor driver enable pin is 26 (IC8 just on the bottom right of atmega).
Correct me if I am wrong about the pins  :)

This might sound silly but how to proceed from here  :~

Nick Gammon

I'm not sure I really understand the problem. You have a digital signal which tells you if the motor is running, and you want to know if the motor is running?
http://www.gammon.com.au/electronics

Javaid

Actually, I want Reprap to send a signal to Arduino whenever the motor starts running and turn a LED ON. But that seems to be a bit of a problem as attaching anything to the specified pin on the Gen 6 board will be messy.

Anyways, I hope you remember the diagram where I showed all the connections of Arduino, breadboard etc. I have a question in that context.

1- The Arduino has 1 digital and 1 analogue signal, how can I tell Reprap that there is a digital and an analogue signal present at the Arduino?
2- Can I send certain values to those signals like sending 1 or 0 at the digital output ?

Javaid

#40
Aug 26, 2011, 07:22 pm Last Edit: Aug 27, 2011, 01:31 am by Javaid Reason: 1
I have been up and down the code and I think that now I have a question that won't cause any confusion (I hope) :)

Reprap (Gen 6 or 3D printer) has some G Codes in the program (like G1, G28, G90 etc) that allows the movement of the axes, in the process_g_code file.
Code: [Select]

http://forums.reprap.org/read.php?13,92469


I just want to send a signal whenever a G Code is executed, like when G1 is executed send 1 to the Arduino and light the LED, when G28 is executed send 0 to the Arduino and turn the LED off.

I have this code in Reprap
Code: [Select]

#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.send ("G28");
}


And this in Arduino.
Code: [Select]

#include <Wire.h>

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

void receiveEvent(int howMany)
{
 while(0 < Wire.available()) // loop through all
   Wire.receive();  
}

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 loop()
{
if ("G1")
   {
     //turn dispenser on
     digitalWrite(2, HIGH);
   }
   if ("G28")
   {
     //turn dispenser off
     digitalWrite(2, LOW);
   }
}


The problem is that the LED is continuously ON. Any suggestions here.

PaulS

Code: [Select]
if ("G1")
An if test generally compares two things. If a equals b, do this. If c greater than d, do that.

When only one value is used, "equals true" is implied.

Since false is 0, and non-zero is true, your test always evaluates to true, since "G1" is not 0.

Javaid

I should change that to
Code: [Select]

void loop()
{
if ("G1")
   
      //turn dispenser on
      digitalWrite(2, HIGH);
   
    else if ("G28")
   
      //turn dispenser off
      digitalWrite(2, LOW);
    else
    delay(100);
}

There was no need to add the last else with the delay but this is is the structure of nested if-else statements (according to the internet :P).
I hope this is correct now and since you haven't mentioned anything else, I am guessing that I will get the desired result with this coding.


AWOL

Quote
if ("G1")

Is always true.
You need a strcmp.
"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.

Javaid

Will you please guide me through this because quite frankly this is the first time I have read strcmp  :~

Go Up