Single shot analogue read

Untested, only compiled.
Generates identical warnings.

#include <IRremote.h>

const uint8_t analogPin = A0;
const uint16_t threshold = 405;

const uint32_t idleTime = 15000;
const uint32_t burstPacketSpacing = 40;
const uint8_t numberOfBurstPackets = 4;
const uint32_t burstSpacing = 1000;

enum sStates {
  wait15seconds,
  checkInput,
  sendFirstBurst,
  waitBetweenBursts,
  sendSecondBurst,
} pgmState = wait15seconds;

IRsend irsend;

void setup() {}

void loop() {
  static uint32_t lastStateSwitch;
  static uint8_t burstPacketCount;
  uint32_t topLoop = millis();
  switch (pgmState) {
    case wait15seconds:
      if (topLoop - lastStateSwitch >= idleTime) {
        pgmState = checkInput;
        lastStateSwitch = topLoop;
      }
      break;
    case checkInput:
      if (analogRead(analogPin) > threshold)  {
        pgmState = sendFirstBurst;
        burstPacketCount = 0;
        lastStateSwitch = topLoop;
      }
      break;
    case sendFirstBurst:
      if (topLoop - lastStateSwitch >= burstPacketSpacing) {
        irsend.sendSony(0x2B09, 15);// 2B09  15
        lastStateSwitch = topLoop;
        if (++burstPacketCount >= numberOfBurstPackets) {
          pgmState = waitBetweenBursts;
        }
      }
      break;
    case waitBetweenBursts:
      if (topLoop - lastStateSwitch >= burstSpacing) {
        pgmState = sendSecondBurst;
        burstPacketCount = 0;
        lastStateSwitch = topLoop;
      }
      break;
    case sendSecondBurst:
      if (topLoop - lastStateSwitch >= burstPacketSpacing) {
        irsend.sendSony(0x4B09, 15);// 4B09  15
        lastStateSwitch = topLoop;
        if (++burstPacketCount >= numberOfBurstPackets) {
          pgmState = wait15seconds;
        }
      }
      break;
  }
}

I could have shortend the code by using fallthrough on some (or most?) cases,
but that - and the optical clutter to get rid of the warnings -
would somehow hurt the example character of the sketch.

Good evening Whandall, I have uploaded your latest code, and unfortunately it doesn't work. It compiles and uploads with no problem, and wont accept the input till 15 seconds have elapsed - which does work. But it just lights the led on digiout 3 (I use a led to visually check the output untill I get it right). Unfortunately, you have blown my mind with this code, I don't write to this level yet, and as such I am really stuck.

So you can see 40 kHz signals?

I never used a visible LED on the IR output pin, and I'm not aware of the idle state.

If you attach a logic analyzer or an oszilloscope you should be able to see the signals.

As I said, I did not test the sketch, but it is merely a framework around your original
send statements (which are still deprecated...),
so it should emit the same signals as your OP.

And there is nothing special with the code.

You start by enumerating all the different operational states that make up your sketch,
then you select in loop which segment of the code has to be executed under the current state.

These code parts are usually very short and most of the time check only if the conditions for a
transition to a different state are met.

If so, you set up some variables for the new state,
note the time of the transition, and set the new state as current.

Divide et impera.

I feel a right plank now Whandall, I had placed the led reverse bias ith the anode on the 5 volt rail - should have been pwm-anode-cathode and then 0v. Yes it is working perfectly, I cannot thank you enough. I owe it to you now to figure out how it is working. Thankyou so much. Alistair

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.