Show Posts
Pages: 1 ... 7 8 [9] 10 11 ... 24
121  Using Arduino / Programming Questions / Re: DHT: Celsius to Fahrenheit Conversion on: June 10, 2013, 03:35:20 pm
IIRC the dht library has a method for returning either F or C.

A quick look in the library and yes.

Code:
t = dht.readTemperature();         // will return C
t =  dht.readTemperature(true);  // returns F
122  Using Arduino / Programming Questions / Re: Code a menu that won't interfere with process on: June 08, 2013, 03:51:06 pm
I have a menu in a project. I was concerned with someone starting to change something and then leaving it. Leaving the sketch sitting waiting for input. The way I dealt with it is to have a time out on my menu. Generally where I'm checking for button presses I also check to see if the menu time out has come and make it exit. One of my buttons also exits so it was the same behaviour on a time out. Also, every time a button is pressed or the pot is rotated the time out timer gets reset. It's only with inactivity that it will time out and quit.

Basically I have a global variable where I store the time of an activity like a button press. So that if the current time - the time out length is > last activity time then exit the menu and resume normal operation.

I also like to put time out timers in while loops. I remember the first time I type while() into an Arduino sketch I was struck by how they could be a serious problem if the exit conditions are never met.
123  Using Arduino / Programming Questions / Re: Help changing delay() to millis() on: June 04, 2013, 05:58:10 pm
To use timing instead of delay will mean that you have to call that function once per loop(). I see two blocks of stuff being done in that topOff function, one before the delay and one after. Do you want the first part to run every time topOff() is called and the second part only when a certain time has expired? Or once the top portion has executed it shouldn't execute again until the second part has run?

It's probably more helpful if you post all your code.
124  Using Arduino / Programming Questions / Re: what the function of command "delay", and why it important? on: May 21, 2013, 05:05:49 pm
A couple of things to keep in mind with the delay() function.

It blocks so while it is delaying nothing else can happen.

If you have a delay(1000) or more you should probably rethink your code. It should only be used for very brief delays, even one second is a long time.
125  Using Arduino / Programming Questions / Re: Random parts of code getting chopped off without error message on: May 13, 2013, 04:16:42 pm
My wild guess: you're out of RAM.

Wild guessing is all we can do without code.
126  Using Arduino / Programming Questions / Re: A 60 second timer on: May 08, 2013, 09:43:48 pm
As Toby says you know when you start just wait sixty seconds.

Arduino keeps the time. It just keeps it in a simple count. Trying to keep track of seconds is over thinking the problem.

Code:
/*
 watches for LOW signal  at alarmPin
 when detected delays for alarmDelay
 then turns on relay
 
 opening the circuit at alarmPin resets anytime
 */

const byte alarmPin = 2;   
const byte relayPin = 6;   
const unsigned long alarmDelay = 60000UL;

void checkAlarm(){
  static boolean tripped = false;
  static unsigned long trippedTime;
  if (tripped && digitalRead(alarmPin) == HIGH)
  {
    tripped = false;
    digitalWrite(relayPin, LOW);
  }
  if (tripped && millis() - alarmDelay > trippedTime)
  {
    digitalWrite(relayPin, HIGH);
  }
  if (!tripped && digitalRead(alarmPin) == LOW)
  {
    tripped = true;
    trippedTime = millis();
  }
}

void setup(){
  pinMode (alarmPin, INPUT_PULLUP);
  pinMode (relayPin, OUTPUT);
}

void loop(){
  checkAlarm();
}
127  Using Arduino / Programming Questions / Re: [SOLVED] "Hourglass" without delay - understanding if, while, & for loops on: May 07, 2013, 08:20:26 am
I don't change up the order for display. The leds are all in a nice line it's just that the pins aren't wired consecutively. I started with them mostly nice and in order at one time  smiley-grin. To adapt your Arduino setup to my code only requires putting in your particular led pin values into the ledPin[] array declaration.

The reason for using an array like this is that it becomes portable. Say that in your project you decided that you want to use interrupts for switches. Interrupts on UNO only work on pins 2 and 3. You would have to move those leds which breaks your code. With the array approach you would just move the leds to new pins and change those pin values in the array.

Also notice how my code can be changed easily for the number of leds. If you wanted this to work with 7 leds you would just remove a value from the array. The sketch will recalculate both the count of leds and the interval.

To randomly rearrange them you would have to change the ledPin array to just byte not constant. Then write a small function that rearranges the values in the array after each cycle.
128  Using Arduino / Programming Questions / Re: [SOLVED] "Hourglass" without delay - understanding if, while, & for loops on: May 06, 2013, 09:26:32 pm
Quote
The only issue I have after seeing it run is that the last pin (11) turns off and then back on so fast it appears to stay lit. My sketch turns it off for the entire interval time before turning all of them back on.

That simply requires moving a piece of code. I had it your way at first but didn't like waiting for it to turn on. LOL

Code:
const byte ledPin[] = {
  9, 7, 4, 5, 6, 8, 10, 11};
const byte ledCount = sizeof(ledPin) / sizeof(ledPin[0]);

void setup(){
  for (byte n = 0 ; n < ledCount; n++) pinMode(ledPin[n], OUTPUT);
}

void loop(){
  static byte pointer = ledCount;
  static unsigned long lastChangeTime;
  const unsigned long interval = 60000 / ledCount;
  unsigned long currentTime = millis();
  if (currentTime - lastChangeTime > interval)
  {
    lastChangeTime = currentTime;
    if (pointer == ledCount)
    {
      for (byte n = 0; n < ledCount; n++)
      {
        digitalWrite(ledPin[n], HIGH);
      }
    }   
    if (pointer < ledCount)
    {
      digitalWrite(ledPin[pointer], LOW);
    }
    if (++pointer > ledCount)
    {
      pointer = 0;
    }
  }
}

I just moved the bit that turns on the leds to inside the timing condition.
129  Using Arduino / Programming Questions / Re: [SOLVED] "Hourglass" without delay - understanding if, while, & for loops on: May 06, 2013, 08:16:34 pm
Hey, I solved it too. The biggest problem I see with the OP's code is that it expects all the led pins to be sequential and he makes absolute reference to them. With an array the physical sequence of the pins doesn't matter. As you'll see in my sketch.

Code:
const byte ledPin[] = {
  9, 7, 4, 5, 6, 8, 10, 11};
const byte ledCount = sizeof(ledPin) / sizeof(ledPin[0]);

void setup(){
  for (byte n = 0 ; n < ledCount; n++) pinMode(ledPin[n], OUTPUT);
}

void loop(){
  static byte pointer = ledCount;
  static unsigned long lastChangeTime;
  const unsigned long interval = 6667;
  unsigned long currentTime = millis();
  if (pointer == ledCount)
  {
    for (byte n = 0; n < ledCount; n++)
    {
      digitalWrite(ledPin[n], HIGH);
    }
  }
  if (currentTime - lastChangeTime > interval)
  {
    lastChangeTime = currentTime;
    if (pointer < ledCount)
    {
      digitalWrite(ledPin[pointer], LOW);
    }
    if (++pointer > ledCount)
    {
      pointer = 0;
    }
  }
}
130  Using Arduino / Programming Questions / Re: Program game does not work on chip, whats wrong with program on: May 02, 2013, 09:21:15 pm
When you say "presses the button" do you mean press and hold it or press and release it? Could I, as a player simply hold the button down?

I can see your description in your code but would suggest that you rename some variables. "State22" might be better as "player2buttonCounter". Use descriptive variable names and use copy / paste to avoid typos and undeclared variable errors.

131  Using Arduino / Programming Questions / Re: Program game does not work on chip, whats wrong with program on: May 02, 2013, 08:41:23 pm
Yes. How does the game work?
132  Using Arduino / Programming Questions / Re: Program game does not work on chip, whats wrong with program on: May 02, 2013, 08:22:11 pm
Forget code for a moment. Exactly how do you see your game playing out?
133  Using Arduino / Programming Questions / Re: Reading buttons to count! on: May 01, 2013, 05:15:54 pm
In C and C++ variables are very strictly typed and very tightly scoped. Your issue, as pointed out by Arrch is one of scope.

I highly recommend the tutorial at cplusplus.com. At least read the section on variables and data types.

http://cplusplus.com/doc/tutorial/
134  Using Arduino / Programming Questions / Re: Code help? Buttons? on: April 22, 2013, 08:23:05 pm
Don't use pins 0 or 1 they are used for serial. Move your switches to 2 and 3. I also recommend using the internal pullup resistors. You have no debouncing strategy.
135  Using Arduino / Programming Questions / Re: Numbered Variables In For Loop on: April 13, 2013, 02:39:01 pm
You're trying to use a technique for referencing an array on non-array type variables.

Code:
// an array declaration
int encoder[4];

//

for (byte j = 0; j < 4; j++)
{
     Serial.print("Encoder ");
     Serial.print(j + 1);            // array index start at 0
     Serial.print(" [");
     Serial.print(encoder[j]);    // the encoder value you are looking for
     Serial.print("]  ");             // you may want to make this one println()
}

// spot the difference?


You may want to read up more on using arrays. Try cplusplus.com
Pages: 1 ... 7 8 [9] 10 11 ... 24