Using One Variable for two seconds then another to create a third

Hi,
I'd like to use one variable for two seconds then another to create a third.
output = pot for 2 seconds then,
output = pot / 2 going forward.
If possible,,,How?

Thanks,


int Pot;     //Pot reading
int PotD;    //Pot reading divided by 2
int Output;  // value that uses "Pot" for two seconds then uses "PotD"
int APOTI;
#define APOTI A1

void setup() {
  pinMode(APOTI, INPUT);
  Serial.begin(9600);
}

void loop() {
  int Pot = analogRead(1);
  (PotD = Pot / 2);

  delay(90);

  Serial.print("Pot  ");
  Serial.print(Pot);
  Serial.print("\t");
  Serial.print("PotD  ");
  Serial.print(PotD);
  Serial.print("\t");
  Serial.print("Output  ");
  Serial.print(Output);
  Serial.println("\t");
}

Hi Delta,
I been looking at the millis() function but I'm not clear on the proper syntax.

Hi Delta,

That runs but really slow

unsigned long previousMillis = 0;
const long interval = 3000;
int Pot;     //Pot reading
int PotD;    //Pot reading divided by 2
int Output;  // value that uses "Pot" for two seconds then uses "PotD"
int APOTI;
#define APOTI A1

void setup() {
  pinMode(APOTI, INPUT);
  Serial.begin(9600);
}

void loop() {
  int Pot = analogRead(1);
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (Output = Pot) {
      
    } else {
      Output = PotD;
    }

  (PotD = Pot / 2);

  delay(90);

  Serial.print("Pot  ");
  Serial.print(Pot);
  Serial.print("\t");
  Serial.print("PotD  ");
  Serial.print(PotD);
  Serial.print("\t");
  Serial.print("Output  ");
  Serial.print(Output);
  Serial.println("\t");
}
}

Hi Delta,
I got this to work, once.
I'd like it to reset when "Pot" changes.

Thanks for getting me this far

unsigned long previoustimeStamp;
unsigned long timeStamp;
unsigned long currenttimeStamp;
const long interval = 5000;
int Pot;     //Pot reading
int PotD;    //Pot reading divided by 2
int Output;  // value that uses "Pot" for two seconds then uses "PotD"
int APOTI;
#define APOTI A1

void setup() {
  pinMode(APOTI, INPUT);
  Serial.begin(9600);
}




void loop() {

  unsigned long timeStamp = millis();
  // unsigned long currentMillis = millis();
  int Pot = analogRead(1);
  if (timeStamp >= interval) {
    Output = PotD;
  } else {
    Output = Pot;
  }
  (PotD = Pot / 2);

}
  
  //previoustimeStamp = timeStamp;
  delay(90);

  Serial.print("Pot  ");
  Serial.print(Pot);
  Serial.print("\t");
  Serial.print("PotD  ");
  Serial.print(PotD);
  Serial.print("\t");
  Serial.print("Output  ");
  Serial.print(Output);
  Serial.println("\t");
}

look this over

const unsigned MsecPrint =  500;    // Capitalize Constants
const unsigned TwoSecs   = 2000;
unsigned long t0;
unsigned long msecLst;

int output;

char s [90];

// -----------------------------------------------------------------------------
void loop ()
{
    unsigned long msec = millis ();

    int pot = analogRead (A0);

    if (msec - t0 > 2000)
        output = pot / 2;
    else
        output = pot;

    if (msec - msecLst >= MsecPrint)  {
        msecLst += MsecPrint;

        sprintf (s, "pot %d, output %d", pot, output);
        Serial.println (s);
    }
}

// -----------------------------------------------------------------------------
void setup ()
{
    Serial.begin (9600);
    t0 = millis ();
}

Hi gcjr,
That works as well, but I'd like it to reset when "pot" is changed.
pot 402 output 201
pot 341 output 341
pot 341 output 341
pot 341 output 170
"pot" isn't a potentiometer output but changes like " 250, 200, 300, 325, 275" at various time intervals.

thanks,

ok. need to compare the new to old value, but since it's an analog reading, need a tolerance


const unsigned MsecPrint =  500;    // Capitalize Constants
const unsigned TwoSecs   = 2000;
unsigned long t0;
unsigned long msecLst;

int potLast;
int output;

char s [90];

// -----------------------------------------------------------------------------
void loop ()
{
    unsigned long msec = millis ();

    int pot = analogRead (A0);

    if (msec - t0 > 2000)
        output = pot / 2;
    else
        output = pot;

    if (msec - msecLst >= MsecPrint)  {
        msecLst += MsecPrint;

        sprintf (s, "pot %d, potLast %d, output %d", pot, potLast, output);
        Serial.print (s);
        sprintf (s, ", msec %8lu, t0 %8lu", msec, t0);
        Serial.println (s);
    }

    if (1 < abs(potLast - pot))  {
        potLast = pot;
        t0      = msec;
    }
}

// -----------------------------------------------------------------------------
void setup ()
{
    Serial.begin (9600);
    t0 = millis ();
}

Hi Delta_G,
My apologies on omitting that last parameter of my objective.
I try to keep things simple and concise for as not to get sidetracked.
GC's solution proved to be the resolution, although I don't need the "halving" on the decrease side it still works well for me.
Thanks to you and GC for taking the time to help me out.
I learn a little bit every time.

Thanks again

GC,
Thanks very much for helping me find a working solution on the first crack
(and for not blowing a gasket when I altered the parameters).

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