Beginner Arduino loop function and variable -


this revised code, initial value reading ridiculously high without conversion!! how conversion apply initialtemp temperaturec?

the code have helped far can tell trying achieve, obviously.

thanks again!!

int pin_tempread = 0;   // temperature sensor pin int coolled = 2; // cooling led digital pin int heatled = 3; // heating led digital pin float initialtemp; float cutofftemp = 30; //cut off temperature = 30°c  void setup() {   serial.begin(9600);  //start serial connection computer   pinmode(heatled, output); //initialise output   pinmode(coolled, output); //initialise output   initialtemp = analogread(pin_tempread);   // read initial temp   serial.print("initial temperature: "); serial.print(initialtemp); serial.println("c"); //prints out starting temperature }  void loop()                     // run on , on again {   //getting voltage reading temperature sensor   float current_temp = analogread(pin_tempread);    // converting reading voltage   float voltage = current_temp * 5.0; voltage /= 1024.0;    float temperaturec = (voltage - 0.5) * 100 ;  //converting 10 mv per degree 500 mv offset   //to degrees ((voltage - 500mv) times 100)      if(temperaturec > cutofftemp) {         // temp high -> turn on cooling system         digitalwrite(heatled, low);         digitalwrite(coolled, high);                               }else if (temperaturec < initialtemp) {         // temp low -> turn on heating system         digitalwrite(heatled, high);         digitalwrite(coolled, low);     }     serial.print("current pump temperature: ");     serial.print(temperaturec); serial.println("c");     delay(1000); } 

the loop() function executes on , over, it's not place store initial values.

what need define global variable, initialize inside setup() function , can read in loop() function

minimalist example:

int pin_tempread = 0;   // temperature sensor pin float initial_temp;     // define global variable  void setup() {     initial_temp = analogread(pin_tempread);   // read initial temp }  void loop() {     float current_temp = analogread(pin_tempread);     // temperature difference respect initial 1     float difference = initial_temp - current_temp;     delay(1000); } 

pd: practice distinguish variables defining hardware connection (pins) software ones. append pin_ variables define connections. otherwise not clear if tempread value of temperature or pin sensor attached.

also, turning on , off of heater/cooling system: in loop (the loop() function loop) don't need while loop.

and have problem our logic.

as understood, want heat until higher threshold (cutoff) reached, cool down until lower threshold reached (initialtemperature).

this called hysteresis, logic wrong, here's corrected one:

just do:

void loop() {      if(temperaturec > cutofftemp) {         // temp high -> turn on cooling system         digitalwrite(heatled, low);         digitalwrite(coolled, high);                               }else if (temperaturec < initialtemp) {         // temp low -> turn on heating system         digitalwrite(heatled, high);         digitalwrite(coolled, low);     }     serial.print("current pump temperature: ");     serial.print(temperaturec); serial.println("c");     delay(1000); } 

by way, using initialtemperature low threshold turning on heating.

is want?

what if initial temperature higher cutofftemp? have problems in case since lower threshold higher higher threshold.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -