03-03-2025, 03:14 PM
Thanks for the recommendations! Experience truly makes a difference. You've touched on some very critical points for me. I revised the code a bit yesterday, and now my goal is to develop a small React Native app using either Bluetooth Classic or Low Energy. Later, I plan to add an HTTP server and integrate it into the caravan system I'm developing. I'm sharing the latest code—it's still a work in progress.
/*Code Begin*/
#include <Arduino.h>
#include "HardwareSerial.h"
#include "BluetoothSerial.h"
#define TX_RX_PIN 17 // Tek hatlı UART için kullanılacak pin
BluetoothSerial SerialBT;
int oldValue = 0;
int newValue = 0;
unsigned long timeNow;
unsigned long timeOld;
unsigned long elapsedTime;
int counter = 0;
bool rxActive = 0;
/*
8H, 4L, 4H, 8L, 8H, 4L, 4H, 8L, 4H, 8L, 8H, 4L, 8H, 4L, 4H, 8L, 1H, 30L,
8H, 4L, 4H, 8L, 8H, 4L, 4H, 8L, 8H, 4L, 8H, 4L, 8H, 4L, 8H, 4L, 1H, 30L,
*/
int start_stop1[] = {
1, // 1H -> 1 adet 1
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 30L -> 30 adet 0
1,1,1,1,1,1,1,1, // 8H -> 8 adet 1
0,0,0,0, // 4L -> 4 adet 0
1,1,1,1, // 4H -> 4 adet 1
0,0,0,0,0,0,0,0, // 8L -> 8 adet 0
1,1,1,1,1,1,1,1, // 8H -> 8 adet 1
0,0,0,0, // 4L -> 4 adet 0
1,1,1,1, // 4H -> 4 adet 1
0,0,0,0,0,0,0,0, // 8L -> 8 adet 0
1,1,1,1, // 4H -> 4 adet 1
0,0,0,0,0,0,0,0, // 8L -> 8 adet 0
1,1,1,1, // 4H -> 4 adet 1
0,0,0,0,0,0,0,0, // 8L -> 8 adet 0
1,1,1,1,1,1,1,1, // 8H -> 8 adet 1
0,0,0,0, // 4L -> 4 adet 0
1,1,1,1, // 4H -> 4 adet 1
0,0,0,0,0,0,0,0, // 8L -> 8 adet 0
1 // 1H -> 1 adet 1
};
int start_stop2[] = {
1, // 1H -> 1 adet 1
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 30L -> 30 adet 0
1,1,1,1,1,1,1,1, // 8H -> 8 adet 1
0,0,0,0, // 4L -> 4 adet 0
1,1,1,1, // 4H -> 4 adet 1
0,0,0,0,0,0,0,0, // 8L -> 8 adet 0
1,1,1,1,1,1,1,1, // 8H -> 8 adet 1
0,0,0,0, // 4L -> 4 adet 0
1,1,1,1, // 4H -> 4 adet 1
0,0,0,0,0,0,0,0, // 8L -> 8 adet 0
1,1,1,1, // 4H -> 4 adet 1
0,0,0,0,0,0,0,0, // 8L -> 8 adet 0
1,1,1,1, // 4H -> 4 adet 1
0,0,0,0,0,0,0,0, // 8L -> 8 adet 0
1,1,1,1,1,1,1,1, // 8H -> 8 adet 1
0,0,0,0, // 4L -> 4 adet 0
1,1,1,1,1,1,1,1, // 8H -> 8 adet 1
0,0,0,0, // 4L -> 4 adet 0
1 // 1H -> 1 adet 1
};
int start_stop3[] = {
1, // 1H
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 30L
1,1,1,1,1,1,1,1, // 8H
0,0,0,0, // 4L
1,1,1,1, // 4H
0,0,0,0,0,0,0,0, // 8L
1,1,1,1,1,1,1,1, // 8H
0,0,0,0, // 4L
1,1,1,1, // 4H
0,0,0,0,0,0,0,0, // 8L
1,1,1,1, // 4H
0,0,0,0,0,0,0,0, // 8L
1,1,1,1,1,1,1,1, // 8H
0,0,0,0, // 4L
1,1,1,1, // 4H
0,0,0,0,0,0,0,0, // 8L
1,1,1,1, // 4H
0,0,0,0,0,0,0,0, // 8L
1 // 1H
};
int devamli[] = {
1,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1,1,1,1,1,1,1,1,0,0,0,0,
1,1,1,1,0,0,0,0,0,0,0,0,
1,1,1,1,1,1,1,1,0,0,0,0,
1,1,1,1,0,0,0,0,0,0,0,0,
1,1,1,1,0,0,0,0,0,0,0,0,
1,1,1,1,1,1,1,1,0,0,0,0,
1,1,1,1,1,1,1,1,0,0,0,0,
1,1,1,1,0,0,0,0,0,0,0,0,
1
};
int kazan_sicaklik[] = {
1, // 1H -> 1 adet 1
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 30L -> 30 adet 0
1,1,1,1,1,1,1,1,0,0,0,0,
1,1,1,1,0,0,0,0,0,0,0,0,
1,1,1,1,1,1,1,1,0,0,0,0,
1,1,1,1,1,1,1,1,0,0,0,0,
1,1,1,1,1,1,1,1,0,0,0,0,
1,1,1,1,1,1,1,1,0,0,0,0,
1,1,1,1,1,1,1,1,0,0,0,0,
1,1,1,1,1,1,1,1,0,0,0,0,
1
};
int pompaTetikle[] = {
// 1 ms high
1,
// 30 ms low
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
// 8 ms high
1, 1, 1, 1, 1, 1, 1, 1,
// 4 ms low
0, 0, 0, 0,
// 4 ms high
1, 1, 1, 1,
// 8 ms low
0, 0, 0, 0, 0, 0, 0, 0,
// 4 ms high
1, 1, 1, 1,
// 8 ms low
0, 0, 0, 0, 0, 0, 0, 0,
// 4 ms high
1, 1, 1, 1,
// 8 ms low
0, 0, 0, 0, 0, 0, 0, 0,
// 8 ms high
1, 1, 1, 1, 1, 1, 1, 1,
// 4 ms low
0, 0, 0, 0,
// 8 ms high
1, 1, 1, 1, 1, 1, 1, 1,
// 4 ms low
0, 0, 0, 0,
// 4 ms high
1, 1, 1, 1,
// 8 ms low
0, 0, 0, 0, 0, 0, 0, 0,
// 1 ms high
1
};
volatile bool sendData = false;
void setup() {
Serial.begin(115200); // Seri monitör başlat
SerialBT.begin("EskKaravanHeater");
//baudrate 1000
timeOld = 0;
timeNow = millis();
#ifndef SNIFFERMODE
pinMode(TX_RX_PIN, OUTPUT);
#endif
//sleep(3);
#ifdef SNIFFERMODE
pinMode(TX_RX_PIN, INPUT);
while (1)
{
timeNow = millis();
newValue = digitalRead(TX_RX_PIN);
if (newValue != oldValue){
elapsedTime = timeNow - timeOld;
timeOld = timeNow;
}
if (elapsedTime > 25 && elapsedTime < 35)
break;
oldValue = newValue;
}
#endif
}
int findLowHighCount(int arr[], int len)
{
int counter = 0;
int highCount = 0;
int lowCount = 0;
while (counter < len)
{
if (arr[counter] == 1)
highCount++;
else
lowCount++;
counter++;
}
if (lowCount > highCount)
return (0);
else
return (1);
}
void setVoltage(unsigned short receivedData) {
// İlk 4 bit (Başlangıç Kodu) 0110 olmalı
unsigned short header = (receivedData >> 12) & 0xF; // En üst 4 biti al
// Ortadaki 8 bit sıfır olmalı
unsigned short middleBits = (receivedData >> 4) & 0xFF; // Ortadaki 8 biti al
// Kontroller
if (header == 0b0110 && middleBits == 0x00) {
unsigned short voltage = receivedData & 0xF; // Son 4 biti al
}
}
void setTemperatureLevel(unsigned short receivedData) {
// İlk 4 bit (Header) al
unsigned short header = (receivedData >> 12) & 0xF;
// Ortadaki 8 biti al
unsigned short middleBits = (receivedData >> 4) & 0xFF;
// Son 4 bit (Sıcaklık kademesi)
unsigned short temperatureLevel = receivedData & 0xF;
// Eğer ortadaki 8 bitin sağdan 3. biti `1` ise sıcaklık kademesini al
if ((middleBits & 0b00000100) != 0) {
unsigned short temperatureLevel = receivedData & 0xF;
temperatureLevel++;
}
}
void takeFeedback()
{
bool bits[16];
int feedCounter;
int feedBackTx[192];
int garbage[8];
int garbageCounter;
int tempCounter;
unsigned short receivedData = 0;
int rcCounter = 0;
vTaskDelay(1);
while(1)
{
if (digitalRead(TX_RX_PIN))
break;
}
feedCounter = 0;
tempCounter = 0;
while (feedCounter < 192){
/*
Serial.printf("%d, ", digitalRead(TX_RX_PIN));
vTaskDelay(1);
feedCounter++;
*/
garbageCounter = 0;
while (garbageCounter < 4)
{
garbage[garbageCounter] = digitalRead(TX_RX_PIN);
feedBackTx[tempCounter] = garbage[garbageCounter];
tempCounter++;
garbageCounter++;
vTaskDelay(1);
}
if (findLowHighCount(garbage, 4) != 1)
{
Serial.println("fail");
feedCounter = 0;
while (feedCounter < tempCounter)
{
Serial.printf("%d, ", feedBackTx[feedCounter]);
feedCounter++;
}
feedCounter = 0;
while (feedCounter < 200)
{
Serial.printf("%d, ", digitalRead(TX_RX_PIN));
feedCounter++;
}
Serial.println("");
return;
}
garbageCounter = 0;
while (garbageCounter < 4)
{
garbage[garbageCounter] = digitalRead(TX_RX_PIN);
feedBackTx[tempCounter] = garbage[garbageCounter];
tempCounter++;
garbageCounter++;
vTaskDelay(1);
}
if (findLowHighCount(garbage, 4) == 1)
{
receivedData = (receivedData << 1) | 1;
Serial.print("1, ");
rcCounter++;
}
else
{
receivedData = (receivedData << 1) | 0;
Serial.print("0, ");
rcCounter++;
}
garbageCounter = 0;
while (garbageCounter < 4)
{
garbage[garbageCounter] = digitalRead(TX_RX_PIN);
feedBackTx[tempCounter] = garbage[garbageCounter];
tempCounter++;
garbageCounter++;
vTaskDelay(1);
}
if (findLowHighCount(garbage, 4) == 1)
{
Serial.println("fail");
feedCounter = 0;
while (feedCounter < tempCounter)
{
Serial.printf("%d, ", feedBackTx[feedCounter]);
feedCounter++;
}
feedCounter = 0;
while (feedCounter < 200)
{
Serial.printf("%d, ", digitalRead(TX_RX_PIN));
feedCounter++;
}
Serial.println("");
return;
}
feedCounter+= 12;
}
for (int i = 15; i >= 0; i--) {
Serial.print((receivedData >> i) & 1); // Her biti tek tek yazdır
}
Serial.println();
Serial.println("Success");
}
int highCounter = 1;
int lowCounter = 1;
bool sendData1 = false;
bool sendData2 = false;
bool sendData3 = false;
bool sendData4 = false;
int process = 0;
int process1 = 0;
int switchh = 1;
void loop() {
#ifdef SNIFFERMODE
if (newValue == LOW)
lowCounter++;
else if (newValue == HIGH)
highCounter++;
if (newValue == LOW && oldValue == HIGH)
{
if (highCounter == 3 || highCounter == 5)
highCounter = 4;
else if (highCounter == 2)
highCounter = 1;
else if (highCounter == 7 || highCounter == 9)
highCounter = 8;
else if (highCounter == 649 || highCounter == 651 || highCounter == 652)
highCounter = 650;
Serial.printf("%dH, ", highCounter);
if (highCounter == 650)
Serial.println("");
highCounter = 1;
lowCounter = 1;
} else if (newValue == HIGH && oldValue == LOW)
{
if (lowCounter == 3 || lowCounter == 5)
lowCounter = 4;
else if (lowCounter == 29 || lowCounter == 31)
lowCounter = 30;
else if (lowCounter == 7 || lowCounter == 9)
lowCounter = 8;
Serial.printf("%dL, ", lowCounter);
if (lowCounter == 30)
Serial.println("");
highCounter = 1;
lowCounter = 1;
}
oldValue = newValue;
///Serial.printf("%d, ", newValue);
timeNow = millis();
newValue = digitalRead(TX_RX_PIN);
vTaskDelay(1);
#endif
#ifndef SNIFFERMODE
if (Serial.available()) {
char receivedChar = Serial.read();
if (receivedChar == '1') {
//pinMode(TX_RX_PIN, OUTPUT);
process = 1;
Serial.println("Veri gönderiliyor...");
} else if (receivedChar == '2') {
//pinMode(TX_RX_PIN, OUTPUT);
process = 2;
Serial.println("Veri gönderiliyor...");
} else if (receivedChar == '3') {
//pinMode(TX_RX_PIN, OUTPUT);
process = 3;
Serial.println("Veri gönderiliyor...");
} else if (receivedChar == '4') {
//pinMode(TX_RX_PIN, OUTPUT);
process = 4;
Serial.println("Veri gönderiliyor...");
}
}
// 4 low 8 high 4 low = 4080
// 4 low 4 high 8 low = 3840
// 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0
// 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0
//
if (process1 == 0)
{
digitalWrite(TX_RX_PIN, devamli[counter]);
counter++;
vTaskDelay(1);
if (counter == 128)
{
pinMode(TX_RX_PIN, INPUT_PULLDOWN);
counter = 0;
takeFeedback();
counter = 0;
pinMode(TX_RX_PIN, OUTPUT);
vTaskDelay(200);
//cevabiBekle();
if (process1 == process)
{
process1 = 0;
process = 0;
}
else
process1 = process;
}
} else if (process1 == 1)
{
digitalWrite(TX_RX_PIN, start_stop1[counter]);
counter++;
vTaskDelay(1);
if (counter == 128)
{
Serial.println("PRocess1 send!");
pinMode(TX_RX_PIN, INPUT_PULLDOWN);
counter = 0;
takeFeedback();
counter = 0;
pinMode(TX_RX_PIN, OUTPUT);
vTaskDelay(200);
//cevabiBekle();
if (process1 == process)
{
process1 = 0;
process = 0;
}
else
process1 = process;
}
} else if (process1 == 2)
{
digitalWrite(TX_RX_PIN, start_stop2[counter]);
counter++;
vTaskDelay(1);
if (counter == 128)
{
Serial.println("PRocess1 send!");
pinMode(TX_RX_PIN, INPUT_PULLDOWN);
counter = 0;
takeFeedback();
counter = 0;
pinMode(TX_RX_PIN, OUTPUT);
vTaskDelay(200);
//cevabiBekle();
if (process1 == process)
{
process1 = 0;
process = 0;
}
else
process1 = process;
}
} else if (process1 == 3)
{
digitalWrite(TX_RX_PIN, start_stop3[counter]);
counter++;
vTaskDelay(1);
if (counter == 128)
{
Serial.println("PRocess3 send!");
pinMode(TX_RX_PIN, INPUT_PULLDOWN);
counter = 0;
takeFeedback();
counter = 0;
pinMode(TX_RX_PIN, OUTPUT);
vTaskDelay(200);
//cevabiBekle();
if (process1 == process)
{
process1 = 0;
process = 0;
}
else
process1 = process;
}
} else if (process1 == 4)
{
digitalWrite(TX_RX_PIN, kazan_sicaklik[counter]);
counter++;
vTaskDelay(1);
if (counter == 128)
{
Serial.println("PRocess4 send!");
pinMode(TX_RX_PIN, INPUT_PULLDOWN);
counter = 0;
takeFeedback();
counter = 0;
pinMode(TX_RX_PIN, OUTPUT);
vTaskDelay(200);
//cevabiBekle();
if (process1 == process)
{
process1 = 0;
process = 0;
}
else
process1 = process;
}
}
#endif
}
/*Code end*/
/*Code Begin*/
#include <Arduino.h>
#include "HardwareSerial.h"
#include "BluetoothSerial.h"
#define TX_RX_PIN 17 // Tek hatlı UART için kullanılacak pin
BluetoothSerial SerialBT;
int oldValue = 0;
int newValue = 0;
unsigned long timeNow;
unsigned long timeOld;
unsigned long elapsedTime;
int counter = 0;
bool rxActive = 0;
/*
8H, 4L, 4H, 8L, 8H, 4L, 4H, 8L, 4H, 8L, 8H, 4L, 8H, 4L, 4H, 8L, 1H, 30L,
8H, 4L, 4H, 8L, 8H, 4L, 4H, 8L, 8H, 4L, 8H, 4L, 8H, 4L, 8H, 4L, 1H, 30L,
*/
int start_stop1[] = {
1, // 1H -> 1 adet 1
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 30L -> 30 adet 0
1,1,1,1,1,1,1,1, // 8H -> 8 adet 1
0,0,0,0, // 4L -> 4 adet 0
1,1,1,1, // 4H -> 4 adet 1
0,0,0,0,0,0,0,0, // 8L -> 8 adet 0
1,1,1,1,1,1,1,1, // 8H -> 8 adet 1
0,0,0,0, // 4L -> 4 adet 0
1,1,1,1, // 4H -> 4 adet 1
0,0,0,0,0,0,0,0, // 8L -> 8 adet 0
1,1,1,1, // 4H -> 4 adet 1
0,0,0,0,0,0,0,0, // 8L -> 8 adet 0
1,1,1,1, // 4H -> 4 adet 1
0,0,0,0,0,0,0,0, // 8L -> 8 adet 0
1,1,1,1,1,1,1,1, // 8H -> 8 adet 1
0,0,0,0, // 4L -> 4 adet 0
1,1,1,1, // 4H -> 4 adet 1
0,0,0,0,0,0,0,0, // 8L -> 8 adet 0
1 // 1H -> 1 adet 1
};
int start_stop2[] = {
1, // 1H -> 1 adet 1
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 30L -> 30 adet 0
1,1,1,1,1,1,1,1, // 8H -> 8 adet 1
0,0,0,0, // 4L -> 4 adet 0
1,1,1,1, // 4H -> 4 adet 1
0,0,0,0,0,0,0,0, // 8L -> 8 adet 0
1,1,1,1,1,1,1,1, // 8H -> 8 adet 1
0,0,0,0, // 4L -> 4 adet 0
1,1,1,1, // 4H -> 4 adet 1
0,0,0,0,0,0,0,0, // 8L -> 8 adet 0
1,1,1,1, // 4H -> 4 adet 1
0,0,0,0,0,0,0,0, // 8L -> 8 adet 0
1,1,1,1, // 4H -> 4 adet 1
0,0,0,0,0,0,0,0, // 8L -> 8 adet 0
1,1,1,1,1,1,1,1, // 8H -> 8 adet 1
0,0,0,0, // 4L -> 4 adet 0
1,1,1,1,1,1,1,1, // 8H -> 8 adet 1
0,0,0,0, // 4L -> 4 adet 0
1 // 1H -> 1 adet 1
};
int start_stop3[] = {
1, // 1H
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 30L
1,1,1,1,1,1,1,1, // 8H
0,0,0,0, // 4L
1,1,1,1, // 4H
0,0,0,0,0,0,0,0, // 8L
1,1,1,1,1,1,1,1, // 8H
0,0,0,0, // 4L
1,1,1,1, // 4H
0,0,0,0,0,0,0,0, // 8L
1,1,1,1, // 4H
0,0,0,0,0,0,0,0, // 8L
1,1,1,1,1,1,1,1, // 8H
0,0,0,0, // 4L
1,1,1,1, // 4H
0,0,0,0,0,0,0,0, // 8L
1,1,1,1, // 4H
0,0,0,0,0,0,0,0, // 8L
1 // 1H
};
int devamli[] = {
1,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1,1,1,1,1,1,1,1,0,0,0,0,
1,1,1,1,0,0,0,0,0,0,0,0,
1,1,1,1,1,1,1,1,0,0,0,0,
1,1,1,1,0,0,0,0,0,0,0,0,
1,1,1,1,0,0,0,0,0,0,0,0,
1,1,1,1,1,1,1,1,0,0,0,0,
1,1,1,1,1,1,1,1,0,0,0,0,
1,1,1,1,0,0,0,0,0,0,0,0,
1
};
int kazan_sicaklik[] = {
1, // 1H -> 1 adet 1
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 30L -> 30 adet 0
1,1,1,1,1,1,1,1,0,0,0,0,
1,1,1,1,0,0,0,0,0,0,0,0,
1,1,1,1,1,1,1,1,0,0,0,0,
1,1,1,1,1,1,1,1,0,0,0,0,
1,1,1,1,1,1,1,1,0,0,0,0,
1,1,1,1,1,1,1,1,0,0,0,0,
1,1,1,1,1,1,1,1,0,0,0,0,
1,1,1,1,1,1,1,1,0,0,0,0,
1
};
int pompaTetikle[] = {
// 1 ms high
1,
// 30 ms low
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
// 8 ms high
1, 1, 1, 1, 1, 1, 1, 1,
// 4 ms low
0, 0, 0, 0,
// 4 ms high
1, 1, 1, 1,
// 8 ms low
0, 0, 0, 0, 0, 0, 0, 0,
// 4 ms high
1, 1, 1, 1,
// 8 ms low
0, 0, 0, 0, 0, 0, 0, 0,
// 4 ms high
1, 1, 1, 1,
// 8 ms low
0, 0, 0, 0, 0, 0, 0, 0,
// 8 ms high
1, 1, 1, 1, 1, 1, 1, 1,
// 4 ms low
0, 0, 0, 0,
// 8 ms high
1, 1, 1, 1, 1, 1, 1, 1,
// 4 ms low
0, 0, 0, 0,
// 4 ms high
1, 1, 1, 1,
// 8 ms low
0, 0, 0, 0, 0, 0, 0, 0,
// 1 ms high
1
};
volatile bool sendData = false;
void setup() {
Serial.begin(115200); // Seri monitör başlat
SerialBT.begin("EskKaravanHeater");
//baudrate 1000
timeOld = 0;
timeNow = millis();
#ifndef SNIFFERMODE
pinMode(TX_RX_PIN, OUTPUT);
#endif
//sleep(3);
#ifdef SNIFFERMODE
pinMode(TX_RX_PIN, INPUT);
while (1)
{
timeNow = millis();
newValue = digitalRead(TX_RX_PIN);
if (newValue != oldValue){
elapsedTime = timeNow - timeOld;
timeOld = timeNow;
}
if (elapsedTime > 25 && elapsedTime < 35)
break;
oldValue = newValue;
}
#endif
}
int findLowHighCount(int arr[], int len)
{
int counter = 0;
int highCount = 0;
int lowCount = 0;
while (counter < len)
{
if (arr[counter] == 1)
highCount++;
else
lowCount++;
counter++;
}
if (lowCount > highCount)
return (0);
else
return (1);
}
void setVoltage(unsigned short receivedData) {
// İlk 4 bit (Başlangıç Kodu) 0110 olmalı
unsigned short header = (receivedData >> 12) & 0xF; // En üst 4 biti al
// Ortadaki 8 bit sıfır olmalı
unsigned short middleBits = (receivedData >> 4) & 0xFF; // Ortadaki 8 biti al
// Kontroller
if (header == 0b0110 && middleBits == 0x00) {
unsigned short voltage = receivedData & 0xF; // Son 4 biti al
}
}
void setTemperatureLevel(unsigned short receivedData) {
// İlk 4 bit (Header) al
unsigned short header = (receivedData >> 12) & 0xF;
// Ortadaki 8 biti al
unsigned short middleBits = (receivedData >> 4) & 0xFF;
// Son 4 bit (Sıcaklık kademesi)
unsigned short temperatureLevel = receivedData & 0xF;
// Eğer ortadaki 8 bitin sağdan 3. biti `1` ise sıcaklık kademesini al
if ((middleBits & 0b00000100) != 0) {
unsigned short temperatureLevel = receivedData & 0xF;
temperatureLevel++;
}
}
void takeFeedback()
{
bool bits[16];
int feedCounter;
int feedBackTx[192];
int garbage[8];
int garbageCounter;
int tempCounter;
unsigned short receivedData = 0;
int rcCounter = 0;
vTaskDelay(1);
while(1)
{
if (digitalRead(TX_RX_PIN))
break;
}
feedCounter = 0;
tempCounter = 0;
while (feedCounter < 192){
/*
Serial.printf("%d, ", digitalRead(TX_RX_PIN));
vTaskDelay(1);
feedCounter++;
*/
garbageCounter = 0;
while (garbageCounter < 4)
{
garbage[garbageCounter] = digitalRead(TX_RX_PIN);
feedBackTx[tempCounter] = garbage[garbageCounter];
tempCounter++;
garbageCounter++;
vTaskDelay(1);
}
if (findLowHighCount(garbage, 4) != 1)
{
Serial.println("fail");
feedCounter = 0;
while (feedCounter < tempCounter)
{
Serial.printf("%d, ", feedBackTx[feedCounter]);
feedCounter++;
}
feedCounter = 0;
while (feedCounter < 200)
{
Serial.printf("%d, ", digitalRead(TX_RX_PIN));
feedCounter++;
}
Serial.println("");
return;
}
garbageCounter = 0;
while (garbageCounter < 4)
{
garbage[garbageCounter] = digitalRead(TX_RX_PIN);
feedBackTx[tempCounter] = garbage[garbageCounter];
tempCounter++;
garbageCounter++;
vTaskDelay(1);
}
if (findLowHighCount(garbage, 4) == 1)
{
receivedData = (receivedData << 1) | 1;
Serial.print("1, ");
rcCounter++;
}
else
{
receivedData = (receivedData << 1) | 0;
Serial.print("0, ");
rcCounter++;
}
garbageCounter = 0;
while (garbageCounter < 4)
{
garbage[garbageCounter] = digitalRead(TX_RX_PIN);
feedBackTx[tempCounter] = garbage[garbageCounter];
tempCounter++;
garbageCounter++;
vTaskDelay(1);
}
if (findLowHighCount(garbage, 4) == 1)
{
Serial.println("fail");
feedCounter = 0;
while (feedCounter < tempCounter)
{
Serial.printf("%d, ", feedBackTx[feedCounter]);
feedCounter++;
}
feedCounter = 0;
while (feedCounter < 200)
{
Serial.printf("%d, ", digitalRead(TX_RX_PIN));
feedCounter++;
}
Serial.println("");
return;
}
feedCounter+= 12;
}
for (int i = 15; i >= 0; i--) {
Serial.print((receivedData >> i) & 1); // Her biti tek tek yazdır
}
Serial.println();
Serial.println("Success");
}
int highCounter = 1;
int lowCounter = 1;
bool sendData1 = false;
bool sendData2 = false;
bool sendData3 = false;
bool sendData4 = false;
int process = 0;
int process1 = 0;
int switchh = 1;
void loop() {
#ifdef SNIFFERMODE
if (newValue == LOW)
lowCounter++;
else if (newValue == HIGH)
highCounter++;
if (newValue == LOW && oldValue == HIGH)
{
if (highCounter == 3 || highCounter == 5)
highCounter = 4;
else if (highCounter == 2)
highCounter = 1;
else if (highCounter == 7 || highCounter == 9)
highCounter = 8;
else if (highCounter == 649 || highCounter == 651 || highCounter == 652)
highCounter = 650;
Serial.printf("%dH, ", highCounter);
if (highCounter == 650)
Serial.println("");
highCounter = 1;
lowCounter = 1;
} else if (newValue == HIGH && oldValue == LOW)
{
if (lowCounter == 3 || lowCounter == 5)
lowCounter = 4;
else if (lowCounter == 29 || lowCounter == 31)
lowCounter = 30;
else if (lowCounter == 7 || lowCounter == 9)
lowCounter = 8;
Serial.printf("%dL, ", lowCounter);
if (lowCounter == 30)
Serial.println("");
highCounter = 1;
lowCounter = 1;
}
oldValue = newValue;
///Serial.printf("%d, ", newValue);
timeNow = millis();
newValue = digitalRead(TX_RX_PIN);
vTaskDelay(1);
#endif
#ifndef SNIFFERMODE
if (Serial.available()) {
char receivedChar = Serial.read();
if (receivedChar == '1') {
//pinMode(TX_RX_PIN, OUTPUT);
process = 1;
Serial.println("Veri gönderiliyor...");
} else if (receivedChar == '2') {
//pinMode(TX_RX_PIN, OUTPUT);
process = 2;
Serial.println("Veri gönderiliyor...");
} else if (receivedChar == '3') {
//pinMode(TX_RX_PIN, OUTPUT);
process = 3;
Serial.println("Veri gönderiliyor...");
} else if (receivedChar == '4') {
//pinMode(TX_RX_PIN, OUTPUT);
process = 4;
Serial.println("Veri gönderiliyor...");
}
}
// 4 low 8 high 4 low = 4080
// 4 low 4 high 8 low = 3840
// 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0
// 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0
//
if (process1 == 0)
{
digitalWrite(TX_RX_PIN, devamli[counter]);
counter++;
vTaskDelay(1);
if (counter == 128)
{
pinMode(TX_RX_PIN, INPUT_PULLDOWN);
counter = 0;
takeFeedback();
counter = 0;
pinMode(TX_RX_PIN, OUTPUT);
vTaskDelay(200);
//cevabiBekle();
if (process1 == process)
{
process1 = 0;
process = 0;
}
else
process1 = process;
}
} else if (process1 == 1)
{
digitalWrite(TX_RX_PIN, start_stop1[counter]);
counter++;
vTaskDelay(1);
if (counter == 128)
{
Serial.println("PRocess1 send!");
pinMode(TX_RX_PIN, INPUT_PULLDOWN);
counter = 0;
takeFeedback();
counter = 0;
pinMode(TX_RX_PIN, OUTPUT);
vTaskDelay(200);
//cevabiBekle();
if (process1 == process)
{
process1 = 0;
process = 0;
}
else
process1 = process;
}
} else if (process1 == 2)
{
digitalWrite(TX_RX_PIN, start_stop2[counter]);
counter++;
vTaskDelay(1);
if (counter == 128)
{
Serial.println("PRocess1 send!");
pinMode(TX_RX_PIN, INPUT_PULLDOWN);
counter = 0;
takeFeedback();
counter = 0;
pinMode(TX_RX_PIN, OUTPUT);
vTaskDelay(200);
//cevabiBekle();
if (process1 == process)
{
process1 = 0;
process = 0;
}
else
process1 = process;
}
} else if (process1 == 3)
{
digitalWrite(TX_RX_PIN, start_stop3[counter]);
counter++;
vTaskDelay(1);
if (counter == 128)
{
Serial.println("PRocess3 send!");
pinMode(TX_RX_PIN, INPUT_PULLDOWN);
counter = 0;
takeFeedback();
counter = 0;
pinMode(TX_RX_PIN, OUTPUT);
vTaskDelay(200);
//cevabiBekle();
if (process1 == process)
{
process1 = 0;
process = 0;
}
else
process1 = process;
}
} else if (process1 == 4)
{
digitalWrite(TX_RX_PIN, kazan_sicaklik[counter]);
counter++;
vTaskDelay(1);
if (counter == 128)
{
Serial.println("PRocess4 send!");
pinMode(TX_RX_PIN, INPUT_PULLDOWN);
counter = 0;
takeFeedback();
counter = 0;
pinMode(TX_RX_PIN, OUTPUT);
vTaskDelay(200);
//cevabiBekle();
if (process1 == process)
{
process1 = 0;
process = 0;
}
else
process1 = process;
}
}
#endif
}
/*Code end*/