Pages: [1]   Go Down
Author Topic: Help please - simple idoit with newb problem  (Read 434 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 29
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25861
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
« Last Edit: July 28, 2010, 02:55:16 pm by AWOL » Logged

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 29
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: July 28, 2010, 03:05:17 pm by mjblack » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Something like this:
Code:
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;
   }
}
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 29
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Paul,

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

many many thanks.

Mike.
Logged

Pages: [1]   Go Up
Jump to: