Hello,
I have this configuration:
- 2 led matrix 8*8 controlled by MAX7219
- 1 ultrasonic sensor HC-SR04
- a little servomotor not yet connected
- arduino uno
I want to measure a distance and print the results on the led matrixs.
Everything works fine without the code of the led matrix: i can mesure up to 500 cm and read good results on terminal monitor.
When I add the code of the led matrix, using MD_MAX72xx library the system works well but only for small distance, like < 20 cm. when i try to measure for example 150 cm it's showing only random numbers.
I suppose that there is a timer or interrupts problem between newping and MD_MAX72xx libraries
... any suggestion?
here is my complete code :
#define TRIGGER_PIN 3 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 5 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 500 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
int n=0;
#include <NewPing.h>
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
#include <VarSpeedServo.h>
VarSpeedServo myservo;
#include <MD_MAX72xx.h>
#define PRINT(s, v) { Serial.print(F(s)); Serial.print(v); }
// Define the number of devices we have in the chain and the hardware interface
// NOTE: These pin numbers will probably not work with your hardware and may
// need to be adapted
#define MAX_DEVICES 2
#define CLK_PIN 13 // or SCK
#define DATA_PIN 11 // or MOSI
#define CS_PIN 10 // or SS
//MD_MAX72XX mx = MD_MAX72XX(CS_PIN, MAX_DEVICES);
MD_MAX72XX mx = MD_MAX72XX(DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
// Text parameters
#define CHAR_SPACING 1 // pixels between characters
// Global message buffers shared by Serial and Scrolling functions
#define BUF_SIZE 40
char message[BUF_SIZE] = {"="};
bool newMessageAvailable = true;
int valore_servo_old =0;
int distanza_old = 1;
unsigned int spike=0;
void setup() {
Serial.begin(9600); // Open serial monitor at 115200 baud to see ping results.
myservo.attach(6);
mx.begin();
}
void loop() {
unsigned int uS = sonar.ping();
unsigned int distanza = uS / US_ROUNDTRIP_CM;
snprintf(message, sizeof(message), "%u", distanza);
printText(0, MAX_DEVICES-1, message);
// Serial.print("Distanza: ");
Serial.print(distanza); // Convert ping time to distance in cm and print result (0 = outside set distance range)
// Serial.print("cm - Distanza_old: ");
// Serial.println(distanza_old);
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
if (distanza==0)
{
distanza = distanza_old;
Serial.print("uno");
}
if (abs(distanza_old-distanza)>35 && spike>=1){
distanza_old = distanza;
spike=0;
Serial.print("due");
Serial.print(abs(distanza_old-distanza));
}
else if (abs(distanza_old-distanza)>35)
{
distanza = distanza_old;
spike++;
Serial.print("due incrementa");
Serial.print(abs(distanza_old-distanza));
}
unsigned int valore_servo = constrain(map(distanza, 15,80,170, 60),60,170);
// Serial.print(" Posizione Servo: ");
// Serial.print(valore_servo);
Serial.print("Distanza: ");
Serial.print(distanza); // Convert ping time to distance in cm and print result (0 = outside set distance range)
Serial.print("cm - Distanza_old: ");
Serial.print(distanza_old);
Serial.print(" spike : ");
Serial.println(spike);
myservo.write(valore_servo,50); // il secondo numero é la velocità, il massimo dovrebbe essere 255
}
void printText(uint8_t modStart, uint8_t modEnd, char *pMsg)
// Print the text string to the LED matrix modules specified.
// Message area is padded with blank columns after printing.
{
uint8_t state = 0;
uint8_t curLen;
uint16_t showLen;
uint8_t cBuf[8];
int16_t col = ((modEnd + 1) * COL_SIZE) - 1;
mx.control(modStart, modEnd, MD_MAX72XX::UPDATE, MD_MAX72XX::OFF);
do // finite state machine to print the characters in the space available
{
switch(state)
{
case 0: // Load the next character from the font table
// if we reached end of message, reset the message pointer
if (*pMsg == '\0')
{
showLen = col - (modEnd * COL_SIZE); // padding characters
state = 2;
break;
}
// retrieve the next character form the font file
showLen = mx.getChar(*pMsg++, sizeof(cBuf)/sizeof(cBuf[0]), cBuf);
curLen = 0;
state++;
// !! deliberately fall through to next state to start displaying
case 1: // display the next part of the character
mx.setColumn(col--, cBuf[curLen++]);
// done with font character, now display the space between chars
if (curLen == showLen)
{
showLen = CHAR_SPACING;
state = 2;
}
break;
case 2: // initialize state for displaying empty columns
curLen = 0;
state++;
// fall through
case 3: // display inter-character spacing or end of message padding (blank columns)
mx.setColumn(col--, 0);
curLen++;
if (curLen == showLen)
state = 0;
break;
default:
col = -1; // this definitely ends the do loop
}
} while (col >= (modStart * COL_SIZE));
mx.control(modStart, modEnd, MD_MAX72XX::UPDATE, MD_MAX72XX::ON);
}
thanks!