Arduino to read and send signal at the same time?

Hi,

I’m very new to programming but I think I can complete this project with some advice from you gurus. I’ll do my best to explain my project.

I’m working on an automatic printer project. I am sending a print signal from the arduino to the printer. While the printer is printing, it will send an output saying that it is busy. Once the busy signal is over, I need to turn a stepper motor. The motor cannot turn while the printer is busy. The printer has a quantity of prints input on it, and will automatically stop printing once that quantity is reached.

I wrote a program that will fire a relay to signal the printer to print, then wait 1 second, then turn the motor. I have several problems that I cannot figure out because I’m a super newb.

1.) I think the delays in my program are preventing me from counting how many times the print signal has cycled. I removed the delays and everything worked better but it’s too fast for the relay to latch and the motor will move during printing. With the delays on there, the counter only increments when I push the manual print button on the printer, but will not increment when the relay signals the printer. I believe this to be because the delays or because the arduino can’t multitask.

2.) I need to time different between the High and Low signals from the printer. That way I can stop the loop by saying something like “if there is no state change in 3 seconds, stop firing relaying and turning motor” because that means the printer has printed the quantity the user input.

Is there anyway to constantly read the state of the Printer Signal regardless of the delays?
I tried my best to explain this, please let me know if I can provide anymore info.

Print_and_Index_Loop.ino (2.66 KB)

s there anyway to constantly read the state of the Printer Signal regardless of the delays?

Yes
There is an example called BWD (BlinkWithoutDelay) that comes with the IDE you should look at.
Also there is an excellent discussion by Robin2 you should also review.

Once you read these, try to incorporate the ideas.
If you need help, lots here will help out.

// if the state has changed, increment the counter
if (sigState == LOW)
. . .
else
{
// if the current state is LOW then the button <<<<< do you mean HIGH ?
. . .

You can use the </> icon “code tags” to show us your code.

#include <Stepper.h>

int STEPS = 1600;
Stepper stepper(STEPS,2,3);


int LED = 24;
int BUTTON = 22;
int RELAY = 32;
int stepPin = 2;
int printSig = 40;  
int sigLED = 10;

int sigCounter = 0;
int sigState = 0;
int lastSigState = 0;


/**********************************************************************************************************/
/***********************************************************************************************************/
/***********************************************************************************************************/

void setup() 
{
  pinMode(LED,OUTPUT);         
  pinMode(BUTTON,INPUT);
  pinMode(stepPin, OUTPUT);
  pinMode(RELAY, OUTPUT);
  pinMode(printSig, INPUT);
  pinMode(sigLED, OUTPUT);


  // initialize serial communication:
  Serial.begin(9600);
}


/***********************************************************************************************************/
/***********************************************************************************************************/
/***********************************************************************************************************/


void loop()
{

  sigState = digitalRead(printSig);

  // compare the buttonState to its previous state
  if (sigState != lastSigState) 
  {
    // if the state has changed, increment the counter
    if (sigState == LOW) 
    {
      // if the current state is HIGH then the button
      // went from off to on:
      sigCounter++;
      Serial.println("ON");
      Serial.print("number of button pushes:  ");
      Serial.println(sigCounter);
      digitalWrite(sigLED, LOW);      //LED to indicate if the print signal is being recognized

    }
    else 
    {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("OFF");
      digitalWrite(sigLED, HIGH);
    }
    // Delay a little bit to avoid bouncing
    delay(10);
  }
  // save the current state as the last state,
  //for next time through the loop
  lastSigState = sigState;



  /***********************************************************************************************************/
  /***********************************************************************************************************/

  if(digitalRead(BUTTON) == HIGH)   //Print , Wait , Index
  {

    digitalWrite(RELAY, HIGH);            //Energize relay
    delay(50);                            //delay to latch relay
    digitalWrite(RELAY, LOW);             
    delay(1000);                          //Wait 1 second while printer is printing
    stepper.step(1600);                   // Step 1600
    stepper.setSpeed(250); 
  }



  if(digitalRead(BUTTON) == LOW)
  {
    digitalWrite(RELAY, LOW);
    stepper.step(0);
  }

}

Also have you used “CTRL T”
It formats your code so it reads better.