I am using Arduino Uno and Core Temp Plug in which are supposed to talk with eachother.
I have edited a code that i found on github. "https://github.com/pvyParts/CoreTemp_lcd"
I have removed the LCD commands and edited the code so that i would have 2 LEDS that should switch if my CPU temp is <30 and >30.
1 led turns on when CPU Temp is >30 and other if its <30. I have problem with the code.
The code currently only Loops it once. Arduino and the plug in do not chat anymore after the first launch.
String inputString = "";
boolean NewData = false; // update Lcd flag
// holder strings
String coreSpd = "";
String coretmp = "";
String coretime = "";
int coreLoad = 0;
void setup() {
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
// setup LCD
//lcd.begin(16, 2);
// initialize serial:
Serial.begin(19200);
// reserve 200 bytes for the inputString:
inputString.reserve(200);
serialEvent();
};
// the loop function runs over and over again forever
void loop () {
if (NewData){
int temp = coretmp.toInt();
if (coretmp.toInt() < 30){
digitalWrite(13, HIGH);
digitalWrite(12, LOW);// turn the LED on (HIGH is the voltage level)
//delay(1000); // wait for a second
}else if (coretmp.toInt() > 30){
digitalWrite(12, HIGH);
digitalWrite(13, LOW);// turn the LED off by making the voltage LOW
//delay(1000); // wait for a second
};
};
digitalWrite(11,HIGH);
delay(500);
digitalWrite(11,LOW);
delay(500);
};
/*
while(true) {
int temp = coretmp.toInt();
if (temp < 30){
digitalWrite(13, HIGH);
digitalWrite(12, LOW);// turn the LED on (HIGH is the voltage level)
//delay(1000); // wait for a second
}else if (temp > 30){
digitalWrite(12, HIGH);
digitalWrite(13, LOW);// turn the LED off by making the voltage LOW
//delay(1000); // wait for a second
};
delay(1000);
};*/
int b[4];// b #0
// b0 -> b1 first break speed and avg load >> coreSpd
// b1+1 -> b2 max core temp >>coreTmp
// b2+1 -> end time >> coretime
// TODO - RAM usage and %
// Graphics LCD Graph each core load.
void serialEvent() {
b[0] = 0;
// clear the string:
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set the flag
// so the f loop can do something about it:
// update the strings to input.
if (inChar == '\n') {
b[1] = inputString.indexOf(',');
b[2] = inputString.indexOf(',', b[1]+1);
b[3] = inputString.indexOf(',', b[2]+1);
b[4] = inputString.length()-1;
coreSpd = inputString.substring(b[0],b[1]);
coretmp = inputString.substring(b[1]+1,b[2]);
coretime = inputString.substring(b[2]+1,b[3]);
String loadHold = inputString.substring(b[3]+1,b[4]);
coreLoad = loadHold.toInt();
NewData = true;
}
}
}
I don't get any errors. It just doesn't Loop it, maybe im missing something (which is for sure)
Use CTRL-T in the Arduino IDE to autoformat your complete code.
Paste the complete autoformatted code between code tags (the </> button)
so that we can easily see and deal with your code.
Paste the complete error message between code tags (the </> button)
so that we can easily see and deal with your messages.
If you already posted without code tags, you may add the code tags by
editing your post. Do not change your existing posts in any other way.
You may make additional posts as needed.
Before posting again, you should read the three locked topics at the top of the Programming Questions forum, and any links to which these posts point.
If your project involves wiring, please provide a schematic and/or a wiring diagram and/or a clear photograph of the wiring.
If you want the usual serialEvent() function to be called automatically when there is serial data to be processed, that is NOT the correct signature for the function.
Just after your sketch starts, there is no serial data for your function to process. Since your function does not have the correct signature, it is not called between iterations of loop(), even if serial data arrives.
There is NOTHING magic about the serialEvent() function. It is EXACTLY the same as putting
if(Serial.available() > 0)
{
// do something about it
}
at the top of loop, where the "do something about it" is whatever you put in the serialEvent() function.
Your next issue is going to be the fact that you piss away memory using the String class. Stop doing that RIGHT NOW. Learn to use c-style strings (NULL terminated arrays of chars) NOW.
Hello PaulS,
Thank you for your reply but i'd like to state that i am newbie in coding. This is my very first project that i have to do and the codel ines that i have copied above are not mine. I just edited them alittle bit so that i could test out of the loop works as i want it to.
I have no experience in C++ what so ever.
I have edited this code so much as my brain would understand.
Also i have tried it different ways, for example
String inputString = "";
boolean NewData = false; // update Lcd flag
// holder strings
String coreSpd = "";
String coretmp = "";
String coretime = "";
int coreLoad = 0;
void setup() {
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
// setup LCD
//lcd.begin(16, 2);
// initialize serial:
Serial.begin(19200);
// reserve 200 bytes for the inputString:
inputString.reserve(200);
//serialEvent();
};
int b[4];// b #0
// the loop function runs over and over again forever
void loop() {
delay(500);
}
void serialEvent() {
b[0] = 0;
// clear the string:
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set the flag
// so the f loop can do something about it:
// update the strings to input.
if (inChar == '\n') {
b[1] = inputString.indexOf(',');
b[2] = inputString.indexOf(',', b[1] + 1);
b[3] = inputString.indexOf(',', b[2] + 1);
b[4] = inputString.length() - 1;
coreSpd = inputString.substring(b[0], b[1]);
coretmp = inputString.substring(b[1] + 1, b[2]);
coretime = inputString.substring(b[2] + 1, b[3]);
String loadHold = inputString.substring(b[3] + 1, b[4]);
coreLoad = loadHold.toInt();
NewData = true;
digitalWrite(10, HIGH);
delay(500);
digitalWrite(10, LOW);
if (coretmp.toInt() < 30) {
digitalWrite(13, HIGH);
digitalWrite(12, LOW);// turn the LED on (HIGH is the voltage level)
//delay(1000); // wait for a second
} else if (coretmp.toInt() > 30) {
digitalWrite(12, HIGH);
digitalWrite(13, LOW);// turn the LED off by making the voltage LOW
//delay(1000); // wait for a second
};
Serial.flush();
};
};
};
This is the last thing i tried to do, i added another LED to see if the the data read is 0. but it is not. I just cant figure out how i could make the loop work that Arduino will get information from the CoreTemp plugin again and again.
Well thank you all for your replies. Did not get the code working even after examining all the info you guys gave me and no, i don't have reading comprehensions, english just isnt my first language.
TheSebamed:
Did not get the code working even after examining all the info you guys gave me
Did you try the code in the link I gave you in Reply #5?
Did it work?
Did you compare how that works to your own code?
Or just use my examples in your own project?