Hi guys, I'm building a 3D LIDAR system. Basically my program will send back a string contains coordination to terminal every 4ms (E.g 180;360;400 cm). After running for a couple of seconds, the Serial.print stop working, it just hang there on Arduino Serial monitor, while everything else is working normally. After doing some research on the forum, I suspect it happened because I have overload the SRAM. Is there any way I can fix it? .Thank you guys for your time.
#include <Servo.h>
#include <Arduino_FreeRTOS.h>
#include <LIDARLite.h>
#include <Wire.h>
#include <nema.h>
#define PIN_SCL A5
#define PIN_SDA A4
NEMA NEMA(3,4,5,2,8,7,6);
LIDARLite myLidarLite;
Servo myservo;
uint8_t interval = 2;
uint32_t timer=0;
int AngleV=180;
uint16_t pulsecount;
uint8_t circle=0;
uint16_t distance = 0;
float AngleH = 0;
bool print = 0;
bool out = 0;
/*
0x04 quarter speed 1 400
0x04 quarter speed 2 400
0x04 half speed 1 200
0x04 half speed 2 200
*/
void setup() {
Serial.begin(115200);
while (!Serial)
{
}
Serial.println("System is online");
// Stepper Setup
NEMA.StepperSetup(2);
NEMA.StepperDIR(false);
// Servo Setup
myservo.attach(10,544,2500);
//SG90 560 1530 2500
//MG90 550 1375 2200
myservo.writeMicroseconds(550);
delay(1000);
myservo.detach();
// Lidar Setup
myLidarLite.begin(0);
myLidarLite.configure(2);
myLidarLite.beginContinuous(true,0x04,0xff,0x62);
pinMode(11, INPUT);
}
void loop() {
ScanningSequences();
//uint8_t c;
//while(1)
//{
//if (Serial.available()>0)
// {
// {
// // read input character ...
// c = (uint8_t) Serial.read();
//
// // ... and parse
// switch (c)
// {
// case 'B':
// case 'b':\
// ScanningSequences();
// Serial.println("Scanning Complete");
// break;
// case 'R':
// case 'r':
// RestartScan();
// ScanningSequences();
// Serial.println("Scanning Complete");
// break;
//
// default:
// Serial.println("=====================================");
// Serial.println("== Type a single character command ==");
// Serial.println("=====================================");
// Serial.println(" B - Begin Scanning Sequences");
// Serial.println(" R - Restart Scan");
//
// break;
// }
// }
//}
//}
}
void ScanningSequences(void)
{
for (;AngleV>=75;)
{
NEMA.StepperRun(1);
NEMA.StepperENA();
if (millis() - timer > interval)
{
timer = millis();
CalculateOutput();
print = 0;
if(distance>=4000)
{
goto skiperror;
}
print = 1;
if (print == 1)
{
uint16_t (AngleH) = pulsecount*9;
Serial.println(AngleV);
Serial.print(",");
Serial.print(AngleH);
Serial.print(",");
Serial.println(distance);
print = 0;
}
}
skiperror:;
}
}
void CalculateOutput(void)
{
myLidarLite.write(0x00,0x04,0x62);
distance = (myLidarLite.distanceContinuous())-1320;
pulsecount = pulsecount+1;
if (pulsecount > 400)
{
circle=circle+1;
pulsecount -= 400;
}
if (circle>2)
{
AngleV--;
float ds = 2200-(180-AngleV)*55/6;
myservo.writeMicroseconds(uint16_t(ds));
circle=0;
}
// DEBUG PULSE COUNT///////////////////////////////////////////////////////////////////////////////////
// if ((distance < 30) && (distance > 5) && (out == 1))
// {
// out = 0;
// Serial.print("so xung cho 1 vong la: ");
// Serial.println(pulsecount);
// pulsecount = 0;
// }
// else if ((distance > 40) && (out == 0))
// {
// out = 1;
// }
///////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////I2c hang prevention module/////////////////////////////////////////////////////////////
// if (digitalRead(PIN_SCL) == HIGH && digitalRead(PIN_SDA) == LOW) //Reset Program if I2C hang
// {
//
// pinMode(A4, OUTPUT); // is connected to SDA
// digitalWrite(18, LOW);
// pinMode(A4, INPUT); // reset pin
// }
///////////////////////////////////////////////////////////////////////////////////////////////////////
}
void RestartScan()
{
timer=0;
AngleV=180;
pulsecount=0;
circle=0;
distance = 0;
AngleH = 0;
print = 0;
out = 0;
}
LVTN.ino (4.03 KB)