Show Posts
Pages: 1 [2] 3 4 ... 25
16  Using Arduino / Programming Questions / Re: Stuck on loop problem. on: May 06, 2014, 07:59:36 pm
You're handling your buttons wrong. You are simply looking for the state you want then acting. What you want to do is watch for state changes then act on that state.

Keep a variable with the old reading. Compare with a current reading. If they are not equal then you have detected a state change. This is where you might start debouncing (I call this a pending state change) if it passes debouncing you can now use the state to do what you want. For example turn on a led if LOW or do nothing if HIGH.

Check out some of the button examples, especially state change detection and debouncing. Read every post written by Paul S.
17  Using Arduino / Programming Questions / Re: Calculating slope using array issues on: April 18, 2014, 09:29:04 am
Isn't slope only useful when one of the axis is linear? I'm genuinely curious. The readings are being timed but time is ignored in the graph. If x was time the zero denominator problem doesn't exist.

The OP is plotting displacement (y) against shaft force (x). If shaft force keeps increasing shouldn't he watch for regular intervals in shaft force then read displacement and plot? If shaft force isn't always rising wouldn't the resulting graph be a scatter graph? You can't find the slope of such a graph.

I'm really just curious.
18  Using Arduino / Project Guidance / Re: void loop() 1 void loop() 2 on: April 18, 2014, 08:46:12 am
Time everything using millis() instead of using delay() which blocks the sketch. Blocking means that nothing else can happen. Timing means starting something, noting the time and then checking whether it is time to start something else or stop something. The blink without delay example shows this approach. Once you get the hang of it, it's easy.
19  Using Arduino / Programming Questions / Re: Calculating slope using array issues on: April 17, 2014, 11:14:50 pm
Shouldn't one of the axis represent time?
20  Using Arduino / Programming Questions / Re: Checking State of LED inside an array? on: April 12, 2014, 02:34:08 pm
Yep, pretty much useless for anything.
21  Using Arduino / Programming Questions / Re: Checking State of LED inside an array? on: April 12, 2014, 02:20:36 pm
Code:
int ledPins[] = { 4,5,6,7,8,9,10,11 };
 int amountLeds = 8;

for( int k =0; k< 8; k++ ){
    if(LEDarray[k] == digitalRead(HIGH)){
     Serial.print("0");
   }

Nope.

Code:
int amountLeds = 8;
int ledPins[amountLeds] = { 4,5,6,7,8,9,10,11 };
 
for( int k = 0; k < amountLeds; k++ )
{
    if ( digitalRead(ledPins[k]) == HIGH )
   {
     Serial.print('0');
   }
}
22  Using Arduino / Programming Questions / Re: Attempting to shut off a Solenoid valve after it remains at the threshold? on: April 11, 2014, 09:30:32 pm
It looks to me like you want to monitor some frequencies until they reach a threshold, then turn on something for ten seconds or the frequencies drop below the threshold. Sound right?

monitor frequencies
if flag false and threshold reached, turn on valve, get time, set flag
if flag true and below threshold, turn off valve, reset flag
if flag true and run time has passed, turn off valve, reset flag
rinse, lather, repeat
23  Using Arduino / Programming Questions / Re: Using millis() to create a single time delay on: April 10, 2014, 12:55:56 pm
A boolean is just a variable type like int. So you just declare it that way.

Code:
boolean flag = false; // or true, or unitialized
// flag can any be valid variable name
boolean areTheZebrasRunningAway = false;

24  Using Arduino / Programming Questions / Re: Any suggestions how to organize this? on: April 10, 2014, 11:46:21 am
I do it similar to what Caltoa is saying. I just did a temperature monitor, logger with LCD display and it's programmed so that loop() mostly does nothing but keep checking if it should do anything. Some items rely on millis for timing and some rely on seconds. As it has an RTC and uses time.h I use the now() function just like millis(). In my sketch the busiest thing loop() does is turn the colon in the time display on or off every one second.
25  Using Arduino / Programming Questions / Re: Arduino Uno as a HID Keyboard PROBLEM on: April 09, 2014, 10:16:28 am
Quote
But I have the Pin set high if the button is not pressed. so Why it should not be counted when its low?
600ms is because I don't want to allow the button being pressed that frequently.

That means you're debouncing the user, not the switch. If you use the state change detection approach you only act when the state of the switch changes. The user could hold it down for minutes and it will only count once. When you simply check the state of the switch you increment the counter every time it checks, if the switch is pressed. Your work around is the long delay giving the user time to release the switch. This is debouncing the user. I used to do that until I figured it out.
26  Using Arduino / Programming Questions / Re: Can't break out of a loop with a keypad on: April 08, 2014, 10:18:39 pm
Code:
if(digitalRead(10 == LOW))

That's one of those pesky syntax errors that will still compile but not do what you think it should. If I'm not mistaken it will read pin 0 because 10 != LOW so the comparison is false which is == 0. I'm sure you want to compare the result of reading pin 10 with LOW. Which brings up something else, what's at pin 10? Give it a better name than 10. Assign 10 to a const variable name and use the name.

Assuming pin 10 is a button switch.

Code:
const byte BUTTON_PIN = 10; // now pin ten has a name
// read it and make a comparison
if (digitalRead(BUTTON_PIN) == LOW) {break;}

In C++ it helps to have a basic understanding of how functions accept parameters and return values.
27  Using Arduino / Programming Questions / Re: Light Timer on: April 05, 2014, 03:07:39 pm
LOL, I thought the exact same thing as Paul.

What the others are telling you is that you are reading the button but not using the results.  Without seeing how buton is declared we can only guess that it is the pin number of the switch.

I've added comments to your code.

Code:
void loop()
{
digitalRead(buton); // this reads the pin at buton then does nothing with the result
   if(buton == HIGH) // this is, presumably, comparing a pin value to HIGH.
   {
   analogWrite(ledBrightness, 255);
   
   delay(5000); // bad idea but a different topic
   }
   
   else
   {
   analogWrite(ledBrightness, 120); 
   }
}

Here is how I'd fix  what you have posted.

Code:
void loop()
{
   int buttonState = digitalRead(buton); // buttonState now has the result of the read
   if(buttonState == HIGH) // we use the read result for our condition not the pin number
   {
   analogWrite(ledBrightness, 255);
   
   delay(5000); // bad idea but a different topic
   }
   
   else
   {
   analogWrite(ledBrightness, 120); 
   }
}

This all is based on the assumption that buton is a pin number. This is why you should post all your code. Buttons and switches often don't work because they are not handled properly in setup() but without all the code. Who knows?
28  Using Arduino / Programming Questions / Re: Will this "break" statement work correctly. on: March 26, 2014, 08:28:53 pm
You want to break out of the while loop if the delay in the if statement gets executed? No it won't work the way you have it. Try setting MILLIS_NOW = TIMEOUT_START in the if statement. That will result in a condition that will exit the while loop. A flag could also be used. If the flag is not set stay in the while loop and set it in the if. This would be an or type of condition.
29  Using Arduino / Programming Questions / Re: Turning YYYY to YY on: March 22, 2014, 07:54:34 pm
year - 2000 should keep you going until the next century.
30  Using Arduino / Programming Questions / Re: simple sketch for switch and relay on: March 21, 2014, 08:19:49 pm
Because when you are an unsigned value with only n number of bits available 0 - 1 != -1

Code:
void setup() {
  Serial.begin(115200);
  unsigned int x = 0;
  Serial.println(x - 1);
}

void loop() {

}

So it isn't an issue when done properly and you seem to have a that figured out. Always subtract from millis() and don't worry about it. You can find more in depth discussions about it.
Pages: 1 [2] 3 4 ... 25