Hi, I'm trying to get my sketch to work properly. It works ok but it needs to be better. What I want is, it reads A0 and if the voltage is over a value, it then sends an IR signal, waits for 1 second then sends another IR signal pulse, then delays by 15 seconds then starts again. I don't want it to read the analogue input A0 until the 15 seconds to start the routine again, but it does. When A0 goes high within the 15 seconds the routine will IR send-delay-send and its all out of sync. Here is my code (it may need cleaning up a tad but it works)
#include <IRremote.h>
// Define switch pin
const int analogPin = A0;
const int threshold = 405;
// Define a variable for the button state
int TimeComplete = 0;
// Create IR Send Object
IRsend irsend;
void setup()
{
}
void loop() {
// read the value of the input
int analogValue = analogRead(analogPin);
if (TimeComplete = 1); {
// if the analog value is high enough the send IR
if (analogValue > threshold) {
for (int i = 0; i < 4; i++) {
irsend.sendSony(0x2B09, 15);// 2B09 15
delay(40);
delay(1000);
{
for (int i = 0; i < 4; i++) {
irsend.sendSony(0x4B09, 15);// 4B09 15
delay(40);
}
TimeComplete = 0;
delay(5000);
TimeComplete = 1;
}
}
}
Sorry, I am using code tags now, here is the code I am using:
#include <IRremote.h>
// Define switch pin
const int analogPin = A0;
const int threshold = 405;
// Define a variable for the button state
int TimeComplete = 0;
// Create IR Send Object
IRsend irsend;
void setup()
{
}
void loop() {
// read the value of the input
int analogValue = analogRead(analogPin);
if (TimeComplete = 1); {
// if the analog value is high enough the send IR
if (analogValue > threshold) {
for (int i = 0; i < 4; i++) {
irsend.sendSony(0x2B09, 15);// 2B09 15
delay(40);
delay(1000);
{
for (int i = 0; i < 4; i++) {
irsend.sendSony(0x4B09, 15);// 4B09 15
delay(40);
}
TimeComplete = 0;
delay(15000);
TimeComplete = 1;
}
}
}
}
}
the single '=' sets the value to 1, you need to compare, so use '=='. And the ';' after the ) makes the if statment end, so the following will ALWAYS be run.
As far as I understand your description, I would write it as follows
#include <IRremote.h>
const uint8_t analogPin = A0;
const uint16_t threshold = 405;
IRsend irsend;
void setup() {
delay(15000);
}
void loop() {
uint16_t analogValue = analogRead(analogPin);
if (analogValue > threshold) {
for (int i = 0; i < 4; i++) {
irsend.sendSony(0x2B09, 15);// 2B09 15
delay(40);
}
delay(1000);
for (int i = 0; i < 4; i++) {
irsend.sendSony(0x4B09, 15);// 4B09 15
delay(40);
}
delay(15000);
}
}
Your usage of sendSony generates the following warnings
SomeWhere\FuenfzehnSekunden\FuenfzehnSekunden.ino: In function 'void loop()':
SomeWhere\FuenfzehnSekunden\FuenfzehnSekunden.ino:17:33: warning: 'void IRsend::sendSony(long unsigned int, int)' is deprecated: This old function sends MSB first! Please use sendSony(aAddress, aCommand, aNumberOfRepeats). [-Wdeprecated-declarations]
irsend.sendSony(0x2B09, 15);// 2B09 15
^
In file included from SomeWhere\Arduino\libraries\IRremote\src/IRremote.h:191:0,
from SomeWhere\FuenfzehnSekunden\FuenfzehnSekunden.ino:1:
SomeWhere\Arduino\libraries\IRremote\src/IRremoteInt.h:538:10: note: declared here
void sendSony(unsigned long data,
^~~~~~~~
SomeWhere\FuenfzehnSekunden\FuenfzehnSekunden.ino:22:33: warning: 'void IRsend::sendSony(long unsigned int, int)' is deprecated: This old function sends MSB first! Please use sendSony(aAddress, aCommand, aNumberOfRepeats). [-Wdeprecated-declarations]
irsend.sendSony(0x4B09, 15);// 4B09 15
^
In file included from SomeWhere\Arduino\libraries\IRremote\src/IRremote.h:191:0,
from SomeWhere\FuenfzehnSekunden\FuenfzehnSekunden.ino:1:
SomeWhere\Arduino\libraries\IRremote\src/IRremoteInt.h:538:10: note: declared here
void sendSony(unsigned long data,
^~~~~~~~
Ahh ok, I can see you are using to separate memory and you have put a delay of 15 seconds in void setup, this would only be used once. Could you please explain, just so I understand, I would be so greatfull. I am also using a nano if that helps too. Many thanks
I put the 15 seconds delay in setup, to suppress an eventual immediate send after startup,
if the value happens to be high.
As I understood your description, you want to send two bursts of packets spaced a second,
but don't want that to happen more often than once each 15 seconds.
If that is all you want to achieve, delays are absolutely ok.
If not, this small problem could make a good assignment
for the creation of a functional identical state machine, that does not use delay.
I see, yes that makes sense now, I would have never thought to put the delay in the setup, because that loop is still read before the void loop, nice work, I shall upload the code tomorrow when I get the nano from work. I take it the error messages you are getting from compiling is because you don't have the IRremote library?
No, it is because you are using deprecated functionality of the library,
or my library is a more recent version than yours.
Maybe we are using different libraries, I don't know yours.
Maybe I have warnings enabled, and you have not (which you should change).
name=IRremote
version=3.4.0
author=shirriff, z3t0, ArminJo
maintainer=Armin Joachimsmeyer <armin.arduino@gmail.com>
sentence=Send and receive infrared signals with multiple protocols
paragraph=Currently included protocols: Denon / Sharp, JVC, LG / LG2, NEC / Onkyo / Apple, Panasonic / Kaseikyo, RC5, RC6, Samsung, Sony, (Pronto), BoseWave, Lego, Whynter, MagiQuest.<br/><br/><b>New: </b><a href="https://github.com/Arduino-IRremote/Arduino-IRremote#converting-your-program-to-the-31-version">3.x upgrade instructions</a><br/>Added LG2 protocol.<br/>For all 3.x: Generation of PWM is now done by software by default, thus saving the hardware timer and enabling abitrary output pins. Removed decode_results results. Renamed most irparams_struct values. Support for more CPU's.<br/><b>New: </b>Adjusted LG timing and new LG2 protocol. Compiler switch USE_OPEN_DRAIN_OUTPUT_FOR_SEND_PIN added. Improved Attiny88 support. Renamed *.cpp.h to .hpp.<br/>
category=Communication
url=https://github.com/Arduino-IRremote/Arduino-IRremote
architectures=avr,megaavr,samd,esp8266,esp32,stm32,STM32F1,mbed,mbed_nano
includes=IRremote.h