Pages: [1]   Go Down
Author Topic: Loop problem  (Read 311 times)
0 Members and 1 Guest are viewing this topic.
Lanus
Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, I need to control a electrostatic deflector that has a TTL input. I need to send a some number of pulses and the rest of the time the signal must be in HIGH. I want to decide when are going to start my pulses pushing a button, the code is pretty easy but I dont understand why it doesn't made the number of pulses of I write in the for sentence, always send more. The idea is: press the button "play" the pulses and keep in high until the button is pressed again.
Thanks
Code:

void loop(){
   buttonState = digitalRead(buttonPin);
 
    if (buttonState == HIGH) {    
        for (int i=0; i <= numberOfPulses; i++){
        // set the LED on
      delay(10);              
      digitalWrite(13, LOW);  
      delay(10);
      digitalWrite(13, HIGH);
         }
        }
  else {
       digitalWrite(ledPin, HIGH);
   delay(100);
  }
}
 
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3368
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It sounds like you're problem is that the loop finished before you release the button, so it goes into another loop. Your best bet is to use signal edge detection to detect the instance when the button is being pushed down or released. To do that, you need to keep track of the last state of the button. If the last state was LOW and the current state is HIGH, then the button was just pushed down.
Logged

New Hampshire
Offline Offline
God Member
*****
Karma: 17
Posts: 781
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

loop is going to execute very fast.  Depending on which block of the if statement gets executed, at least 10 times a second and up to 50 times a second.  In the case of you pushing the button, it's getting executed 5 times a second, so if you hold the button down for longer than about 20ms, it's going to execute that block of code multiple time, and thus send out more pulses.

What you need is some one shot logic.  Keep track of both the previous button state and the current button state, and only execute the block of code when the previous state was low, and the current state is high (so basically creating a rising edge trigger).

To do that, add a second buttonstate variable to your code and name it previousbuttonstate.  Rename your current variable to currentbuttonstate.

Change your if condition to (currentbuttonstate == HIGH && previousbuttonstate == low).

Then at the end of your loop, set previousbuttonstate to currentbuttonstate.

Word of warning though, if you have any bounce in your button, you may still see undesirable pulses, in which case you'll either have to implement some debounce code, or add some debounce circuitry.
« Last Edit: June 12, 2012, 12:41:17 pm by jraskell » Logged


UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

A simple little cheat way is to wait for button release, then wait for button press at the start of your code:

Code:
void loop()
{
  digitalWrite(ledPin,HIGH); // Set the idle state
 
  while(digitalRead(buttonPin)==HIGH); // Wait for a release, if it is pressed.
  while(digitalRead(buttonPin)==LOW); // Wait for button to be pressed.

  for (int i=0; i <= numberOfPulses; i++)
  {
    // set the LED on
    delay(10);             
    digitalWrite(ledPin, LOW);   
    delay(10);
    digitalWrite(ledPin, HIGH);
  }
}

Of course, this doesn't fit well with a state machine if you ever want to expand your code to do other things.  If this is all it will do then this method is fine.
« Last Edit: June 12, 2012, 01:11:28 pm by majenko » Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Lanus
Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You are AWESOME, I love this forum!
Logged

Pages: [1]   Go Up
Jump to: