I can run the code I added below on arduino mega. I can't run it even if I switch it to uno with the same connections. What do you think is the reason? I couldn't solve the problem for days.
#include <Wire.h>
#include <Adafruit_MLX90614.h>
#include <Servo.h>
#include <IRremote.h>
Adafruit_MLX90614 mlx = Adafruit_MLX90614();
Servo servoMotor;
IRsend irsend(3);
int ledPin = 13; // choose the pin for the LED
int inputPin = 11; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
int angles[] = {0, 90, 180}; // Açılar dizisi
// Horizontal soldan 1
unsigned int sendbuf34[] = {
2956, 9648, 476, 1560, 472, 532, 472, 532, 472, 532, 472, 1564, 472, 532, 472, 532, 472, 532, 472, 532, 476, 528, 472, 532, 472, 1564, 472, 532, 472, 532, 472, 1560, 504, 1532, 472, 532, 472, 532, 504, 500, 472, 548, 456, 1564, 472, 532, 472, 1564, 472, 1560, 504, 1532, 500, 1536, 500, 1532, 504, 1532, 504
};
// Horizontal soldan 3
unsigned int sendbuf36[] = {
2988, 9620, 504, 1528, 508, 500, 500, 500, 508, 496, 508, 1528, 504, 500, 504, 500, 504, 500, 504, 500, 504, 500, 504, 500, 504, 1528, 508, 500, 504, 496, 508, 1528, 504, 1532, 504, 500, 504, 500, 504, 500, 504, 500, 504, 1528, 504, 1532, 504, 500, 504, 1532, 504, 500, 504, 500, 504, 500, 504, 1528, 504
};
// Horizontal soldan 5
unsigned int sendbuf38[] = {
2988, 9620, 504, 1528, 504, 500, 504, 500, 504, 500, 504, 1532, 504, 500, 504, 500, 504, 500, 504, 500, 504, 500, 504, 500, 504, 1528, 508, 496, 508, 500, 500, 1532, 508, 1528, 504, 500, 504, 512, 492, 500, 504, 500, 504, 1532, 504, 1528, 504, 1532, 504, 1532, 504, 500, 504, 500, 504, 1528, 504, 1532, 504
};
// Economy %80
unsigned int sendbuf42[] = {
2952, 9640, 472, 1560, 472, 532, 472, 532, 496, 508, 500, 1532, 472, 532, 468, 532, 472, 532, 472, 1560, 472, 1560, 472, 532, 472, 532, 496, 508, 472, 528, 500, 504, 472, 532, 468, 532, 472, 1564, 468, 1564, 472, 1560, 472, 1560, 472, 1560, 472, 532, 472, 1560, 472, 532, 472, 532, 468, 532, 472};
// Economy %60
unsigned int sendbuf43[] = {
2984, 9612, 500, 1536, 500, 504, 496, 508, 500, 504, 496, 1536, 500, 504, 500, 500, 500, 504, 500, 1532, 500, 1536, 472, 532, 496, 504, 500, 504, 500, 504, 500, 504, 500, 504, 496, 504, 500, 1536, 500, 1532, 500, 1532, 500, 1536, 496, 1536, 496, 1536, 500, 504, 500, 504, 500, 504, 496, 504, 500, 1536, 496};
// Economy %40
unsigned int sendbuf44[] = {
2980, 9616, 500, 1532, 500, 504, 500, 504, 504, 500, 500, 1532, 504, 500, 500, 504, 500, 504, 500, 1532, 500, 1532, 500, 504, 500, 504, 500, 504, 500, 504, 496, 508, 500, 500, 500, 1536, 496, 504, 500, 504, 500, 504, 500, 504, 500, 504, 496, 508, 500, 500, 500, 504, 500, 1532, 500, 504, 500, 504, 472};
// TURN off
unsigned int sendbuf2[] = {
3020, 9484, 548, 1468, 548, 464, 524, 488, 524, 492, 520, 1492, 524, 492, 548, 464, 548, 464, 520, 1492, 524, 1492, 520, 492, 524, 492, 520, 492, 520, 492, 524, 492, 520, 492, 552, 460, 524, 492, 520, 492, 548, 464, 524, 492, 520, 1492, 524, 488, 524, 1492, 520, 492, 524, 492, 520, 492, 520, 1492, 524};
// Exit Economy Mode
unsigned int sendbuf45[] = {
2984, 9616, 500, 1532, 504, 500, 500, 504, 500, 504, 500, 1536, 496, 504, 500, 504, 500, 504, 500, 1532, 500, 1536, 500, 504, 500, 500, 500, 504, 500, 516, 488, 504, 500, 504, 496, 508, 496, 1536, 500, 1532, 500, 1536, 496, 1536, 500, 1532, 500, 1536, 500, 1532, 504, 500, 500, 504, 500, 1532, 504, 500, 500};
unsigned long previousMotionTime = 0; // variable to store the last time motion was detected
unsigned long currentTime = 0; // variable to store the current time
unsigned long delayStart =0 ; // Alternate code attemp for 'delay()' . 'delay()' blocks code for getting PIR signals constantly.
unsigned long delayStop = 0;
// Define the time thresholds for economy mode control (in milliseconds)
const unsigned long timeThreshold80 = 10000; // 10 seconds
const unsigned long timeThreshold60 = 25000; // 25 seconds
const unsigned long timeThreshold40 = 40000; // 40 seconds
const unsigned long timeThresholdOff = 60000; // 60 seconds
const unsigned long timeThresholdExitEconomy = 1000; // 1 seconds to exit economy mode immediately
bool sent80 = false;
bool sent60 = false;
bool sent40 = false;
bool sentOff = false;
bool exitingEconomyMode = false; // flag to control exiting economy mode
void setup() {
Serial.begin(9600);
mlx.begin(); // Sensörü başlat
servoMotor.attach(9); // Servo motorun sinyal pini
servoMotor.write(angles[0]); // İlk açıya getir
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
}
void loop() {
delayStop=millis(); // RTC'den anlık zaman değerini alır.
//Serial.print("Fark: "); // Sayacın işlediğini görmek için bu satırı açabilirsin !!
//Serial.println(String(delayStop-delayStart)); //
if ((delayStop-delayStart)>30000){ // Tarama işlemi bittiğindeki zaman ile anlık zaman arasındaki fark 10 saniyeden büyükse tekrar tarama yapar !
scanRoom(); // Odayı tarama fonksiyonuna gider
}
val = digitalRead(inputPin); // read input value from PIR sensor
if (val == HIGH) { // check if motion is detected
digitalWrite(ledPin, HIGH); // turn LED ON
if (pirState == LOW) {
// we have just turned on, i.e., motion detected after no motion
Serial.println("Motion detected!");
// We only want to print on the output change, not state
pirState = HIGH; // update the PIR state to HIGH
// Reset the flags to allow sending signals again
sent80 = false;
sent60 = false;
sent40 = false;
sentOff = false;
exitingEconomyMode = false;
}
// Reset the timer since motion was detected
previousMotionTime = millis();
} else {
digitalWrite(ledPin, LOW); // turn LED OFF
if (pirState == HIGH) {
// we have just turned off, i.e., motion ended after motion detected
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW; // update the PIR state to LOW
}
}
// Get the current time
currentTime = millis();
// Check the time elapsed since the last motion was detected
unsigned long timeSinceMotion = currentTime - previousMotionTime;
// Control the air conditioner's economy mode based on time thresholds
if (timeSinceMotion >= timeThresholdOff && !sentOff) {
// Turn off the air conditioner
sendSignal(sendbuf2, sizeof(sendbuf2) / sizeof(int));
Serial.println("Air conditioner OFF");
sentOff = true;
} else if (timeSinceMotion >= timeThreshold40 && !sent40) {
// Set the economy mode to 40%
sendSignal(sendbuf44, sizeof(sendbuf44) / sizeof(int));
Serial.println("Economy mode: 40%");
sent40 = true;
} else if (timeSinceMotion >= timeThreshold60 && !sent60) {
sendSignal(sendbuf43, sizeof(sendbuf43) / sizeof(int));
Serial.println("Economy mode: 60%");
sent60 = true;
} else if (timeSinceMotion >= timeThreshold80 && !sent80) {
// Set the economy mode to 80%
sendSignal(sendbuf42, sizeof(sendbuf42) / sizeof(int));
Serial.println("Economy mode: 80%");
sent80 = true;
} else if (timeSinceMotion >= timeThresholdExitEconomy && !exitingEconomyMode) {
// Exiting the economy mode
sendSignal(sendbuf45, sizeof(sendbuf45) / sizeof(int));
Serial.println("Exiting the economy mode!");
exitingEconomyMode = true;
}
}
float readTemperature(int angle) {
servoMotor.write(angle); // Servo motoru belirtilen açıya getir
delay(500); // Servo motorun konum alması için biraz bekleyin
float temp = mlx.readObjectTempC(); // Nesne sıcaklığını oku
return temp;
}
void scanRoom (){
float maxTemp = -273.15; // Minimum sıcaklık değeri
int maxTempIndex = -1; // En yüksek sıcaklık değerinin dizideki indeksi
for (int i = 0; i < 3; i++) { // 3 farklı açıyı tarayarak sıcaklık ölçümü yap
float temp = readTemperature(angles[i]);
Serial.print("Açı: ");
Serial.print(angles[i]);
Serial.print(" Derece, Sıcaklık: ");
Serial.print(temp);
Serial.println(" °C");
if (temp > maxTemp) {
maxTemp = temp;
maxTempIndex = i;
}
delay(1000); // Her açı için 1 saniye bekle
}
if (maxTempIndex != -1) {
// Determine the corresponding signal based on the highest temperature
if (maxTempIndex == 0) {
sendSignal(sendbuf34, sizeof(sendbuf34) / sizeof(int));
Serial.println("Sending signal: Horizontal 1");
} else if (maxTempIndex == 1) {
sendSignal(sendbuf36, sizeof(sendbuf36) / sizeof(int));
Serial.println("Sending signal: Horizontal 3");
} else if (maxTempIndex == 2) {
sendSignal(sendbuf38, sizeof(sendbuf38) / sizeof(int));
Serial.println("Sending signal: Horizontal 5");
}
// Move the servo motor to the angle with the highest temperature
servoMotor.write(angles[maxTempIndex]);
delayStart=millis(); // Tarama işleminin bittiği zamanı kaydeder.
}
}
void sendSignal(unsigned int *signal, int length) {
irsend.sendRaw(signal, length, 38); // kHz
delay(200);
}