Hello everyone
I recently finished work on a data logger and I thought to share the code.Am a noob and completing this project has given me immense satisfaction.Suggestions and criticisms are welcome
CODE.txt (104 KB)
Hello everyone
I recently finished work on a data logger and I thought to share the code.Am a noob and completing this project has given me immense satisfaction.Suggestions and criticisms are welcome
CODE.txt (104 KB)
How about a plain text version?
Rob
This version is 100% plain text !
You mean a code only version that runs in the IDE, without all the color tags....
You mean a code only version that runs in the IDE, without all the color tags....
Yep, I don't know what package formats that but certainly nothing I normally use.
Rob
Am sorry, I shall post the plain text.I have not commented my code yet.However the basic idea is there are four switches to control combinations of four sensors whose data can be logged on to a text file(via a processing code,readily available upon googling but I shall include it here for your convenience) by a switch controlled by the variable"switchpin". The program is also designed to log data at intervals of 10ms,10^2 ms,10^3 ms,10^4 ms,10^5 ms respectively.This code is not perfect yet, as it is not fast enough for precise timing needs.Does anybody know how to change baud rate settings on the arduino as it is reading junk at higher baud rates(>9600).
CODE.txt (39.3 KB)
Processing code.txt (419 Bytes)
Where to begin, the code is nearly 1800 lines long and it does bugger all, there is certainly room for improvement
For starters, whenever you have multiple blocks of code that are almost the same think about creating a function. EG
if(time==100000){
   count1++;
   count++;
   delay(99998);
   originalmillis=count1;
    Serial.print(count);
  Serial.print(") ");
  Serial.print("sensor1 value=");
  Serial.print(volts);
  Serial.print(",");
  Serial.print("sensor2 value=");
  Serial.print(volts1);
  Serial.print(",");
  Serial.print("sensor3 value=");
  Serial.print(volts2);
  Serial.print(",");
  Serial.print("sensor4 value=");
  Serial.print(volts3);
  Serial.print(",");
  Serial.print("time=");
  Serial.print(originalmillis);
  Serial.print("*100000");
Serial.println(".");
 }
This appears 5 times and the only difference I can see is the delay() parameter. So.
if(time==10)
  printStuff (8);
if(time==100)
  printStuff (98);
if(time==1000)
  printStuff (998);
if(time==10000)
  printStuff (9998);
if(time==100000)
  printStuff (999998);
}
void printStuff ( unsigned long delayValue) {
   count1++;
   count++;
   delay(delayValue);
   originalmillis=count1;
    Serial.print(count);
  Serial.print(") ");
  Serial.print("sensor1 value=");
  Serial.print(volts);
  Serial.print(",");
  Serial.print("sensor2 value=");
  Serial.print(volts1);
  Serial.print(",");
  Serial.print("sensor3 value=");
  Serial.print(volts2);
  Serial.print(",");
  Serial.print("sensor4 value=");
  Serial.print(volts3);
  Serial.print(",");
  Serial.print("time=");
  Serial.print(originalmillis);
  Serial.print("*100000");
Serial.println(".");
 }
That would remove about 100 lines or more of repetitive code.
The same thing occurs in all the logDataxx() functions.
There's a LOT more that could be done but that's a start.
Rob
Even better, the delay() value is always time - 2 so
if(time==10)
  printStuff (8);
if(time==100)
  printStuff (98);
if(time==1000)
  printStuff (998);
if(time==10000)
  printStuff (9998);
if(time==100000)
  printStuff (999998);
}
can be
printStuff (time-2);
No need for all the if blocks at all.
Rob
What's with all the delay(00) and delay(0)?
Rob
code too long to check.
Be aware that declaring a number as an int gives it the range -32768 - +32767, numbers larger should be long. Also constants sould have the flag L so write 100000L iso 100000 to advice the compiler how to interpret the numbers.
further - the functions void logDataX( int value1)
can all be rewritten into one function
void logData(int X, int value1)
as most code is identical
@all- Thank you for your suggestions.I concur that this code is far too long and that I need to create functions ,which am working on now.However, could someone be kind enough to let me know how to change the baud rate settings as my serial monitor is reading junk.
how to change the baud rate settings as my serial monitor is reading junk.
Have a look down at the bottom right corner of the serial monitor, there's a combo box there with the baud rates.
Rob
Thank you so much for the information.Correct me if am wrong but will increasing the baud rate effectively increase how fast your code outputs data onto the serial monitor?If this is totally wrong , what does increasing baud rate actually do to a piece of code?
With higher baudrates the time spent to communicate texts is (far) less so there is more time todo some computing...
Thank you Rob and one more question.Currently am enclosing my arduino in a metal box with switches and LEDs on the outside.I wanted to know how I can code a hard reset for the arduino.Am thinking about providing a switch ,which when high resets the arduino.I did search the previous posts on this but could only obtain answers for soft resets.
I wanted to know how I can code a hard reset for the arduino
You can't really. You can get close with a jump to address zero, but it doesn't reset the Amtel chip. If you need a true reset, you'll need some external chippery such as a 555 to get it done, or a mechanical arrangement so that the reset switch on your box can activate the arduino's reset switch. This question comes up fairly frequently, so you can search the forums for more detail, but that's the usual consensus.
I wanted to know how I can code a hard reset for the arduino.Am thinking about providing a switch.
Use the switch to switch the power that is as hard as it can get....