Hello everyone,
I-am trying to create a thermometer with bluetooth to connect with a phone.
The idea is that i code a few functions on the arduino that can be run by a command over bluetooth.
Now the code works and has everything i want so far. however it runs slow as f.
It takes one command "readC" for example every 1.5 ish seconds.
I know it can read the temp over 10 times each second so something goes wrong in figuring out what command has been given i guess.
However i-am a PHP Javascript guy and not so much a C? guy.
So someone PLEASE HELP ME OPTIMISE THIS CODE:
* SS: pin 10
* MOSI: pin 11 (NC)
* MISO: pin 12
* SCK: pin 13
* VCC: pin 14
* GND: pin 15
* arduino>>bluetooth
* RX: PIN 2
* TX: PIN 3
* Arduino Pro Mini 328 3.3V/8MHz
#include <SoftwareSerial.h>// import the serial library
#include "Adafruit_MAX31855.h"
#include <SPI.h> // Included here too due Arduino IDE; Used in above header
SoftwareSerial SoftSerial(3, 2); //arduino RX, TX
String BluetoothData;
#define RPin 5
#define GPin 6
#define BPin 9
#define MAXCS 10
Adafruit_MAX31855 thermocouple(MAXCS);
void setup() {
SoftSerial.begin(19200);
while (!Serial) {
delay(1); // wait for serial port to connect
}
pinMode(RPin, OUTPUT);
pinMode(GPin, OUTPUT);
pinMode(BPin, OUTPUT);
SoftSerial.println("Software serial over bluetooth starting");
}
void loop() {
delay(100);
if(SoftSerial.available()){
BluetoothData = SoftSerial.readString();
BluetoothData.trim();
//SoftSerial.println(BluetoothData);
if(BluetoothData.equals("readAll")){
SoftSerial.print("Internal Temp = ");
SoftSerial.println(thermocouple.readInternal());
SoftSerial.print("C = ");
SoftSerial.println(thermocouple.readCelsius());
SoftSerial.print("F = ");
SoftSerial.println(thermocouple.readFarenheit());
}else if(BluetoothData.equals("setLed")){
int RedInt = 0;
int GreenInt = 0;
int BlueInt = 0;
float AlphaFloat = 0.0;
SoftSerial.println("send color like this: 255,255,255,1.0 ");
String RedStr = SoftSerial.readStringUntil(',');
Serial.read();
String GreenStr = SoftSerial.readStringUntil(',');
Serial.read();
String BlueStr = SoftSerial.readStringUntil(',');
Serial.read();
String AlphaStr = SoftSerial.readStringUntil('\0');
RedInt = RedStr.toInt();
GreenInt = GreenStr.toInt();
BlueInt = BlueStr.toInt();
AlphaFloat = AlphaStr.toFloat();
RedInt = constrain(RedInt, 0, 255);
GreenInt = constrain(GreenInt, 0, 255);
BlueInt = constrain(BlueInt, 0, 255);
AlphaFloat = constrain(AlphaFloat, 0.0, 1.0);
setLED(RedInt, GreenInt, BlueInt, AlphaFloat);
}else if(BluetoothData.equals("readC")){
SoftSerial.print("C = ");
SoftSerial.println(thermocouple.readCelsius());
}else if(BluetoothData.equals("readF")){
SoftSerial.print("F = ");
SoftSerial.println(thermocouple.readFarenheit());
}else if(BluetoothData.equals("readInternal")){
SoftSerial.print("Internal Temp = ");
SoftSerial.println(thermocouple.readInternal());
}else if(BluetoothData.equals("marco")){
SoftSerial.println("polo");
}else{
SoftSerial.println("i did not understand that");
}
SoftSerial.println("send: readAll, readInternal, readC, readF, marco ");
}
}
void setLED(int Red, int Green, int Blue, float Alpha )
{
//SoftSerial.println(Red);
//SoftSerial.println(Green);
//SoftSerial.println(Blue);
//SoftSerial.println(Alpha);
analogWrite(RPin, (Red * Alpha));
analogWrite(GPin, (Green * Alpha));
analogWrite(BPin, (Blue * Alpha));
}