Go Down

Topic: project: power mitt (Read 6401 times) previous topic - next topic

Sigma-x

I still cannot fix the timing I will still need some help on this project

Sigma-x

ok I think I have the answer AWOL is right the delay IS the problem
the command delay isn't suited for real time events thus my problem
so my question is; Is there a way to preform this code without the delay command?

Code: [Select]
// Common Anode pins 2 and 5

//   A B C D    -> pins and segments they control
//   | | | |    -> segments

// Segments that make each number when lit:
// 0 => 0
// 1 => A
// 2 => B
// 3 => C
// 4 => D

// Arduino digital pins used to light up
// corresponding segments on the LED display
#define A 2
#define B 3
#define C 4
#define D 5

// Pushbutton connected to pin 14
#define BUTTON 14

// Common anode;
// on when pin is low
// and off when pin is high
#define ON HIGH
#define OFF LOW

int count = 0; // current display count
int val = 0;   // digital input from button
int lastVal = 0;     //previous state of the button

void setup() {
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(D, OUTPUT);
  pinMode(BUTTON, INPUT);
  zero();
}

void loop() {
  val = digitalRead(BUTTON);      //read the pushbutton input pin
   if (val != lastVal) {          // compare the buttonState to its previous state
    if (val == HIGH) {            // if the state has changed, increment the counter
     count++; 
  } lastVal = val; }  // save the current state as the last state, for next time through the loo
  delay(200);
    switch (count) {
      case 0:
        zero();
        break;
      case 1:
        one();
        break;
      case 2:
        two();
        break;
      case 3:
        three();
        break;
      case 4:
        four();
        break;
        count = -1;
        break;
      }
    }
 


// 0 => Empty
void zero() {
  digitalWrite(A, OFF);
  digitalWrite(B, OFF);
  digitalWrite(C, OFF);
  digitalWrite(D, OFF);
}

// 1 => Led 1
void one() {
  digitalWrite(A, ON);
  digitalWrite(B, OFF);
  digitalWrite(C, OFF);
  digitalWrite(D, OFF);
}

// 2 => Led 2
void two() {
  digitalWrite(A, OFF);
  digitalWrite(B, ON);
  digitalWrite(C, OFF);
  digitalWrite(D, OFF);
}

// 3 => Led 3
void three() {
  digitalWrite(A, OFF);
  digitalWrite(B, OFF);
  digitalWrite(C, ON);
  digitalWrite(D, OFF);
}

// 4 => Led 4
void four() {
  digitalWrite(A, OFF);
  digitalWrite(B, OFF);
  digitalWrite(C, OFF);
  digitalWrite(D, ON);
}

Sigma-x

Alright I know i'm close now but the effect still isn't in real time

Code: [Select]
// Arduino digital pins used to light up
// corresponding segments on the LED display
#define A 2
#define B 3
#define C 4
#define D 5

// Pushbutton connected to pin 14
#define BUTTON 14

// Common anode;
// on when pin is low
// and off when pin is high
#define ON HIGH
#define OFF LOW


// Variables will change:
long previousMillis = 0;   // will store last time LED was updated
int count = 0; // current display count
int val = 0;   // digital input from button
int lastVal = 0;     //previous state of the button

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 250;           // interval at which to blink (milliseconds)


void setup() {
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(D, OUTPUT);
  pinMode(BUTTON, INPUT);
  zero();
}


// needs work!


void loop()
// check if button pressed
{
  val = digitalRead(BUTTON);
  if(val == LOW)
  {
    if(count == 4) // if count is at 4 already, make it 0 again
    {
      count = 0;
    }
    else
    {
      count++; // otherwise go to the next count
    }
  }

  switch (count) {
  case 0:
    zero();
    break;
  case 1:
    one();
    break;
  case 2:
    two();
    break;
  case 3:
    three();
    break;
  case 4:
    four();
    break;
  }

  unsigned long currentMillis = millis();

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

  }
}


// 0 => Empty/loaded 
void zero() {
  digitalWrite(A, OFF);
  digitalWrite(B, OFF);
  digitalWrite(C, OFF);
  digitalWrite(D, OFF);
}

// 1 => Led 1
void one() {
  digitalWrite(A, ON);
  digitalWrite(B, OFF);
  digitalWrite(C, OFF);
  digitalWrite(D, OFF);
}

// 2 => Led 2
void two() {
  digitalWrite(A, OFF);
  digitalWrite(B, ON);
  digitalWrite(C, OFF);
  digitalWrite(D, OFF);
}

// 3 => Led 3
void three() {
  digitalWrite(A, OFF);
  digitalWrite(B, OFF);
  digitalWrite(C, ON);
  digitalWrite(D, OFF);
}

// 4 => Led 4 empty reload
void four() {
  digitalWrite(A, OFF);
  digitalWrite(B, OFF);
  digitalWrite(C, OFF);
  digitalWrite(D, ON);
}

Sigma-x

Nope it doesn't work I still need help with the sequencer
and the push button that commands it

Arrch

What are your expectations for this part of the code:

Code: [Select]

  unsigned long currentMillis = millis();

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

  }
}



From a quick glance at your last post's code, I gather you have four LEDs that you want to light in sequence for each button press including one part of the sequence where none are lit?

Sigma-x

yep that's right and from there I could modify the code much more

Arrch

#66
Apr 17, 2012, 03:56 am Last Edit: Apr 17, 2012, 04:01 am by Arrch Reason: 1
To simplify, I would break this project into two main parts:

  • Edge detection on the button

  • State machine for the LEDs


It's easier to break it down and accomplish the small tasks first, and build off of it. It will make it much easier to debug because you're only working on a small amount of code at a time. In your case, I would start with the debounce example to accomplish the first task. You only have to add two lines of code to that to accomplish the first task.

This point of the code is run when the debounce is done and the state is known for sure:
Code: [Select]
 if ((millis() - lastDebounceTime) > debounceDelay) {
   buttonState = reading;
 }


So to implement edge detection, you would just have to modify it to:
Code: [Select]
 if ((millis() - lastDebounceTime) > debounceDelay) {
   buttonState = reading;
   if ( (buttonState == HIGH) && (lastButtonState == LOW) ) {
     // Do Something
   }
 }


This new code will run "on the edge" either when the button is released, or pushed down depending on how you have the button hooked up. You can always reverse the HIGH and LOW if you need the opposite behavior.

You can test it by adding a serial print to the "//Do Something" to verify the correct behavior. At this point, you've completed the first of your tasks.
I would then add a function called something like "stateChange()" to the "// Do Something" area of the code and create that function outside to work on the second part.

Sigma-x

I see a divide and conquer technique sounds simple ok I'll give it a shot

Sigma-x

I think I may have the whole thing solved
take a look

Code: [Select]
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 14;   // the number of the pushbutton pin

// Arduino digital pins used to light up
// corresponding segments on the LED display
#define A 2
#define B 3
#define C 4
#define D 5

// Variables will change:
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin
int count = 0;               // current display count
int val = 0;                 // digital input from button
int lastVal = 0;             //previous state of the button

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0;    // the last time the output pin was toggled
long debounceDelay = 250;     // the debounce time; increase if the output flickers


void setup() {
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(D, OUTPUT);
  pinMode(buttonPin, INPUT);
  zero();
}

void loop()
//Trigger Function
{
  val = digitalRead(buttonPin); //read the pushbutton input pin
  if (val != lastVal) {        // compare the buttonState to its previous state
    if (val == HIGH) {          // if the state has changed, increment the counter
      count++;                   
    }
    lastVal = val;
  }            // save the current state as the last state, for next time through the loop

  switch (count) {
  case 0:
    zero();
    break;
  case 1:
    one();
    break;
  case 2:
    two();
    break;
  case 3:
    three();
    break;
  case 4:
    four();
    break;

  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);

  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH),  and you've waited
  // long enough since the last press to ignore any noise: 

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:
    buttonState = reading;
  }

  // save the reading.  Next time through the loop,
  // it'll be the lastButtonState:
  lastButtonState = reading;
}
}

// 0 => Empty
void zero() {
  digitalWrite(A, LOW);
  digitalWrite(B, LOW);
  digitalWrite(C, LOW);
  digitalWrite(D, LOW);
}

// 1 => Led 1
void one() {
  digitalWrite(A, HIGH);
  digitalWrite(B, LOW);
  digitalWrite(C, LOW);
  digitalWrite(D, LOW);
}

// 2 => Led 2
void two() {
  digitalWrite(A, LOW);
  digitalWrite(B, HIGH);
  digitalWrite(C, LOW);
  digitalWrite(D, LOW);
}

// 3 => Led 3
void three() {
  digitalWrite(A, LOW);
  digitalWrite(B, LOW);
  digitalWrite(C, HIGH);
  digitalWrite(D, LOW);
}

// 4 => Led 4 empty reload
void four() {
  digitalWrite(A, LOW);
  digitalWrite(B, LOW);
  digitalWrite(C, LOW);
  digitalWrite(D, HIGH);
}



From what I gathered this should work but I cannot tell
when I press the button the button goes hare and cycles all LED's rapidly
of another case the leds are slow to react but I believe this problem happens because of a few
things:

1)I'm using no resistors in the pushbutton and the noise from the pushbutton causes these effects
2)The button is bad itself
3)I did something wrong in the code...again

perhaps someone can test this code and see what results they get then I can compare to see what i'm doing wrong because I really want to finish this project soon

Sigma-x

The code is changed and improved but the thing is that it continues to work on it's own
now I think it's the button and I have no idea as to why the mitt continously fire even after the fourth shot

Code: [Select]

int triggerPin = 14;      // Pushbutton Trigger on Pin 4
int speakerPin = 12;     // Positive Lead on the Piezo
int blinkPin   = 13;     // Positive Leg of the LED we will use to indicate signal is received

int trigger;             // This is used to hold the value of the trigger read;
boolean fired  = true;  // Boolean used to remember if the trigger has already been read.

int maxShots   = 4;      // You can fire 4 safe shots;
int myShots    = 0;      // You can fire 4 safe shots;


void setup() {
  pinMode(blinkPin, OUTPUT);
  pinMode(speakerPin, OUTPUT);
  pinMode(triggerPin, INPUT);


  for (int i = 1;i < 4;i++) {
    digitalWrite(blinkPin, HIGH);
    playTone(900*i, 200);
    digitalWrite(blinkPin, LOW);
    delay(200);
  }

  Serial.begin(9600);
  Serial.println("Ready: ");
}

void loop() {
  senseFire();
    playTone(1000, 50); 
}       


void playTone(int tone, int duration) {
  for (long i = 0; i < duration * 1000L; i += tone * 2) {
    digitalWrite(speakerPin, HIGH);
    delayMicroseconds(tone);
    digitalWrite(speakerPin, LOW);
    delayMicroseconds(tone);
  }
}


void senseFire() {
  trigger = digitalRead(triggerPin);
  if (trigger == HIGH && fired == false) {
    // insert count
    Serial.println("Button Pressed");
    Serial.println("Firing Shot:");
    fired = true;
    myShots++;
    if (myShots >= maxShots) {
     
      Serial.println("Out of ammo");
      Serial.println("myShots");
    }
  } else if (trigger == LOW) {
    if (fired == true) {
      Serial.println("Button Released");
    }
    // reset the fired variable
    fired = false;
  }
}

Sigma-x

I was wondering could some one help out

Go Up