Hi, this is the code developed today to work with our recently built TCLab boards. Use Arduino IDE to upload and test your code:
#include <Arduino.h>
// pins and definitions
const int led = 9;
const int T1 = A5;
const int T2 = A3;
const int Q1 = 3;
const int Q2 = 5;
// vars and consts
float T2Val;
float T1Val;
const int nAvg = 10; //samples per reading
const long BAUD = 9600; // 115200
const float factor = 3.3/1024*100;
//functions:
float readDeg(int channel){
float acc = 0;
for(int i=0;i<nAvg;i++){
acc +=analogRead(channel)*factor-50;
}
return acc/nAvg;
}
void setHeater(int pin, float value){
if(value<0) value = 0;
if(value>100) value = 100;
int pwm = (int)(value*255/100+0.5);
analogWrite(pin, pwm);
analogWrite(led, pwm);
}
void setup() {
// put your setup code here, to run once:
pinMode(led, OUTPUT);
pinMode(Q1, OUTPUT);
pinMode(Q2, OUTPUT);
analogReference(EXTERNAL);
Serial.begin(BAUD);
//setHeater(Q2,40); // 0 to 100%
//Serial.println("Temps");
}
void loop() {
// put your main code here, to run repeatedly:
if(Serial.available()){
String cmd = Serial.readStringUntil('\n');
cmd.trim();
if(cmd=="T1"){
T1Val = readDeg(T1);
Serial.println(T1Val);
}
else if(cmd=="T2"){
T2Val = readDeg(T2);
Serial.println(T2Val);
}
else if(cmd.startsWith("Q1 ")){
int val = cmd.substring(3).toInt();
setHeater(Q1, val);
}
else if(cmd.startsWith("Q2 ")){
int val = cmd.substring(3).toInt();
setHeater(Q2, val);
}
else {
Serial.println("Command error");
}
}
}