Show Posts
Pages: 1 ... 117 118 [119] 120 121 ... 225
1771  Using Arduino / Programming Questions / Re: how to make public variables declared in setup() on: March 18, 2013, 03:51:58 pm
If you want a dynamically sized array, then you'll need to use malloc(). If you want to re-size it, then you'll need free() as well, but there is currently a bug with free() right now that can lead to memory leakage. I think it's fixed in 1.0.4; someone correct me if I'm wrong. It sounds like you are only sizing the array once, so a single malloc() call should suffice.
1772  Using Arduino / Programming Questions / Re: First attempt at combining the blink example with readanalogvoltage example on: March 18, 2013, 03:49:31 pm
Yes I was hoping if the analog voltage is great than 4V It would make the led pin13 come on...

Figure out what 4V means in terms of analogRead value (so you can avoid pointless floating point calculations) and write a simple if statement:

Code:
if (analogRead(somePin) > someValue)
  digitalWrite(someLED, HIGH);
else
  digitalWrite(someLED, LOW);
1773  Using Arduino / Programming Questions / Re: First attempt at combining the blink example with readanalogvoltage example on: March 18, 2013, 03:30:08 pm
Is the analog reading supposed to affect the LED in some way? If so, you have to define how you want it to change it. If not, just combine the setup functions, the loop functions and anything outside of them (global).
1774  Using Arduino / Programming Questions / Re: 'For' Loop on: March 18, 2013, 11:13:01 am
Quote
I am still getting '0' instead of '100' smiley-sad

Not surprising, considering all this code:

Code:
      int convert[7] = {senB, senBL, senBR, senML, senMR, senL, senR};                                // If distance = 0  ->  distance = 100
      temp = 100;                                                 
   
      for(int i = 0; i <= 6; i++) {                             
        if(convert[i] == 0) {                                   
          convert[i] = temp;           
        }
      }

Is doing is replacing any 0 value in your array with 100. I think this assignment is backwards:

Code:
convert[i] = temp; 
1775  Using Arduino / Project Guidance / Re: Floating Inputs on: March 18, 2013, 10:33:57 am
Say instead I'm worried about the input on a solid state relay

What do you mean by "input on a solid state relay"? Relays are output devices controlled by output pins.
1776  Using Arduino / Programming Questions / Re: millis() push button LED timer demo example coding on: March 18, 2013, 10:30:37 am
Code:
unsigned long elapsedTime;
...
  onTime = millis();
  onTime = elapsedTime;

All of this is pointless, get rid of it.

 
Code:
if (digitalRead(8) == LOW)   // Switch is closed to start LED timer
         {
          digitalWrite(12, HIGH);  // LED comes On 
          onTime = millis(); 
          }
      if(onTime > 0 && millis() - onTime > 5000); 
            {     
            digitalWrite(12, LOW);  // LED goes off
             onTime = 0;
            }

Fix your indentation

     
Code:
if(onTime > 0 && millis() - onTime > 5000); 
            {     
            digitalWrite(12, LOW);  // LED goes off
             onTime = 0;
            }

If all you are doing is turning the LED off and it is not controlled by any other means, the onTime > 0 and onTime = 0 aren't necessary.
1777  Using Arduino / Programming Questions / Re: millis() push button LED timer demo example coding on: March 18, 2013, 10:07:29 am
Code:
if(onTime > 0 && millis() - onTime > 5000);  

Why is there a semicolon at the end of this if statement?
1778  Using Arduino / Programming Questions / Re: millis() push button LED timer demo example coding on: March 18, 2013, 09:16:36 am
In the last thread, I named the variable lastLowReading for a reason. Going back, its probably better to call it lastLowReadingTime. Having meaningful variable names helps; trust me.
1779  Using Arduino / Programming Questions / Re: Merging 2 variables on: March 17, 2013, 03:08:26 pm
Do you have to start with the LSB and work towards the MSB?

Not necessarily:

Code:
bytes = (byte1 << 8) + byte2;

There's no notable difference, if any at all, between the two.
1780  Using Arduino / Programming Questions / Re: Adding a 3 second input delay, using 'delay' lesson for me. on: March 17, 2013, 01:15:16 pm
Simple non-blocking, more accurate approach:

Code:
static unsigned long lastLowReading = 0;

if (digitalRead(someInput) == LOW)
  lastLowReading = millis();

if (millis() - lastLowReading > 2000)
{
  // It's been HIGH for two seconds
}

Pretty simple concept. It updates a variable with the current time whenever it reads a LOW reading. That variable won't be updated when the reading is HIGH, so the difference between the current time and the recorded time will be how long it has been HIGH for. The only caveat would be that the second if statement will fire for as long as the input remains HIGH, so if you want to perform an action only once, you would need to implement a flag or latch variable.
1781  Using Arduino / Programming Questions / Re: LED and interrupt on: March 16, 2013, 11:23:35 pm
The problem is that you are misunderstanding the purpose of an interrupt. An interrupt doesn't mean stop whatever it is doing and go do something else; it means temporarily stopping what it's doing, handling the interrupt and then going back to what it was doing. Like when you're cooking dinner and the doorbell rings.

You should take a look at the Blink Without Delay example for a way to write non-blocking code that can handle performing certain actions at certain times.
1782  Using Arduino / Programming Questions / Re: Next step on: March 16, 2013, 09:17:13 pm
Using array is definitely the more elegant way. But right now you could do a series of digitalwrite commands for each button.

Using an array and combinational logic aren't mutually exclusive.
1783  Using Arduino / Programming Questions / Re: Next step on: March 16, 2013, 12:13:36 pm
Next step would be to better define your requirements.

Should these LEDs/Relays be latched when the button is pushed? What happens when none are pushed? What about when more than one are pushed? It would also be a good idea to learn about combinational logic.
1784  Using Arduino / Programming Questions / Re: concatenate an array to one float variable on: March 16, 2013, 12:04:40 pm
Well, considering that inData is big enough to handle the whole string. You might want to increase the size of the inData array.
In addition to adding the null terminator, those numbers need to be characters:

Code:
char inData[4]={'1', '2', '3', '.', '4', '5', '\0'};
1785  Using Arduino / Programming Questions / Re: problem in while language on: March 16, 2013, 12:02:44 pm
You mean nest an if statement within a while loop? Sure.
Pages: 1 ... 117 118 [119] 120 121 ... 225