Keeping code organized

So I'm a newbie arduino programmer, but I'm slowly learning thanks to much of the information here.

I've done many of the tutorial sketches and understand 90+% of what's going on in all of them. I'm working on a couple home-brewed projects now, and trying to keep everything organized is becoming very difficult.

For example, one of my first projects is to build a bbq pit controller that will read a temperature from two k-type thermocouples, display the output on a 4 row LCD, and control a 12v fan based on a user defined temperature setting. Each of these things is fairly simple to accomplish - where I'm having problems is combining these sketches. I've read a bunch but don't completely understand the best way to do this.

Can anyone point me to a good resource that won't confuse me more?

Im no expert but try breaking down each particular job into a seperate function and call them from the main loop depending on the inputs you have.

So for example you have your temp sensors so I would do it like this (not real code, but you might get the idea)

int tempData = 0; //use this as a global varible that can be changed through your sketch 
int fanTemp = 120; //your kick limit as to when your fan kicks in



void getTempData() {

tempData = digitalRead(pin, data) //Not sure how you get the data but I guess its like this?

}

void toggleFan() {

if (tempData == fanTemp) { //compare the two if they're the same - turn on fan.

digitalWrite(fan, HIGH)

else {  //if they're not the same - turn off fan.
digitalWrite(fan, LOW) 
}

}



void loop() {

getTempData() //this will just keep running the above function just to grab data from your sensors 
                     //and stick the results into the variables (tempData & fanTemp)

toggleFan() //again it will just keep checking the two values 

lcd.print(tempData) //just print the temp data onto the lcd

}

Its not very brilliant but it'd work.

http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html

Also, have you read: http://forum.arduino.cc/index.php?topic=261445.0

Thanks for the replies so far.

Elboor: Im no expert but try breaking down each particular job into a seperate function and call them from the main loop depending on the inputs you have.

So for example you have your temp sensors so I would do it like this (not real code, but you might get the idea)

Its not very brilliant but it'd work.

That's sound advice which makes sense in a very basic sketch, but it gets interesting when each function is essentially a sketch in itself (to me at least). For example, I'm not using a simple on/off for the fan, but instead a PID algorithm that adjusts the PWM output to the fan to keep constant temps without overshooting the set temperature. Ultimately, I believe what you are saying is correct but the implementation screws me up after incorporating a couple different functions

groundfungus: http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html

I have not seen this, however I will read through it. After a quick skim, it seems that it may take a slightly different approach at tackling this.

LarryD: Also, have you read: http://forum.arduino.cc/index.php?topic=261445.0

I have read this thread at least twice, and started reading through it again just before posting. It takes a little bit to digest, but it seems to be a very methodical approach to writing complex code. Now if I can get my non-complex mind to comprehend the code, I'll be good :sunglasses:

I think one of the things that I'm getting hung up on is passing variable into a function. Often I have a function that defines a variable in which I need to use in another function. I've read up on this, but it still confuses me each time I encounter it.

One example is the following portions of my modified checktemp.ino sketch:

void printData(DeviceAddress deviceAddress)
{
  printTemperature(deviceAddress);
 }
void loop(void)
{ 
  // call sensors.requestTemperatures() to issue a global temperature 
  // request to all devices on the bus
  sensors.requestTemperatures();
  
  // print the device information
  Serial.print("Probe 1: ");
  printData(Probe1);
  Serial.print("Probe 2: ");
  printData(Probe2);
  Serial.println();
}

DeviceAddress is defined as:

DeviceAddress Probe1, Probe2;

however deviceAddress doesn't seem to be defined, but is used throughout the example sketch that I have modified. Maybe that's part of the #includes of that sketch??

It's the details like this that I'd like to streamline so they make more sense (to me at least).

That's where OO principles shine. In an OO implementation, you would have created Probe1 and Probe2, objects of a probe class, and to print the parameters you would:

Probe1.print();
Probe2.print();

It would be easy to remember, because many classes have print methods like that.