Help bashing code

Hey All,

I'm a newbie that is trying to understand all of this Arduino stuff, and would like to build a "monitoring system only", for my stock 1931 Ford Model "A". The systems overall functions itself isn't really important at this point in time as it will be evolving as I learn how all of this Arduino stuff works.

I thought I'd start out small. You have to learn how to crawl before you can walk, and eventually lean how to run.

I have a Temperature sensor sketch that works just fine with (10) DS18B20 sensors but I'd like to data log the information onto a SD card. I have compiled some components to either use the Mega board or a ESP32 board. I'd like to add the SD card reader and if possible, the NEO06 GPS unit as well so all the information gets written to the SD card and monitor.

Elegoo Mega 2560 v3

MELIFE ESP32
https://a.co/d/4Btbmtk

HiLetGo HW-125 SD Card Adapter
https://a.co/d/7I8ZlfF

GY-NEO6MV2 GPS
https://a.co/d/d2vevpS

Is this something a newbie could handle, because I've tried and there and I'm still scratching my head. I can read schematics and build the stuff, but the programming, Is my weak spot.

Here is the DS18B20 that I can get to work and would like to add the logger into it, and if possible, the GPS as well.

Thank You for reading this thread.
Regards
Bill

#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

// Addresses of 3 DS18B20s
uint8_t sensor1[8] = { 0x28, 0xF5, 0x4D, 0x49, 0xF6, 0x14, 0x3C, 0x67 };
uint8_t sensor2[8] = { 0x28, 0xA9, 0x79, 0x49, 0xF6, 0x36, 0x3C, 0xFF };
uint8_t sensor3[8] = { 0x28, 0x67, 0xDE, 0x49, 0xF6, 0x5F, 0x3C, 0xF9 };
uint8_t sensor4[8] = { 0x28, 0x07, 0x76, 0x49, 0xF6, 0x88, 0x3C, 0x01 };
uint8_t sensor5[8] = { 0x28, 0x52, 0x0F, 0x49, 0xF6, 0xCC, 0x3C, 0xFA };
uint8_t sensor6[8] = { 0x28, 0xD8, 0x29, 0x49, 0xF6, 0xC7, 0x3C, 0x98 };
uint8_t sensor7[8] = { 0x28, 0xA7, 0x6B, 0x49, 0xF6, 0xE2, 0x3C, 0x70 };
uint8_t sensor8[8] = { 0x28, 0xE7, 0x88, 0x49, 0xF6, 0xC5, 0x3C, 0x3E };
uint8_t sensor9[8] = { 0x28, 0xAC, 0x4A, 0x49, 0xF6, 0xD1, 0x3C, 0xC5 };
uint8_t sensor10[8] = { 0x28, 0x3F, 0x84, 0x49, 0xF6, 0x1D, 0x3C, 0x54 };


void setup(void)
{
  Serial.begin(9600);
  sensors.begin();
}

void loop(void)
{
  sensors.requestTemperatures();
  
  Serial.print("Sensor 1: ");
  printTemperature(sensor1);
  
  Serial.print("Sensor 2: ");
  printTemperature(sensor2);
  
  Serial.print("Sensor 3: ");
  printTemperature(sensor3);

  Serial.print("Sensor 4: ");
  printTemperature(sensor4);

  Serial.print("Sensor 5: ");
  printTemperature(sensor5);

  Serial.print("Sensor 6: ");
  printTemperature(sensor6);

  Serial.print("Sensor 7: ");
  printTemperature(sensor7);

  Serial.print("Sensor 8: ");
  printTemperature(sensor8);

  Serial.print("Sensor 9: ");
  printTemperature(sensor9);

  Serial.print("Sensor 10: ");
  printTemperature(sensor10);
  
  Serial.println();
  delay(1000);
}

void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
 // Serial.print(tempC);
 // Serial.print((char)176);
  //Serial.print("C  |  ");
  Serial.print(DallasTemperature::toFahrenheit(tempC));
  //Serial.print((char)176);
  Serial.println("F");
}

That’s a lot of temperatures to monitor - seems kind of weird to know so much about such a primitive engine. I suppose it would be nice to have warnings of potential problems.

Certainly doable, but here’s another vote for arrays!

1 Like

As long as we are voting, I too vote for arrays.

@billcnc, you may see know you already have arrays.

In C/C++ you can make an array of any data type, including… arrays.

So you'll end up with a two dimensional array

// Addresses of  eight DS18B20s
uint8_t sensor[8][8] = {
  { 0x28, 0xF5, 0x4D, 0x49, 0xF6, 0x14, 0x3C, 0x67 },
  { 0x28, 0xA9, 0x79, 0x49, 0xF6, 0x36, 0x3C, 0xFF },
  { 0x28, 0x67, 0xDE, 0x49, 0xF6, 0x5F, 0x3C, 0xF9 },
  { 0x28, 0x07, 0x76, 0x49, 0xF6, 0x88, 0x3C, 0x01 },
  { 0x28, 0x52, 0x0F, 0x49, 0xF6, 0xCC, 0x3C, 0xFA },
  { 0x28, 0xD8, 0x29, 0x49, 0xF6, 0xC7, 0x3C, 0x98 },
  { 0x28, 0xA7, 0x6B, 0x49, 0xF6, 0xE2, 0x3C, 0x70 },
  { 0x28, 0xE7, 0x88, 0x49, 0xF6, 0xC5, 0x3C, 0x3E },
  { 0x28, 0xAC, 0x4A, 0x49, 0xF6, 0xD1, 0x3C, 0xC5 },
  { 0x28, 0x3F, 0x84, 0x49, 0xF6, 0x1D, 0x3C, 0x54 }
};

and be able to say things like

  Serial.print("Sensor 1: ");
  printTemperature(sensor[1]);

Now I am testing the idea that the quickest way to get the right answer on the internets is to post a wrong answer… I am sure this is right, but cannot test it from where I am just now.

Arrays work very nicely with loops, my example should be

  Serial.print("Sensor ");
  Serial.print(N);
  Serial.print(" reading is ");
  printTemperature(sensor[N]);

Again, this will be expanded on or slam/corrected soon. :wink:

Arrays and loops work very well with functions, should you be tempted to copy/paste/edit code nine times to make ten sensors work...

a7

1 Like

As requested :stuck_out_tongue_closed_eyes:

too many initializers for 'uint8_t [8][8] {aka unsigned char [8][8]}'

Better:

uint8_t sensor[10][8] = {
2 Likes

Thanks Everyone,

I'll have to look into what arrays are for coding, CAD work yes, coding, not yet. Currently I'm looking at a few data logging sketches to try and bash into this sketch but so far, it's not working.

How does one know were to add the bashed coding. I've notice that when placing it in different areas of the sketch, it give different errors.

The errors at this point are not necessarily a bad thig as it gives me some puzzles to figure out until I realize I'm not going to figure it out soon, so I scrap that one and try another.

Success through failure.

Regards
Bill

Haha, THX.

Even better:

unsigned char sensor[][8] = {
// blah blah
// blah blah
};

const int nSensors = sizeof sensor / sizeof sensor[0];

Let the compiler count them and never use 10 anywhere you mean to ble talking about how many.

And while we're at it

  for (int N = 0; N < nSensors; N++) {
    Serial.print("Sensor ");
    Serial.print(N);
    Serial.print(" reading is ");
    printTemperature(sensor[N]);
}

@billcnc get used to ppl using zero as a perfectly good place to start numbering things. Or add one when you print N and lie to anyone who needs things to be number 1, 2, 3 and so forth.

a7

1 Like

An alternate expression someone here recently turned us on to, me anyway, is

const int nSensors = sizeof sensor / sizeof *sensor;

which I like very much for some reason. So THX, whoever.

It makes sense with the pointer/array brilliance of C/C++.

a7

1 Like

Thank a7,

I'll research what you have posted. It's still kind of Greek to me.

Anyhow, I think I might be getting somewhere as this bash has the least amount of errors so far. and one part I'm unsure of what to do.

The part of the code that I'm unsure of is swaping out the 3 original temp sensors for my 10.

The bashed code so far is:

#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#include <SD.h>

// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

// Addresses of 10 DS18B20s
uint8_t sensor1[8] = { 0x28, 0xF5, 0x4D, 0x49, 0xF6, 0x14, 0x3C, 0x67 }; 
uint8_t sensor2[8] = { 0x28, 0xA9, 0x79, 0x49, 0xF6, 0x36, 0x3C, 0xFF };
uint8_t sensor3[8] = { 0x28, 0x67, 0xDE, 0x49, 0xF6, 0x5F, 0x3C, 0xF9 };
uint8_t sensor4[8] = { 0x28, 0x07, 0x76, 0x49, 0xF6, 0x88, 0x3C, 0x01 };
uint8_t sensor5[8] = { 0x28, 0x52, 0x0F, 0x49, 0xF6, 0xCC, 0x3C, 0xFA };
uint8_t sensor6[8] = { 0x28, 0xD8, 0x29, 0x49, 0xF6, 0xC7, 0x3C, 0x98 };
uint8_t sensor7[8] = { 0x28, 0xA7, 0x6B, 0x49, 0xF6, 0xE2, 0x3C, 0x70 };
uint8_t sensor8[8] = { 0x28, 0xE7, 0x88, 0x49, 0xF6, 0xC5, 0x3C, 0x3E };
uint8_t sensor9[8] = { 0x28, 0xAC, 0x4A, 0x49, 0xF6, 0xD1, 0x3C, 0xC5 };
uint8_t sensor10[8] = { 0x28, 0x3F, 0x84, 0x49, 0xF6, 0x1D, 0x3C, 0x54 };

// ---------------- Beginning of added SD sketch --------------------------
const int chipSelect = 4;
// const int chipSelect = 4; // possibly use for Mega board
// ---------------- End of added SD sketch --------------------------


void setup(void)
{
  Serial.begin(9600);
  sensors.begin();

 // Open serial communications and wait for port to open:
  Serial.begin(9600);

// ---------------- Beginning of added SD sketch --------------------------
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only


  Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) { 
    // if (!SD.begin()) { // possibly use this instead ove above for Mega Board 
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1);
  }
  Serial.println("card initialized.");
  // ---------------- End of added SD sketch --------------------------

}

void loop(void)
{
  sensors.requestTemperatures();
  
  Serial.print("ENG - HTF: ");
  printTemperature(sensor1);
  
  Serial.print("ENG - HTR: ");
  printTemperature(sensor2);
  
  Serial.print("RAD - Top: ");
  printTemperature(sensor3);

  Serial.print("RAD - Bot: ");
  printTemperature(sensor4);

  Serial.print("RAD - OVF: ");
  printTemperature(sensor5);

  Serial.print("ENG - Oil: ");
  printTemperature(sensor6);

  Serial.print("TRS - Oil: ");
  printTemperature(sensor7);

  Serial.print("DIF - Oil: ");
  printTemperature(sensor8);

  Serial.print("Out - Air: ");
  printTemperature(sensor9);

  Serial.print(" IN - Air: ");
  printTemperature(sensor10);
  
  Serial.println();
  delay(5000);

  // ---------------- Beginning of added SD sketch --------------------------
  // make a string for assembling the data to log:
  String dataString = "";

  // read three sensors and append to the string:
  for (int analogPin = 0; analogPin < 3; analogPin++) {
    int sensor = analogRead(analogPin);
    dataString += String(sensor);
    if (analogPin < 2) {
      dataString += ",";
    }
  }

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.println(dataString);
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }
  // ---------------- End of added SD sketch --------------------------

}

void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
 // Serial.print(tempC);
 // Serial.print((char)176);
  Serial.print("  =  ");
  Serial.print(DallasTemperature::toFahrenheit(tempC));
  //Serial.print((char)176);
  //Serial.print(char(176)); // I added
  Serial.println(" F");
}

The part I could use help on is swapping out the sketches original temps for my temps:

  String dataString = "";

  // read three sensors and append to the string:
  for (int analogPin = 0; analogPin < 3; analogPin++) {
    int sensor = analogRead(analogPin);
    dataString += String(sensor);
    if (analogPin < 2) {
      dataString += ",";
    }

Thanks Again
Regards
Bill

Thanks wildbill,

Would I just swap out that one line for all 10 of the others?

Regards
Bill

Here is the original SD sketch that is from the SD library data logging example just in case it's needed to try to figure things out.

Regards
Bill

/*
  SD card datalogger

  This example shows how to log data from three analog sensors
  to an SD card using the SD library.

  The circuit:
   analog sensors on analog ins 0, 1, and 2
   SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN) // possibly change to 53 for Mega board

  created  24 Nov 2010
  modified 9 Apr 2012
  by Tom Igoe

  This example code is in the public domain.

*/

#include <SPI.h>
#include <SD.h>

const int chipSelect = 4;
// const int chipSelect = 4; // possibly use for Mega board

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) { 
    // if (!SD.begin()) { // possibly use this instead ove above for Mega Board 
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1);
  }
  Serial.println("card initialized.");
}

void loop() {
  // make a string for assembling the data to log:
  String dataString = "";

  // read three sensors and append to the string:
  for (int analogPin = 0; analogPin < 3; analogPin++) {
    int sensor = analogRead(analogPin);
    dataString += String(sensor);
    if (analogPin < 2) {
      dataString += ",";
    }
  }

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.println(dataString);
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }
}

+1, and a few other things that usually come along pretty quick in any disciplined march through a well designed course of study.

It is so hard to do, but every minute you spend not seeking immediate gratification but rather getting just a few relatively simple concepts to the point where you can read, understand and soon enough write using them will be returned to you, what would you say, Mr. Spock, what would the factor be?

Probably several orders of magnitude, binary orders certainly, decimal orders maybe even.

Learning by doing is fine, but to really bash, which I'll assume is what I refer to as hacking, it is so much more fun when you kinda sorta know what you are doing at the line by line level.

a7

1 Like

Hey Guy's

You do make valid points, but I'm the guy that learns as I do the research and the task at the same time. So breaking the code just to see what happens is just to dang tempting. I'm knocking on 63 and as long as I can remember, I've always been the guy to say, "gee, I wonder what would happen if", and I have the all scars to prove it.

"BASH"
To me, it means taking two different working sketches (or anything) and bashing them together. Call it bash, hack, blend, zip or join. The purpose is to tweak them here just to see the changes. For me it's easier to understand the research that I'm reading. Since I'm starting off with two known working sketches, bashing them let's me see how it all works or more importantly, it doesn't. Now I have puzzle to solve as well as my research. Right now, I don't really care if my code is slow, crude or 5 miles long as long as it works. From there, I will make it more efficient with the knowledge that I have since gained.

I'm not trying to rub anyone the wrong way here, it's just a me thing, sorry.

Regards
Bill

OK, the irrational exuberance of youth. S'plains it all! :wink:

a7

1 Like

So here's a fish, uncooked.

  // read three sensors and append to the string:
  for (int analogPin = 0; analogPin < 3; analogPin++) {
    int sensor = analogRead(analogPin);
    dataString += String(sensor);
    if (analogPin < 2) {
      dataString += ",";
    }
  }

Instead of a loop of three, make it a loop of nSensors, or just use 10.

Instead of concatenating integers that come from analogRead()s of whatever sensor were attached to the analog inputs, use the temperatures that your DS devices return.

See the for loop version I wrote for your consideration. Use the same pattern, just substitute building up dataString as done in the code above for the printing the N loop thing does as I left it.

Imma bet you can bash that.

Capital S strings are a possible source of trouble on the Mega. From what ppl here say, it isn't, or is less so on the other board you mentioned.

I do not use them. I cannot even say how the problems they may be would manifest. But the guy whose code you are "bashing" wrote it that way, and you are without the ability to judge for yourself the wisdom of going 3.3333333 times further down that road. It is probable that it is worse by more than that factor.

Good luck! You got this.

a7

1 Like

Making progress but I'm having a hard time trying to find a list describing what stuff like this is and how it works.

","

It is being used here.

dataString += ","

Is this seen as 1, 2, or 3 different characters. What is this symbol called and what does it do? Also, does anyone have any links to describe things like this?

Regards
Bill

It's basic C++ syntax. This:

dataString +=

Is a shorthand for:

dataString = dataString +

and this:

","

is a c-string literal. It's a two-byte character array. The first byte is the ASCII code for the comma character and the second byte is the terminating null. So, this:

dataString += ","

Is shorthand for concatenating a comma character to dataString:

dataString = dataString + ","

BUT IT'S STILL INCORRECT SYNTAX because it's missing the ending semicolon:

dataString = dataString + ",";
1 Like

Oh boy, obviously I am offending you.

Sorry, but I have not seen that explained before with all 3 in that order. Not at any site I've been to, or video I have seen which is a lot BTW in the last 24 hours otherwise I would not have asked it here. Delta_G apparently you do have serious issues with me and the way I learn new tasks, so please feel free to consider me just a crazy old guy talking to the clouds and just ignore this thread. Just because something doesn't work for you, doesn't mean it doesn't work for others. If you cant help someone because of it, then don't. It's how I learned HTML in 1995 and it made it a breeze. An HTML book and breaking existing code explains how things are in text and visually in the real world. Visual learning has a bigger imprint to your memory and it also reinforces what the book is saying.

All you needed to do was say was... and I thank you for that.

That symbol is a comma. It is being added to some string of text. It usually represents a place in a sentence where one should pause a little. It means nothing in terms of computer programming. It is just a symbol being added to a string of other symbols for a human to read.

Regards
Bill

Exactly. You started with a book and at least knew what a tag was before you started breaking things. This is considerably more complicated than html.

Wrong, I was tweaking HTML before the book HTML 2.0 and the Java had just been released and the moving water was a game changer so I bought books to better understand HTML and Java. Back then it was all new to everyone.

The fact that someone had to tell you that is PROOF that your method of learning is already failing you. You can't rely on someone to tell you how every single little piece of syntax works.

Wrong again, it's proof that I have not read nor seen it explained before. And YES, it's as simple as that.

I suppose you would know more about how to learn to code than someone who has taught so many. You are certainly free to ignore my advice. But please don't take it as offence. I just hate to see people fail, so I try to warn them about the pitfalls. I don't understand why people get offended that i tell them there's a weak spot in the bridge. You say to me, "Screw you I'll step where I want". Ok, fine. Fall through then. Doesn't hurt me. But how have I harmed you by warning you that it is there? Tell me that.

First off, condescending much?
Okay, I guess someone should. Basically your saying is, if someone doesn't learn the same way I do, or approve of, then your stupid.

Was that simple enough?

Regards
Bill

I have to admit, I did a record breaking eyes-roll when I read your question.

There is no "list". Stop looking for one, and don't be surprised if when you run out the patience of ppl here answering questions like those.

The original loop you were "bashing" was commented, at the very least you could have googled for the terms those comments employed, if not any that were used elsewhere in ppls' responses.

a7

a7

I HAVE been researching everything everyone is posting here. Heck, even the question I asked isn't even going to be used in my sketch, It was all stripped out and I'm going with an array. I've never seen that chicken scratch being explained before. A simple question ended up is a very non-productive issue.

This is a semicolon

;

This is not

','

I was asking what the latter was.

Regards
Bill