Adding delay before transmitting data

I have an switch that has a linear potentiometer that I am using as a dimmer, that is connected to an RF transmitter.

I want to add a small delay between when the the user stops dimming, and the new signal is sent, because right now, the transmitter misses some of the movement of the slider if I move it too quickly.

I assume that is because the transmitter is sending data too slowly to keep up with the movements of the potentiometer.

I have tried adding a timer, but that didn’t work because I would only want to reset the timer if the position of the potentiometer changed, meaning that after a while, the transmitter would always transmit.

Im kinda out of Ideas right now so if anyone has anything, please let me know.

Here is my code:

#include <SPI.h>
#include <Wire.h>
#include <RH_ASK.h>
#include <Switch.h>
#include <buttons.h>

#define sliderPot A1


RH_ASK transmitter(2400, 12, 12, 12, false);

byte switchInformation[] = {0, 0, 0};
const byte RemoteZoneControl = 0;
byte dimValue = 0;
Switch onOff = Switch(6, INPUT_PULLUP, 10);
unsigned long transmitTime = millis() + 100;

void setup() {
  switchInformation[RemoteZoneControl] = 101;
  Serial.begin(57600);
  Serial.println("Switch");
  if (!transmitter.init()) {
    Serial.println("init failed");
  }
  
}

void loop() {
  int dimRead = analogRead(sliderPot);
  byte change = abs((dimRead >> 2) - dimValue);
  if (change > 4) {
    dimValue = dimRead >> 2;
    switchInformation[1] = dimValue;
    transmitTime = millis() + 500;
    transmit();
  }
  
  if(transmitTime < millis() && change > 2){
    Serial.println(transmitTime);
    Serial.print("b");
    Serial.println(millis());
    transmit();
  }
   
   
  onOff.poll();

  if (onOff.switched()) {
    Serial.println("Switched");
    if (switchInformation[2] == 1) {
      switchInformation[2] = 0;
      transmit();
    } else
    {
      switchInformation[2] = 1;
      transmit();
    }
  }
}

void transmit() {
  Serial.print(switchInformation[2]);
  transmitter.send((byte *)switchInformation, 3);
  transmitter.waitPacketSent();
}

I assume that is because the transmitter is sending data too slowly to keep up with the movements of the potentiometer.

What is the sender? What is the receiver?

I am not certain what you are asking, but I am using 433mhz module to receive and transmit data...

http://www.amazon.com/SMAKN®-433Mhz-Transmitter-Receiver-Arduino/dp/B00M2CUALS/ref=sr_1_1?ie=UTF8&qid=1430616189&sr=8-1&keywords=433mhz

the potentiometer and switch unit is transmitting the values of the potentiometer and a control box is receiving them.

I have verified that the transmitter is reading the potentiometer values correctly, but the receiver only receives them if I move the slider slowly.

Maybe the delay is being caused by the debugging statements. It has to talk to the serial, which takes time.

So, the problem is this:

the transmitter misses some of the movement of the slider if I move it too quickly.

Incidentally, never do this:

if(transmitTime < millis() && change > 2){

because millis() flips over and goes back to zero after 5 hours.

Maybe need something like this:

boolean amTransmitting = false;
unsigned long lastChangeMs;
byte dimValue;

void setup() {
}

void loop() {
  // never send anything more than .2 secs apart.
  // we check amTransmitting because lastTransmitMs is meaningless if this isnt set

  if(amTransmitting && (millis()-lastChangeMs) < 200) return; 

  boolean somethingHasChanged = false;

  // read the slider and handle it
  byte dimRead = analogRead(sliderPot) >> 2;
  if(abs(dimRead - dimValue) > 4) {
    dimValue = dimRead;
    switchInformation[1] = dimValue;
    somethingHasChanged = true;
  }

  // read the switch state and handle it
  if (onOff.switched()) {
    if (switchInformation[2] == 1) {
      switchInformation[2] = 0;
    } 
    else
    {
      switchInformation[2] = 1;
    }
    somethingHasChanged = true;
  }

  if(somethingHasChanged) {
    transmit();
    amTransmitting = true;
    lastChangeMs = millis();
  }
  else if(amTransmitting) {
    if((millis()-lastTransmitMs) < 1000) {
      transmit(); // resend the data
    }
    else {
      // ok, we have been se-sending repeat messages for a second. Time to stop doing it.
      amTransmitting = false;
    }
  }
  else {
    // no changes, no repeat transmissions to send, nothing to do!
  }
}

Something like that.

PS: you don't really need dimValue. You can just use switchInformation[1] - it will retain the data. And maybe dimRead shouldn't be called dimRead in my version of the sketch, because it doesn't contain the data that was read, it contains the new value that we want to be sending.

Also, whenever I have a variable or function parameter that stores a physical quantity (like milliseconds), I always suffix it with the unit of measurement. Thus "lastChangeMs". It would have saved that Mars mission where one programming team was using meters and the other side feet. If the functions had read like

float getBoundaryVelocityFps(float inputFps) {}

then they wouldn't have made that mistake.

because millis() flips over and goes back to zero after 5 hours.

No. millis() rolls over after 49 days. micros() rolls over a lot sooner.

PaulS: No. millis() rolls over after 49 days. micros() rolls over a lot sooner.

That might have been what I was thinking of.

The point is, though, if you set a time some time in the future and have a loop wait until the current time is greater than that time, then if that time in the future wraps around, then the current time will be greater than that time immediately.

But if you subtract the current time from that time in the future (or a time in the past from the current time), then even if one of the quantities has rolled over and the other hasn't, the mysteries of unsigned numbers will make the difference come out right.