First project. Be gentle ;) Looking for advice on improving sketch, feasibility

Hey guys,

New to the forums. New to arduino. I did a couple basic raspberry pi diys with ambilight using hyperion. It was a blast. Since iv’e looked into a few different applications i could use a micro controller for and i have one iv’e been contemplating for a while. Here’s the gist

Computer custom water-cooling loop utilizing a secondary loop for added TEC cooling

The purpose of my arduino project is to look at two temp sensors and decide which solenoids should be open/closed as a result of the temp sensors. Now this project has 8 temp sensors in it… but only two are needed for the logic, the other 6 are simply readouts for my purposes. Hopefully later down the road i can display all of these sensor’s information onto a TFT shield.

Here’s the logic. If my CPU temp (water out to be exact, NOT actual core temp) sensor reads a temp that is lower than ambient temp, my logic needs to say, open bypass valve, close radiator loop valve. And vise versa, if the CPU temp is over ambient, open radiator valve, close bypass valve.

Some may wonder why i would even want to do this. For the sake of avoiding those questions, i’m using TEC to cool water to a sub ambient temp. I do not know how efficient this will be yet, its possible the processor will not heat the chilled water back up to ambient even. In this case i want to skip my radiator (as this will only rise my water temp back up to ambient).

Also i will have a setting to completely shut off the TEC’s off, and to just continue using the radiator loop. Hence i think this is a pretty simple, round-about way of accomplishing both of those things.

I can elaborate more if you need. Here’s my parts list for the code i have.

-Arduino Mega 2560

-Water-valve solenoid’s (2)

-TIP120 Darlington Transistor (2)

-1kohm resistor (2)

-1N4001 Diode (2)

-TMP36’s (8)

I’m going to post my sketch below- I think the man readables are good to decipher what i’m doing.

My biggest question for improvement is; how can i take the code that is converting the analog signals into voltage, and then into C, and then into F, all one function? I feel like this would be a simple function but that is where i need to continue to learn with python. Functions confuse the hell out of me sometimes.

P.S. The “freeze” int i put in there was just to toy around with logic on turning off the check altogether. Ultimately that would become my user input on the TFT once i finish testing this out.

Thanks for any comments guys!

/*
 modified 2 June 2016
 by Anthony Kuban
*/

//Solenoid outputs 

int solenoid1Pin = 4;
int solenoid2Pin = 5;

//Temp sensor inputs

int ambiPin = 6;
int cpuPin = 7;
int tankPin = 8;
int radPin = 9;
int tec1Pin = 10;
int tec2Pin = 11;
int tec3Pin = 12;
int tec4Pin = 13;
int freeze = false;  //tec cooling on/off int


// the setup function runs once when you press reset or power the board
void setup() {
 // initialize digital pins 4,5 as an output.
 
 Serial.begin(115200); //sets up serial port 
 
 pinMode(solenoid1Pin, OUTPUT);  //Sets pin as output 
 pinMode(solenoid2Pin, OUTPUT);  //Sets pin as output 

 digitalWrite(solenoid1Pin, HIGH);  //opening radiator valve
 digitalWrite(solenoid2Pin, LOW);   //closing bypass valve

 freeze = true; 
 
 
}


// the loop function runs over and over again forever
void loop() {
 
 //Sets var to read analog for temp sensors
 
 int sens1Reading = analogRead(ambiPin); 
 int sens2Reading = analogRead(cpuPin); 
 int sens3Reading = analogRead(tankPin); 
 int sens4Reading = analogRead(radPin); 
 int sens5Reading = analogRead(tec1Pin); 
 int sens6Reading = analogRead(tec2Pin); 
 int sens7Reading = analogRead(tec3Pin); 
 int sens8Reading = analogRead(tec4Pin); 

 //Converts analog signal to voltage 

 float voltage1 = sens1Reading * 5.0;       
 voltage1 /= 1024.0; 

 float voltage2 = sens2Reading * 5.0;
 voltage2 /= 1024.0;

 float voltage3 = sens3Reading * 5.0;
 voltage3 /= 1024.0;

 float voltage4 = sens4Reading * 5.0;
 voltage4 /= 1024.0;

 float voltage5 = sens5Reading * 5.0;
 voltage5 /= 1024.0;

 float voltage6 = sens6Reading * 5.0;
 voltage6 /= 1024.0;

 float voltage7 = sens7Reading * 5.0;
 voltage7 /= 1024.0;

 float voltage8 = sens8Reading * 5.0;
 voltage8 /= 1024.0;

 //Converts voltage to tempC

 float temperatureC1 = (voltage1 - 0.5) * 100 ;

 float temperatureC2 = (voltage2 - 0.5) * 100 ;

 float temperatureC3 = (voltage3 - 0.5) * 100 ;

 float temperatureC4 = (voltage4 - 0.5) * 100 ;

 float temperatureC5 = (voltage5 - 0.5) * 100 ;

 float temperatureC6 = (voltage6 - 0.5) * 100 ;

 float temperatureC7 = (voltage7 - 0.5) * 100 ;

 float temperatureC8 = (voltage8 - 0.5) * 100 ;

 //Converts tempC to tempF 

 float temperatureF1 = (temperatureC1 * 9.0 / 5.0) + 32.0;

 float temperatureF2 = (temperatureC2 * 9.0 / 5.0) + 32.0;

 float temperatureF3 = (temperatureC3 * 9.0 / 5.0) + 32.0;

 float temperatureF4 = (temperatureC4 * 9.0 / 5.0) + 32.0;

 float temperatureF5 = (temperatureC5 * 9.0 / 5.0) + 32.0;

 float temperatureF6 = (temperatureC6 * 9.0 / 5.0) + 32.0;

 float temperatureF7 = (temperatureC7 * 9.0 / 5.0) + 32.0;

 float temperatureF8 = (temperatureC8 * 9.0 / 5.0) + 32.0;

 Serial.print(temperatureF1); Serial.println(" degrees F");   //Test prints temp sensor 1 in faranheit

 
 if (freeze = true)
 
   if (temperatureF2 < temperatureF1)        //checks to see if cpu hotside temp is cooler than ambiant (no need for radiator loop) 
     digitalWrite(solenoid2Pin, HIGH);     //opens bypass valve
     digitalWrite(solenoid1Pin, LOW);      //closes radiator inlet 
   
   if (temperatureF1 < temperatureF2)        //checks to see if ambiant is cooler than cpu hot side (need radiator loop back) 
     digitalWrite(solenoid1Pin, HIGH);     //opens radiator valve
     digitalWrite(solenoid2Pin, LOW);      //closes bypass valve 
     

}

All of those numbered variables begs for the use of arrays. You could put all of the temperature reading and conversion into one for loop.

Are pins 6 through 13 analog inputs on a Mega?

Also, your pin numbers consume twice as much precious RAM as they ought.
Instead of "int", they should be "const byte".

groundfungus:
All of those numbered variables begs for the use of arrays. You could put all of the temperature reading and conversion into one for loop.

Are pins 6 through 13 analog inputs on a Mega?

Sorry super new with the hardware.

If i understand all of the pin-out documentation correctly, yes?

0-15 i believe. So 16 total analog-in.

I could definitely see an array working. All of that pasted code seems redundant to me even if i don't know the correct way :stuck_out_tongue:

I'll dig and do some reading on arrays, thanks!

AWOL:
Also, your pin numbers consume twice as much precious RAM as they ought.
Instead of "int", they should be "const byte".

You know after reading a few threads i saw a lot of people using 'const int' on the pin assignment! Though i didn't see 'const byte'. Could you explain the difference?

"int" two bytes, good for positive and negative pin numbers in the range -32768 to +32767 ((that's a lot of pins)
"byte" one byte, good for pin numbers in the range 0 to 255 (still a lot of pins)

AWOL:
"int" two bytes, good for positive and negative pin numbers in the range -32768 to +32767 ((that's a lot of pins)
"byte" one byte, good for pin numbers in the range 0 to 255 (still a lot of pins)

Haha perfect!

So my int will simply go from:

//Solenoid outputs

int solenoid1Pin = 4;
int solenoid2Pin = 5;

//Temp sensor inputs

int ambiPin = 6;
int cpuPin = 7;
int tankPin = 8;
int radPin = 9;
int tec1Pin = 10;
int tec2Pin = 11;
int tec3Pin = 12;
int tec4Pin = 13;
int freeze = false; //tec cooling on/off int

To:

//Solenoid outputs

const byte solenoid1Pin = 4;
const byte solenoid2Pin = 5;

//Temp sensor inputs

const byte ambiPin = 6;
const byte cpuPin = 7;
const byte tankPin = 8;
const byte radPin = 9;
const byte tec1Pin = 10;
const byte tec2Pin = 11;
const byte tec3Pin = 12;
const byte tec4Pin = 13;

Thanks ill do that when assigning pins from now on!

I'll take a crack at the for/array tonight. I think i have a good understanding of how it needs to work. Amazon should be dropping off my massive parts kit any minute, so i should be able to test this guy out on a basic level.

You might find that temp readings with this sketch are power supply dependent (= unstable).
When all other problems are sorted, switch to the inbuild 2.56volt Aref of the Mega.
Averaging and/or smoothing could further stabilise temp readings.
Calculation from A/D value to tempF could be done on one line.
Silly to go from A/D, to voltage, to tempC, to tempF.
Leo..

Thanks for the reply Leo!

I revised my conversion code. I think i have it down to one line, could you confirm?

Can i keep an analog read like this?

float temperatureF1 = (((((analogRead(ambiPin) * 5.0)/1024) - 0.5) * 100) * 9.0 / 5.0) + 32.0;

I think that's doing the math if it can be placed this way. Also didn't know if i need to add an additional set of () to have it be like this....

float temperatureF1 = ((((((analogRead(ambiPin)) * 5.0)/1024) - 0.5) * 100) * 9.0 / 5.0) + 32.0;

I didn't get the TMP36's today but i did get some DS18B20 digital temp sensors with 4.7k resistors to run them. Either way i would still like to know what you think about the above code!

I think the first line is ok, but I’m not a good coder.
Someone else might have to confirm this.
9.0/5.0 can be replaced by *1.8

If you add this line in void setup()

analogReference(INTERNAL2V56); // switch to 2.56volt Aref, Mega only!!

AND change the maths line to: …(analogRead(ambiPin) * 2.56)/1024)…

Then you are measuring with a more stable reference voltage.
Not needed ofcourse for the (easier to use) digital DS18B20.
Leo…

Wawa:
I think the first line is ok, but I'm not a good coder.
Someone else might have to confirm this.
9.0/5.0 can be replaced by *1.8
Leo..

Won't make any difference. The compiler should simplify all that math for you anyway. Sometimes leaving it written out the long way makes it easier to see where the numbers come from.

Hi,

Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html
then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks... Tom... :slight_smile:

I added the code tags. So it should be easier to read now :slight_smile: Before i get into the wiring diagram i need to make a few updates here as some hardware has changed, and a few experiments have necessitated a slight design change.

First and foremost i got my Arduino mega on Friday afternoon. I was not able to acquire TMP36’s but thanks to amazon i was able to get 5 of the Ds18b20. My god are those incredibly easy to work with. In less than 10 minutes i had 2 sensors up and running kicking me temp data. Even nicer is they only absorb one PWM pin. So naturally my program has changed. Also still don’t have the Transistors yet for my solenoids.

I added a sainsmart 3.2" TFT to my arduino using an Elecfreaks rev 2.2 shield. Despite all the troubles i read people have with them. Worked perfectly on the first fire up. Couldn’t be happier.

So currently i’m attempting to write an HMI for the TFT shield. Also was able to test my Peltier TEC in conjunction with my DS18b20. Wow was that thing impressive. Chilling the hot side down to about 60F i was able to squeeze all the way down to 1.04F or -17.2C on the cold side. My VAT was water, which froze solid, and kept the temp at a higher 28F. After switching the VAT fluid over to 50% glycol, it quickly dropped below that.

So my game plan is to get the HMI roughly outlined, and from there stick my logic within the HMI loops.

I’ll post up some schematics and code progress tonight! Would have last night but we had a nice power outage that stopped me from getting any work done, and even made me lose an hour of unsaved work :frowning: