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;
}