Go Down

Topic: Help please - simple idoit with newb problem (Read 478 times) previous topic - next topic

Dr Mike Black

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..

Code: [Select]
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

AWOL

#1
Jul 28, 2010, 09:54 pm Last Edit: Jul 28, 2010, 09:55 pm by AWOL Reason: 1
Code: [Select]
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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Dr Mike Black

#2
Jul 28, 2010, 10:04 pm Last Edit: Jul 28, 2010, 10:05 pm by mjblack Reason: 1
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.

PaulS

Something like this:
Code: [Select]
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;
  }
}

Dr Mike Black

Thanks Paul,

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

many many thanks.

Mike.

Go Up