Show Posts
Pages: 1 2 3 [4] 5 6 ... 24
46  Using Arduino / Programming Questions / Re: wind speed average on: January 17, 2014, 10:53:25 am
I took your code and added a way to store and average the last ten readings. I used your code hoping it would help you see what I did. Your code uses way too many floats. I'd store the actual analog readings which are an int and convert to a float when averaging and printing. It's best to minimize the use of floats where possible and with a little thought it's usually possible.

This basically uses the concept found in blink without delay for the timing of taking samples.

Code:
const int SENSOR_PIN = A0;
const int SAMPLE_COUNT = 10;
const unsigned long SAMPLE_RATE = 2000UL; // every two seconds

// array to store the samples
float sample[SAMPLE_COUNT];
byte sampleIndex = 0;

// function to average the samples
float sampleAverage(void)
{
  float accumulator = 0;
  for (int i = 0; i < SAMPLE_COUNT; i++)
  {
    accumulator += sample[i];
  }
  return accumulator / SAMPLE_COUNT;
}

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  static unsigned long lastSampleTime = 0;
  unsigned long currentTime = millis();

  // check if time for new reading
  if (currentTime - lastSampleTime >= SAMPLE_RATE)
  {
    lastSampleTime = currentTime;
    int sensorValue = analogRead(SENSOR_PIN);
    float voltage = sensorValue * (5.0 / 1023.0);
    Serial.print("voltage: ");
    Serial.println(voltage);
    // add the newest reading at the oldest pointer
    sample[sampleIndex] = voltage;
    // advance to next index which is now oldest
    sampleIndex++;
    // check for and handle rollover
    if (sampleIndex == SAMPLE_COUNT)
    {
      sampleIndex = 0;
    }
    // the sample array now contains the last ten readings
    // sampleIndex now points to the oldest reading
    // it will be replaced with the newest next time

    float average = sampleAverage();
    Serial.print("average:");
    Serial.println(average);
    float Wind_Speed = (6 * average);
    Serial.print("Wind_Speed:");
    Serial.print(Wind_Speed);
    Serial.println("m/s");
    float Wind_Kmph = 3.6 * Wind_Speed;
    Serial.print("Wind_Kmph:");
    Serial.print(Wind_Kmph);
    Serial.println("KMPH");
    Serial.println(".................");
  }
}


Hope this helps.
47  Using Arduino / Programming Questions / Re: Is it necessary to use pointers in Arduino C ? on: January 14, 2014, 11:41:31 am
I'm always learning more about programming and lately I've been trying to move more to C++ from C. When I read the title of this thread my first thought was Arduino is C++ and C++ can pass by referenece.

Is this not the C way?

Code:
void loop() [
   int val = 10;
   boolean flag = myFunction(&val);
   Serial.println(val);
}
boolean myFunction(int *v)
{
   if (*v == 10) {  // my compiler complained without the * here
      *v = 5;
      return true;
   }
   return false;
}

Is this the C++ way?

Code:
void loop() [
   int val = 10;
   boolean flag = myFunction(val);
   Serial.println(val);
}
boolean myFunction(int& v) // C++ pass by reference.
{
   if (v == 10) {
      v = 5; // no need to use * anymore
      return true;
   }
   return false;
}

That seems easier to me. I'm a self taught programmer and I've always struggled with pointers and managed to avoid them often. The problem is that to avoid them often means more global variables.
48  Using Arduino / Programming Questions / Re: issue with custom class on: January 03, 2014, 07:26:44 pm
Code:
   micOne.setPin(0);
   micTwo.setPin(1);

Bit of a shot in the dark but shouldn't that be A0 and A1? I always thought 0 and 1 were digital pins and A0 and A1 the first two analog pins for which 13 and 14 would also work. Which is what your wiring diagram shows.

I'm not sure why it would seem to work though.

Well, a little rudimentary testing shows that I was wrong about that. I guess analogRead() is smart enough to only use analog pins.
49  Using Arduino / Programming Questions / Re: error: expected unqualified-id before '{' token on: January 03, 2014, 11:43:33 am
Code:
if (boolean leds[int LEDno] == 1)//if the led is on...

You keep redeclaring variables. There's two in that statement alone. A variable only needs to declared once. When you have a type such as byte, int, unsigned long, followed by a variable name you have declared a variable. To refer to a variable after it's declared you only need its name.
50  Using Arduino / Programming Questions / Re: error: expected unqualified-id before '{' token on: January 03, 2014, 11:10:34 am
It would appear to me that you have one semicolon too many. Especially the one right after

Code:
void actual_animate();

Looks like that mistake may have been repeated.

A function should just look like
Code:
void functionName (void)
{
    // code block
}

No semi colons between the () and {}

ETA I notice you call the functions improperly as well. You do not need the void part which is declaring the return type of the function.

Code:
// call as
functionName();

// not
void functionName();

51  Using Arduino / Programming Questions / Re: Reducing pot flickering with code based solutions? on: January 02, 2014, 03:55:33 pm
I had a pot that worked like that. Turns out it was a poor choice of value and a 10k linear worked fine. In the meantime I fixed it by ignoring readings that hadn't changed more than two units. A solution that may not suit all projects.
52  Using Arduino / Programming Questions / Re: Arduino UNO Keyboard Commands on: December 27, 2013, 12:39:03 pm
You need a Leonardo to do this. The best an UNO can do is send that out to serial. The Leonardo has the ability to appear as a keyboard to a computer and has various methods for sending keystrokes. I used one to make a sequential shifter for racing games.
53  Using Arduino / Programming Questions / Re: Set all LOW digitalWrite on: December 23, 2013, 12:00:27 pm
You need to use an array and reference your pins through it. Right now you are using an absolute reference which will break if you change one pin. Using the iterator value from a for loop to point at the pins is not a good idea. Once the pin values are held in an array it's very simple to operate on all of them or one of them.

Code:
// first declare an array of your pin values
byte pin[] = {2, 4, 6, 8};
// then calculate it's size. Now if you add a pin it will automatically include it
byte pinCount = sizeof(pin) / sizeof(pin[0]);

void setup() {
    // using a for loop to reference all the pins
    for (byte i = 0; i < pinCount; i++)
    {
        pinMode(pin[i], OUTPUT);
    }
}
void loop() {
    // this will turn on all leds with a delay between each
    for (byte i = 0; i < pinCount; i++)
    {   
        digitalWrite(pin[i], HIGH);
        delay(delayTime);
    }
    // turns them all off the same way
    for (byte i = 0; i < pinCount; i++)
    {   
        digitalWrite(pin[i], LOW);
        delay(delayTime);
    }
    // turns on only one led pointed to by ptr
    byte ptr = 2; // the led on pin 6 or element pin[2] in the array
    for (byte i = 0; i < pinCount; i++)
    {   
        if ( i == ptr )
        {
            digitalWrite(pin[i], HIGH);
        }
        else
        {
            digitalWrite(pin[i], LOW);
        }
    }
    delay(2000UL); // pause each loop()
}

Is that what you are looking for? At the very least you should be using an array. Notice how with an array you can wire your leds to any pin in any order. Just fix the order in the array declaration. You could move the led on pin 6 to pin 7, change the 6 to a 7 in the array declaration and that's it. With your code it would break and the only fix would be to return the led to pin 6. That's because of your absolute reference to it. You'll get an error if you try to compile that because the delayTime variable is undeclared. I just left it from your code.
54  Using Arduino / Programming Questions / Re: How to open two relays at the same time? on: December 20, 2013, 05:24:31 pm
Until you understand the Blink Without Delay you can use this:
Code:
if (d > e)
 {
   delay (d-e);
 }
else
 {
   delay (e-d);
 }

I think it's neat that this does the same thing. Also  a pretty clear demonstration of why single letter variable names are a bad idea. It's almost unreadable.

Code:
delay( d > e ? d - e : e - d );
55  Using Arduino / Programming Questions / Re: How to open two relays at the same time? on: December 20, 2013, 09:38:57 am
Code:
    while(startime-millis() > e)
    {
      digitalWrite(PUMP_PIN2, LOW);
    }

Like Paul says you've just created a delay. The sketch is stuck in that loop until it's finished. Anytime the code gets stuck in a block or on a statement it is said to be blocking. You don't want this. delay() has its uses but I've found that if I need a delay of more than 100 millis I rethink my problem. Arduino can do a lot in 100 millis.

We use a conditional statement to see if we need to change a pin's state. Once a pin is written LOW or HIGH it will stay that way until your sketch changes it.

Code:
   
 // this just checks if the duration, e ( a really poor variable name), has passed
// if it has then the pin is written LOW otherwise it just goes on to other things. Like checking on the other relay.
if (millis() - startime >= e)
    {
      digitalWrite(PUMP_PIN2, LOW);
    }

You'll find that you can't simply make this change and have it work. It will turn your relay back on right away. Probably need another variable to keep the machine's state so you can skip turning the relays on if the relays are on.

You're getting close.  smiley-grin
56  Using Arduino / Programming Questions / Re: "!!!" in a string make the Upload to a Mega256 hang on: December 19, 2013, 12:39:34 pm
Apparently it's a known issue. Probably won't be fixed as there is no good reason to ever use !!! as punctuation.

Well it was fixed, in form of a new mega bootloader that was causing the problem.

It seems I need to keep up on current events.
57  Using Arduino / Programming Questions / Re: "!!!" in a string make the Upload to a Mega256 hang on: December 19, 2013, 12:13:15 pm
Apparently it's a known issue. Probably won't be fixed as there is no good reason to ever use !!! as punctuation.
58  Using Arduino / Programming Questions / Re: Annoying IDE behavior on: December 18, 2013, 12:15:23 pm
Are you familiar with using shift and cursor keys to highlight text? Try it.

Then you find handy shortcuts like [home] - [shift end] will highlight a line.

Place the cursor at the start of a line and use [shift up], see what happens.

[shift ctrl cursor] will highlight a word.

Very useful and keeps you from reaching for the mouse all the time.
59  Using Arduino / Programming Questions / Re: How do i make an action last a certain amount of time? on: December 15, 2013, 04:09:29 pm
To do that i need it to perform an action for an x amount of time.

If the current time - the start time >= x amount of time

You can stop doing the thing you're doing.
60  Using Arduino / Programming Questions / Re: Need help! on: December 12, 2013, 11:32:20 am
I need a example for me to understand as i have been stuck at this algorithm for weeks and unable to get a solution.

Did those weeks of work result in any code? Now would be a good time to post it.

Most people here will want to know what you have tried so far.
Pages: 1 2 3 [4] 5 6 ... 24