// Sketch to test 4-ch FET board to use with Webasto heater. Control via serial port in following format:
// if first character is c them following digit sets channel.
// if first character is number then this is percentage drive. if less than 11 (i.e 0 to 10) then this is used as % x 10. 6 = 60%.
// end of message signified by CR or \n.
#include <Arduino.h>
// analogWrite is suppored in Arduino ESP8266 so unlike ESP32 no extra library needed. If ESP8266 Arduino version is less that v3.0 then range is 1023 later is 255
int chPin[4] = {3,5,6,9};
//int ch1Pin = 2;
//int ch2Pin = 12;
//int ch3Pin = 13;
//int ch4Pin = 14;
int ch1Val[4] = {0,0,0,0};
//int ch2Val = 0;
//int ch3Val = 0;
//int ch4Val = 0;
String inPCmessage = "";
bool PCmessageDone = false;
int chNo = 0;
void setup() {
// put your setup code here, to run once:
// pinMode(ch1Pin,OUTPUT);// not needed
Serial.begin(115200);
Serial.println("");
}
void getSerial(void){
if(Serial.available()>0){
char in = Serial.read();
if (in == '\n' ) { // this is end of a message from PC
PCmessageDone = true;
processInMessage();
}
else {
inPCmessage = inPCmessage + in;
}
}
}
void processInMessage(){
if (inPCmessage.substring(0,1) == "c"){
chNo = inPCmessage.substring(1,2).toInt();
Serial.print("you sent ch num ");
Serial.println(chNo);
if (chNo>4){
chNo = 0;
}
}
if (inPCmessage.substring(0,1) == "r"){
for(int r = 0; r<4;r++){
ch1Val[r]=0;
analogWrite(chPin[r],0);
}
Serial.println("All Reset.");
}
else{ int chVal = inPCmessage.toInt();
Serial.print("channel no. ");
Serial.print(chNo);
Serial.print(" ");
Serial.print("chVal = ");
if(chVal<11){
chVal=chVal*10;
}
chVal = chVal * 255 / 100;
if(chVal>255){
chVal=255;
}
Serial.println(chVal);
ch1Val[chNo-1]=chVal;
for(int n = 0;n<4;n++){
Serial.print("Channel");
Serial.print(n+1);
Serial.print(" value is ");
Serial.println(ch1Val[n]);
// Serial.print("pin num ");
// Serial.println(chPin[n]);
analogWrite(chPin[n],ch1Val[n]);
}
}
inPCmessage = "";
// print all channels
}
void loop() {
getSerial();
}