Help please - simple idoit with newb problem

HI Guys,

sorry this is such a newb question…

running a simple bit of code to read pulses…this in itself is done…but for some reason i cannot get a trigger to work…

any idea please…

scenario…

we have pulses coming in, 3 states, when the state changes…for example pulses go over 1700, we change to state 3…when this happens i need to output a simple bit of code (already taken care of)

what i DONT want to do is continually send the same commands out when in state 3, only when it changes from state 2 to 3 …3 to 2…2 to 1…so forth and so on…

Here is my very simple starters of the code…

int zoomp = 7;
//int contr = 8;
int zcom = 0;
int ccom = 0;
int var = 0;
int prin = 0;

unsigned long duration1;
//unsigned long duration2;

void setup()
{
  pinMode(zoomp, INPUT);
  //pinMode(contr, INPUT);
  Serial.begin(38400); 
}

void loop()
{
  duration1 = pulseIn(zoomp, HIGH);
  //duration2 = pulseIn(contr, HIGH);
 
 //read pulses do summet..
  if (duration1 >1700)
  {
  zcom=1;
  
  }
  
  else if (duration1 < 1300)
{
  zcom=2;
 
  
}

else if (duration1 > 1300 && duration1  <1700)
{
  zcom=3;

  
}


//commands out if changes

if (zcom == zcom)
{ 
  prin = 0;
}

else 
{ 
  prin = 1;
  
}

if (prin == 0)

{
   Serial.println("Nout doing");
   Serial.println(prin,DEC);
}


else


{
switch (var)
{
  case 1:
    // statements
    Serial.println("Out");
 
    break;
  case 2:
    // statements
    Serial.println("In");
    break;
     case 3:
    // statements
    Serial.println("stopped");
    break;
  default: 
    // statements
     Serial.println("fuckknows");
  Serial.println(zcom,DEC);
 
}

}
var = zcom;
}

I have tried comparing the INT from every loop so when i changes it makes another INT 1 not 0 then do the case…but no luck…and i cant find any examples of this.

Im sure its really simple but i cant get it to work. :-[

Many thanks in advance fro any help.

Mike

if (zcom == zcom)

Well, that's something of a no-brainer.

If you don't want to do something because the state is the same as it was last time, you need to retain (i.e. store it somewhere) the old state and compare it to the new one.

Thanks Awol…

not being funny…but this is not my forte, been at this for a few hours now…

how would you go about doing such a thing?

believe me i wouldnt be asking if i could had found an example or a bif ot text explaing how to do it.

regards,

Mike.

Something like this:

int oldState = 0;
int newState = 0;

void loop()
{
   duration1 = pulseIn(zoomp, HIGH);
   if(duration1 > 1700)
     newState = 1;
   else if(duration > 1300)
     newState = 2;
   else
      newState = 3;

   if(newState != oldState)
   {
      // Send a message based on newState

      oldState = newState;
   }
}

Thanks Paul,

got my head around the storing and state changes...

many many thanks.

Mike.