Hello, I wrote a program witch gets the information from pc about cpu usage and free ram memory and displays it on lcd, it can also show a temperature and monitor fans.
#include <LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
const float fanc = 0.3922;
float temp;
int tempPin = A0;
int sp;
int fan;
int pro;
int fanrez = A1;
const int buttonPin1 = 8;
const int buttonPin2 = 10;
const int buttonPin3 = 13;
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
const int TIP120pin = 9;
const int LED1 = 11;
const int LED2 = 12;
const byte buffSize = 32;
char inputSeveral[buffSize]; // space for 31 chars and a terminator
byte maxChars = 12; // a shorter limit to make it easier to see what happens
// if too many chars are entered
int cpu;
int ram;
char inputCsvString[12];
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
pinMode(TIP120pin, OUTPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
digitalWrite(buttonPin1, HIGH);
digitalWrite(buttonPin2, HIGH);
digitalWrite(buttonPin3, HIGH);
}
void loop() {
readtemp();
fanctrl();
readCSV();
}
void readCSV() {
// this function expects a series of comma separated values
// for this demo the sequence of items must be a string, an integer, a float
// for example testing, 123 , 4567.89
// spaces around the commas are optional
// first read severalChars into the array inputSeveral
inputSeveral[0] = 0;
maxChars = buffSize - 1; // use full size of buffer for this function
byte charCount = 0;
byte ndx = 0;
if (Serial.available() > 0) {
while (Serial.available() > 0) {
if (ndx > maxChars - 1) {
ndx = maxChars;
}
inputSeveral[ndx] = Serial.read();
ndx ++;
charCount ++;
}
if (ndx > maxChars) {
ndx = maxChars;
}
inputSeveral[ndx] = 0;
// now we need to split the received string into its parts
// this is done by strtok() which looks for the token - the comma in this case
char * partOfString; // this is used by strtok() as an index
partOfString = strtok(inputSeveral,","); // get the first part - the string
strcpy(inputCsvString, partOfString); // copy it to inputCsvString
partOfString = strtok(NULL, ","); // this continues where the previous call left off
cpu = atoi(partOfString); // convert this part to an integer
partOfString = strtok(NULL, ",");
ram = atoi(partOfString); // convert this part to a float
lcdct2();
}
else {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("DUOMENU NERA");
delay(1000);
}
}
void fanctrl() {
buttonState1 = digitalRead(buttonPin1);
if (buttonState1 == HIGH) {
sp = analogRead(fanrez);
sp = map(sp, 0, 1023, 0, 255);
}
else {
if(cpu >= 70) {
sp = 255;
}
else {
sp = 0;
}
}
int y = round(sp*fanc);
if (y >= 50){
analogWrite(TIP120pin, sp); }
else {
analogWrite(TIP120pin, 0);
}
}
void ledctrl() {
buttonState3 = digitalRead(buttonPin3);
if ( buttonState3 == HIGH )
{
if (cpu >= 70) {
digitalWrite(LED1, HIGH);
digitalWrite(LED2, LOW);
}
else {
digitalWrite(LED2, HIGH);
digitalWrite(LED1, LOW);
}
}
else {
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
}
}
void readtemp() {
temp = analogRead(tempPin)*0.48828125;
delay(1000);
}
void lcdct2() {
buttonState2 = digitalRead(buttonPin2);
if (buttonState2 == HIGH) {
lcdct();
}
else {
lcdct1();
}
}
void lcdct() {
pro = round(sp*fanc);
if (pro >= 50 )
{
pro = round(sp*fanc);
lcd.clear();
lcd.print("Temp: ");
lcd.print(temp);
lcd.print(" *C");
lcd.setCursor(0,1);
lcd.print("FAN: ");
lcd.print(pro);
lcd.print(" %");
delay(1000);
}
else { lcd.clear();
lcd.print("Temp: ");
lcd.print(temp);
lcd.print(" *C");
lcd.setCursor(0,1);
lcd.print("FAN: OFF");
delay(1000);
}
}
void lcdct1() {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("CPU: ");
lcd.print(cpu);
lcd.print(" %");
lcd.setCursor(0,1);
lcd.print("RAM: ");
lcd.print(ram);
lcd.print(" mb");
delay(1000);
}
The problem I faced is that if my fans are off it shows right temperature, but when I turn the fans on temperature just starts go crazy jumping from 0 to 45 degrees in C. I thought that the malfunction is somewhere in electric circuit, but I guess it is not, because firstly the fans and LM35 temperature sensor were powered from molex pins in pc case fans used 12V and GND, and LM35 used 5V and GND, but now I changed and LM35 is powered from arduino and the problem still stays. I also made a simple experimet and uploaded a simple skectch like this:
#include <LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
const float fanc = 0.3922;
float temp;
int tempPin = A0;
int sp;
int fan;
int pro;
int fanrez = A1;
const int buttonPin1 = 8;
const int buttonPin2 = 10;
const int buttonPin3 = 13;
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
const int TIP120pin = 9;
const int LED1 = 11;
const int LED2 = 12;
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
pinMode(TIP120pin, OUTPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
digitalWrite(buttonPin1, HIGH);
digitalWrite(buttonPin2, HIGH);
digitalWrite(buttonPin3, HIGH);
}
void loop() {
readtemp();
analogWrite(TIP120pin, 255);
lcd.clear();
lcd.print("Temp: ");
lcd.print(temp);
lcd.print(" *C");
}
void readtemp() {
temp = analogRead(tempPin)*0.48828125;
delay(1000);
}
Here fans are going full speed, but the temperature is normal.... Maybe someone can help me find a mistake in my main program?