Project Sonar-V

Hi everyone.

This is my first Arduino project and my very first C code, so be nice wih me. :slight_smile:

Motivation:
To learn C/C++ and Eletronics.

What it does:
Uses a Stepper and an Ultrasonic range finder to scan an arc for moving objects.

Hardware:
1- Arduino (UNO R3)
2- Steeper (28BYJ-48 - Pins IN1:8, IN2:10, IN3:9, IN4:11 - 5V)
3- Ultrasonic range finder (HC-SRD4 - Pins E:12, T:13 - 5V)
4- Buzzer (Pin 3)

Sketch:
Arduino-Sonar-V

Pic


Today when I was finishing up the code, it started to output garbled content within the text. Before I touch something it was working fine. The only big thing that changed was that I have updated the Arduino IDE from 1.0.x to 1.6.8.
If someone can point me why it is output garbled content, I appreciate.

As you are asking for help, I'll move this to Programming Questions.

If someone can point me why it is output garbled content, I appreciate.

Output to where? If the output is to the Serial Monitor application, the settings for that app are not maintained from one version of the IDE to another. Check that the Serial Monitor app has the correct baud rate set.

Hi,
Is the baud rate on your monitor set to 9600 as in the sketch?

Tom... :slight_smile:

ethraza:
If someone can point me why it is output garbled content, I appreciate.

For somebody like me, who never ever uses dynamically allocated 'String' objects in any of his sketches, some of your 'String' handling looks very weird.

So after you know that I hardly know anything about 'String'. what about doing a formally more strictly 'String' handling, perhaps instead:

 Serial.println(motorPos + txt + countPos + txt + l + txt + r + txt + lrDiff + txt + motorDir);

use something like

 Serial.println(String(motorPos) + txt + String(countPos) + txt + String("l") + txt + String(r) + txt + String(lrDiff) + txt + String(motorDir));

I don't know if that's your actual problem.
(All I know is that such programming with huge amounts of 'String' objects instead of using nullterminated strings eats up RAM quickly) during runtime.

Hey guys, thanks for the replies.

Yep, the app and the dropdown option of Monitor serial are set to 9600.
I tryed String(variables) as well, but nothing changed. The garbage seens to happen with the txt variable.

It starts well and (seens like, not sure) after the motorDir variable (the 6th one) becomes negative (-1),
the txt variables (' : ') goes wild:

0:0:0:40:40:1
20:1:0:40:40:1
40:2:0:40:40:1
60:3:0:41:41:1
80:4:0:41:41:1
100:5:0:40:40:1
120:6:0:41:41:1
1407040401
120640400-1
100540400-1
80440400-1

After some minutes, it gets back to output as " : ", after some seconds it goes wild again and stays in this "loop" forever.
Replace the txt variable with " : " literal seens to fix the problem, but it does not look like a fix.

I was hopping to make this output a JSON in the future, but if a single ":" gets like this, I wonder what a bunch of then will do to the output.
How do you guys think I should output the collected data?

With JavaScript I would get this ready in a heart beat, with C it's... kind of strange. :slight_smile:

Thanks.

Hi,

Hardware:
1- Arduino (UNO R3)
2- Steeper (28BYJ-48 - Pins IN1:8, IN2:10, IN3:9, IN4:11 - 5V)
3- Ultrasonic range finder (HC-SRD4 - Pins E:12, T:13 - 5V)
4- Buzzer (Pin 3)

Even though you have said that you have had it running, can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png or pdf?

Have you tried reloading with the older IDE again to see if it still works?

Tom... :slight_smile:

It starts well

How can you tell? You have no clue what is being printed. Anonymous printing sucks. Stop doing that!

TomGeorge:
Hi,Even though you have said that you have had it running, can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png or pdf?

Have you tried reloading with the older IDE again to see if it still works?

Tom... :slight_smile:

I know it's not the same as schematics, but I found that a real picture was easier and funny to do... I've just add it to Git Readme.

I wanna format this String output as JSON now. I'll not use any libraries, since it's just a few values, I gonna manually format it.
But how is the best and memory efficient way to that in C?

Hi,

I know it's not the same as schematics, but I found that a real picture was easier and funny to do... I've just add it to Git Readme.

Sorry, but the forum has all the facilities you need, please do not post pictures and schematics off site.

Please use REPLY rather than QUICK REPLY and it has an attachment facility, so you can post your pictures and schematics as an attachment.

Can you please repost them here.

Thanke.. Tom... :slight_smile:

But how is the best and memory efficient way to that in C?

While using the String class? That is not possible.

@TomGeorge
Ok, sorry. But I added the pic to the 1st post so everyone can see it when come in. The original pic is very high res and I make sure one can see where all the cables are plugged in.

@PaulS
You got me now! I was just hopping to be able to output a JSON formated string to the Serial so I can read it with NodeJS or something to use it elsewhere. Just want to output to the Serial because I don't have an Ethernet or Wifi shield.
There is a good way to output such string to the Serial or the very fact of output any strings is what kills the idea?

There is a good way to output such string to the Serial

Yes, use sprintf(). For a list of all the formatting options, see this page.

jremington:
Yes, use sprintf(). For a list of all the formatting options, see this page.

Thank you for pointing me in the right direction. I'm now streaming the output to Serial with printf.

I guess I'm happy with this little first project for now.
Now I gonna wait for my new NodeMCU unit to arrive. It can be an Arduino Wifi module, it can be an Espruino unit... lots of fun ahead.

Thanks everyone.