Using Millis instead of delay as a time seperator.

I was referencing http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1231884359 to be able to use a ping sensor and a led matrix at the same time.

I tried working the listed code into my program and it doesn’t seem to work.

Here is the code, let me know if you need anything else…

//We always have to include the library
#include "LedControl.h"

/*
 Now we need a LedControl to work with.
 ***** These pin numbers will probably not work with your hardware *****
 pin 12 is connected to the DataIn 
 pin 11 is connected to the CLK 
 pin 10 is connected to LOAD 
 We have only a single MAX72XX.
 */
LedControl lc=LedControl(12,11,10,2);

int movementDelay = 150;
int breathingPauseDelay = 1000;
int movementDelayModifier = 30;
long inDelay, outDelay;

// this constant won't change.  It's the pin number
// of the sensor's output:
const int pingPin = 7;

void setup() {

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

  inDelay = millis();
  outDelay = millis();

  /*
   The MAX72XX is in power-saving mode on startup,
   we have to do a wakeup call
   */
  lc.shutdown(0,false);
  /* Set the brightness to a medium values */
  lc.setIntensity(0,5);
  /* and clear the display */
  lc.clearDisplay(0);

  lc.shutdown(1,false);
  /* Set the brightness to a medium values */
  lc.setIntensity(1,5);
  /* and clear the display */
  lc.clearDisplay(1);
}



void loop() { 

  //ping sensor

  // establish variables for duration of the ping, 
  // and the distance result in inches and centimeters:
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  Serial.print(0xff, BYTE); // Sync byte
  Serial.print((inches >> 8) & 0xff, BYTE);
  Serial.print(inches & 0xff, BYTE); 


  delay(200);

  //led matrix

  delay(breathingPauseDelay);

  breathIn();
  breathOut();  

}

void breathIn() {

  if (inDelay > millis()) {

    lc.setColumn(0, 0, 24);
    lc.setColumn(1, 7, 24);

    inDelay = millis() + (movementDelay + movementDelayModifier);
  }

  if (inDelay > millis()) {
    lc.setColumn(0, 1, 60);
    lc.setColumn(1, 6, 60);

    inDelay = millis() + (movementDelay + movementDelayModifier*2);
  }

  if (inDelay > millis()) {
    lc.setColumn(0, 2, 126);
    lc.setColumn(1, 5, 126);

    inDelay = millis() + (movementDelay + movementDelayModifier*3);
  }

  if (inDelay > millis()) {
    lc.setColumn(0, 3, 255);
    lc.setColumn(1, 4, 255);

    inDelay = millis() + (movementDelay + movementDelayModifier*4);
  }

  if (inDelay > millis()) {
    lc.setColumn(0, 4, 255);
    lc.setColumn(1, 3, 255);

    inDelay = millis() + (movementDelay + movementDelayModifier*5);
  }

  if (inDelay > millis()) {
    lc.setColumn(0, 5, 255);
    lc.setColumn(1, 2, 255);

    inDelay = (movementDelay + movementDelayModifier*6);
  }

  if (inDelay > millis()) {
    lc.setColumn(0, 6, 255);
    lc.setColumn(1, 1, 255);

    inDelay = millis() + (movementDelay + movementDelayModifier*7);
  }

  if (inDelay > millis()) {
    lc.setColumn(0, 7, 126);
    lc.setColumn(1, 0, 126);

    inDelay = millis() + (movementDelay + movementDelayModifier*8);
  }

}

void breathOut() {
  
  if(outDelay > millis()) {
  lc.setColumn(0, 7, 0);
  lc.setColumn(1, 0, 0);

  outDelay = millis() + (movementDelay + movementDelayModifier*8);
  }

  if(outDelay > millis()) {
  lc.setColumn(0, 6, 0);
  lc.setColumn(1, 1, 0);

  outDelay = millis() + (movementDelay + movementDelayModifier*7);
  }

  if(outDelay > millis()) {
  lc.setColumn(0, 5, 0);
  lc.setColumn(1, 2, 0);

  outDelay = millis() + (movementDelay + movementDelayModifier*6);
  }

  if(outDelay > millis()) {
  lc.setColumn(0, 4, 0);
  lc.setColumn(1, 3, 0);

  outDelay = millis() + (movementDelay + movementDelayModifier*5);
  }

  if(outDelay > millis()) {
  lc.setColumn(0, 3, 0);
  lc.setColumn(1, 4, 0);

  outDelay = millis() + (movementDelay + movementDelayModifier*4);
  }

  if(outDelay > millis()) {
  lc.setColumn(0, 2, 0);
  lc.setColumn(1, 5, 0);

  outDelay = millis() + (movementDelay + movementDelayModifier*3);
  }

  if(outDelay > millis()) {
  lc.setColumn(0, 1, 0);
  lc.setColumn(1, 6, 0);

   outDelay = millis() + (movementDelay + movementDelayModifier*2);
  }

  if(outDelay > millis()) {
  lc.setColumn(0, 0, 0);
  lc.setColumn(1, 7, 0);

  outDelay = millis() + (movementDelay + movementDelayModifier);
  }

}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance traveled.
  return microseconds / 29 / 2;
}

Is that the complete listing? I ask because the MAX72xx chips usually need libraries like wire.h or spi.h to work, unless LEDcontrol.h is providing that.
Which chip are you using?
Also, the variables associate with millis usually need to be of type
unsigned long
so that math manipulation (by subtraction, later minus earlier time) yields correct results.

I believe that ledcontrol.h is supplying this, because the led matrix works perfectly when I replace everything with delays. But then the ping sensor does not work because it is waiting too long.

I replaced all the variables for delay with unsigned long and it still does nothing.

The ping sensor works fine, but either the breathIn(); and out methods are never run, or the timing is not working because nothing happens on the matrix.

Thanks for the input though,

gregsd

There might be flawed logic here -

You give inDelay a value, you run a bunch of code & delays in void loop, that at the loop you call breathIn

Then in breathIn, you comare inDelay to millis(); millis at this point would be quite a bit larger than inDelay void breathIn() {

if (inDelay > millis()) {

so it doesn't seem to me that any of those comparisons will ever pass. You could sprinkle in some print statements and confirm that.

I figured it may have something to do with the methods.

I will look into that.

I tried running print statements but it gave me an error stating that I was trying to access a constant variables or something similar. I will try it again and report back the error.

Thanks,

gregsd

When I try,

Serial.print("inDelay = " + inDelay + " outDelay = " + outDelay + " millis = " + millis());

It says, invalid operands of types 'const char' and ' const char [13] to binary 'operator+'.

Thanks

gregsd

http://arduino.cc/en/Serial/Print

Your syntax for the print statement is non-sense to a C-complier. You are telling it to add a string character array to an integer.

The Arduino's Serial.print() is different from ANSI C's printf(). As you can see in the reference page for Serial.print(), it can only print one variable at a time.

Ah, yes I'm used to java. I forgot about that.

Thank you.

This is what I have at the moment, it seems as though the in and out delay variables are running at the same time, presumably because I cannot treat them the same as the delay function.

//We always have to include the library
#include "LedControl.h"

/*
 Now we need a LedControl to work with.
 ***** These pin numbers will probably not work with your hardware *****
 pin 12 is connected to the DataIn 
 pin 11 is connected to the CLK 
 pin 10 is connected to LOAD 
 We have only a single MAX72XX.
 */
LedControl lc=LedControl(12,11,10,2);

unsigned long movementDelay = 150;
unsigned long breathingPauseDelay = 1000;
unsigned long movementDelayModifier = 30;
unsigned long inDelay1 = 0, inDelay2 = 0, inDelay3 = 0, inDelay4 = 0, inDelay5 = 0, inDelay6 = 0, inDelay7 = 0, inDelay8 = 0;
unsigned long outDelay1 = 0, outDelay2 = 0, outDelay3 = 0, outDelay4 = 0, outDelay5 = 0, outDelay6 = 0, outDelay7 = 0, outDelay8 = 0;

// this constant won't change.  It's the pin number
// of the sensor's output:
const int pingPin = 7;

void setup() {

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

  /*
   The MAX72XX is in power-saving mode on startup,
   we have to do a wakeup call
   */
  lc.shutdown(0,false);
  /* Set the brightness to a medium values */
  lc.setIntensity(0,5);
  /* and clear the display */
  lc.clearDisplay(0);

  lc.shutdown(1,false);
  /* Set the brightness to a medium values */
  lc.setIntensity(1,5);
  /* and clear the display */
  lc.clearDisplay(1);
}



void loop() { 

  //ping sensor

  // establish variables for duration of the ping, 
  // and the distance result in inches and centimeters:
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  //  Serial.print(0xff, BYTE); // Sync byte
  //  Serial.print((inches >> 8) & 0xff, BYTE);
  //  Serial.print(inches & 0xff, BYTE); 


  delay(200);

  //led matrix

  delay(breathingPauseDelay);
  
  breathIn();
  breathOut();
  
  Serial.print("inDelay1 = ");
  Serial.print(inDelay1);
  Serial.println(" ");

  Serial.print("outDelay1 = ");
  Serial.print(outDelay1);
  Serial.println(" ");
  
  Serial.print("millis = ");
  Serial.println(millis());
  Serial.println(" ");
  
    Serial.print("inDelay8 = ");
  Serial.print(inDelay8);
  Serial.println(" ");

  Serial.print("outDelay8 = ");
  Serial.print(outDelay8);
  Serial.println(" ");
  
  Serial.print("millis = ");
  Serial.println(millis());
  Serial.println(" ");
  
}

void breathIn() {
  if (millis() >= inDelay1) {

    lc.setColumn(0, 0, 24);
    lc.setColumn(1, 7, 24);

    inDelay1 = millis() + (movementDelay + movementDelayModifier);
  }

  if (millis() >= inDelay2) {
    lc.setColumn(0, 1, 60);
    lc.setColumn(1, 6, 60);

    inDelay2 = millis() + (movementDelay + movementDelayModifier*2);
  }

  if (millis() >= inDelay3) {
    lc.setColumn(0, 2, 126);
    lc.setColumn(1, 5, 126);

    inDelay3 = millis() + (movementDelay + movementDelayModifier*3);
  }

  if (millis() >= inDelay4) {
    lc.setColumn(0, 3, 255);
    lc.setColumn(1, 4, 255);

    inDelay4 = millis() + (movementDelay + movementDelayModifier*4);
  }

  if (millis() >= inDelay5) {
    lc.setColumn(0, 4, 255);
    lc.setColumn(1, 3, 255);

    inDelay5 = millis() + (movementDelay + movementDelayModifier*5);
  }

  if (millis() >= inDelay6) {
    lc.setColumn(0, 5, 255);
    lc.setColumn(1, 2, 255);

    inDelay6 = millis() + (movementDelay + movementDelayModifier*6);
  }

  if (millis() >= inDelay7) {
    lc.setColumn(0, 6, 255);
    lc.setColumn(1, 1, 255);

    inDelay7 = millis() + (movementDelay + movementDelayModifier*7);
  }

  if (millis() >= inDelay8) {
    lc.setColumn(0, 7, 126);
    lc.setColumn(1, 0, 126);

    inDelay8 = millis() + (movementDelay + movementDelayModifier*8);
  }
}

void breathOut() {

  if (millis() >= outDelay1) {
    lc.setColumn(0, 7, 0);
    lc.setColumn(1, 0, 0);

    outDelay1 = millis() + (movementDelay + movementDelayModifier*8);
  }

  if (millis() >= outDelay2) {
    lc.setColumn(0, 6, 0);
    lc.setColumn(1, 1, 0);

    outDelay2 = millis() + (movementDelay + movementDelayModifier*7);
  }

  if (millis() >= outDelay3) {
    lc.setColumn(0, 5, 0);
    lc.setColumn(1, 2, 0);

    outDelay3 = millis() + (movementDelay + movementDelayModifier*6);
  }

  if (millis() >= outDelay4) {
    lc.setColumn(0, 4, 0);
    lc.setColumn(1, 3, 0);

    outDelay4 = millis() + (movementDelay + movementDelayModifier*5);
  }

  if (millis() >= outDelay5) {
    lc.setColumn(0, 3, 0);
    lc.setColumn(1, 4, 0);

    outDelay5 = millis() + (movementDelay + movementDelayModifier*4);
  }

  if (millis() >= outDelay6) {
    lc.setColumn(0, 2, 0);
    lc.setColumn(1, 5, 0);

    outDelay6 = millis() + (movementDelay + movementDelayModifier*3);
  }

  if (millis() >= outDelay7) {
    lc.setColumn(0, 1, 0);
    lc.setColumn(1, 6, 0);

    outDelay7 = millis() + (movementDelay + movementDelayModifier*2);
  }

  if (millis() >= outDelay8) {
    lc.setColumn(0, 0, 0);
    lc.setColumn(1, 7, 0);

    outDelay8 = millis() + (movementDelay + movementDelayModifier);
  }

}


long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

Right now, if I run the same code without the millis it works perfect:

And the serial console prints out:

inDelay1 = 1420 outDelay1 = 1699 millis = 1416

inDelay8 = 1690 outDelay8 = 1550 millis = 1473

and it is blinking the whole pattern at once. Which makes sense based on the numbers.

What direction should I take to fix this?

-gregsd