Show Posts
Pages: 1 ... 35 36 [37] 38 39 ... 84
541  Using Arduino / Programming Questions / Re: creating function that blink led by number of my choice on: May 26, 2013, 10:19:18 pm
Code:
void blinkred( char c_Pin, char c_Count  )
would
Code:
void blinkred( uint8_t u_Pin, uint8_t U_Count  )
be better style?

For me it is clearer that we are dealing with an unsigned number and not a character (even if the result may be the same).
542  Using Arduino / Programming Questions / Re: |= please help! on: May 18, 2013, 12:21:31 am
A |= B is the same as A = A|B

You can use the same for all the other operators (+=, *=, /=, %=, you get the idea). It is just a shorthand, just as A++ is the same as A+=1 or A=A+1.
543  Using Arduino / Programming Questions / Re: Piezo Switch input Analog or Digital, and detection on: May 12, 2013, 02:33:39 am
The piezo generates a lot more than 2-4V. Here are some references for you to read:

1. http://www.scienceprog.com/thoughts-on-interfacing-piezo-vibration-sensor/
This one has a comparator type output towards the end that may be what you are looing for.

2. http://leucos.lstilde.org/wp/2009/06/piezo-transducer-signal-conditioning/
More general on what happens when a piezo is pressed.
544  Using Arduino / LEDs and Multiplexing / Re: I'm lost and desperate. Need help with RGB's, 595 shifters, on: May 11, 2013, 07:12:13 am
On my site (link below in my signature) I have some sketches that drive seven segment displays using the 595 shifter.
545  Using Arduino / Programming Questions / Re: Help With TCS3200 Color Sensor (Understanding Output) on: May 06, 2013, 03:13:19 am
The output of the sensor is a frequency that is linearly proportional to the colour intensity. What you need to do is measure that frequency for R, G and B and then interpret what colour you have.

I wrote a library for this sensor and have a pdf as part of the distribution that explains how the sensor can be calibrated. It also has a few examples sketches, one of which is for calibration. If you are interested follow the link in my signature below.
546  Using Arduino / Programming Questions / Re: Blinking LED with two different delays using millis. on: May 06, 2013, 03:10:21 am
In my code repository (link below) there is a Multiblink example that uses a data table to define many LEDs at different frequencies and time intervals. The code is a bit complex, as it allows for very general things to happen, but you may be able to learn from it (or play with the LED patterns).
547  Using Arduino / Programming Questions / Re: Noob question regarding variable positioning in the sketch on: May 05, 2013, 05:56:43 am
Code:
long previousTime = 0;
long totalTime = (lightTime + nolightTime);

These should be unsigned. It probably isn't the problem but it will cause issues in the long run.

Here is your code with the if/else reformatted so they make sense to most people:
Code:
unsigned long previousfanTime = 0;
unsigned long lightTime = 1000;
unsigned long nolightTime = 500;
unsigned long fanTime1 = 2000;
unsigned long nofanTime1 = 1000;
//unsigned long fanTime2 = 1000;
//unsigned long nofanTime2 = 2000;
const int ledPin = 13;
const int light1 = 2;
const int fan = 3;
const int light2= 4;
const int valve = 5;
long previousTime = 0;
long totalTime = (lightTime + nolightTime);


// the setup routine runs once when you press reset:
void setup()
{               
  Serial.begin(9600);
  pinMode(light1, OUTPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(light2, OUTPUT);
  pinMode(valve, OUTPUT);
  pinMode(fan, OUTPUT);
  digitalWrite(ledPin, HIGH);
}

// the loop routine runs over and over again forever:
void loop()
{
  unsigned long currentTime = millis();

  if (currentTime - previousTime < lightTime)//this is what happens while the light is on
  {
    digitalWrite(ledPin, HIGH);

    if (currentTime - previousfanTime <= fanTime1)                                                  //si el tiempo actual es menor que el tiempo de encendido de ventilador
      //entonces encendido
    {
      digitalWrite(fan, HIGH);
    }
    else
    {
      digitalWrite(fan, LOW);                                                                         //si no apagado
    }
    //end
    if (currentTime - previousfanTime > (fanTime1 + nofanTime1))                                    //if time is over the total fan cycle, the fan cycle restarts
    {
      previousfanTime = currentTime;
    }
  }
  else // now we stablish the loop for the time interval between lightTime and lightTime+nolightTime
  //that is to say the complete cycle
  {
    digitalWrite(ledPin, LOW);
  }

  if (currentTime - previousTime >= totalTime)
  {
    previousTime = currentTime;
  }

  Serial.print("previousTime: ");
  Serial.println(previousTime);
  Serial.print("currentTime: ");
  Serial.println(currentTime);
  Serial.print("difference: ");
  Serial.println(currentTime - previousTime);
}

Check that the logic you have is correct. Also, is you could explain what this is supposed to do (ie, why you wrote the program) we can look at it with some context for this logic and make comments.
548  Using Arduino / Programming Questions / Re: how to run a loop n number of times on: May 05, 2013, 03:15:15 am
The syntax you gave is not 'while' but 'for'. While loop takes a boolean condition.
549  Using Arduino / Programming Questions / Re: how to run a loop n number of times on: May 05, 2013, 01:07:42 am
It all depends on what the experience is supposed to be.

If person1 is there, and person2 arrives, person 1 leave, person 2 is still there, and person 3 arrives before person2 leaves, etc, is the twitching supposed to happen again? What creates some interest in these sorts of displays is that the action happens when the person is detected and then it repeats after a little while if someone is still in front of the display. If not then it waits for the new person to arrive.

In this case the software is either in a state that is waiting for the sensor to trip, twitching or waiting for time to run out.

I guess it is totally dependent on what the OP wants to do, which is not clear from the info here.
550  Using Arduino / Programming Questions / Re: how to run a loop n number of times on: May 04, 2013, 01:00:15 am
Use a flag to set whether you are waiting for a person (sensor) or are waiting for some time to pass.

When you detect your person, use a for loop with the random number (for I=0; I<random number; i++) to twitch, then set a flag that enables the time code and disables the twitchy sensor code (use the if statement). Then wait for a period of time (look at the blink without delay example for how to do this or use delay() if you don't care about doing something else in between) after which you reset the flag and your twitchy code can run again.
551  Using Arduino / Programming Questions / Re: Calling functions from string on: May 03, 2013, 10:47:08 am
Quote
you'd probably want to offset the indices

This means that you make the zero element of the array the first character that you want to use, For example, if the character is 'A', then to use this as an offset you need to subtract the code for 'A' from the letters you are looking up, like
Code:
index = character - 'A';
array[index] is then the data you are after. IN this case this is the address of the function to call.

To set up the array
Code:
array[] = { functionA, functionB, etc };
just make sure that the functions are defined.

Calling the functions is a bit more complicated and you should look it up. AWOL gave you the search reference.

If you don't understand this method, then something like this may be easier for you.
Code:
switch (character)
{
case 'A': functionA(); break;
case 'B': functionB(); break;
etc.
}
552  Using Arduino / Programming Questions / Re: Calling functions from string on: May 03, 2013, 08:27:23 am
You cannot do this the way you are trying to do it. The function names need to be dereferenced by the compiler at compile time.

There are a number of different approaches:
1. You can set up an array of function pointers for each letter of the alphabet, then call the function based on the array lookup. This produces the most compact code.
2. Each letter can be in a switch/case statement and the function is called from the appropriate case. If you have only a few letters to handle this will probably use the least amount of memory
3. Use if statements, as in "if (letter == 'A') function_A();"
553  Using Arduino / Programming Questions / Re: how to ' convert data types to get only one decimal point ' ? on: May 01, 2013, 03:04:39 am
Riva probably has the easiest answer for you.

@ukhelibob - good question and I can't see an answer. Give other restrictions on floats, I expect probably not.

Update: here is a solution http://arduino.cc/forum/index.php/topic,146638.0.html
554  Using Arduino / Programming Questions / Re: how to ' convert data types to get only one decimal point ' ? on: May 01, 2013, 02:47:18 am
Use sprintf() to format the string before you use it, into a character array and then print the character array. There is an option to use a specific number of decimal points as part of the %format string (%.1f from memory, but better look it up).
555  Using Arduino / Programming Questions / Re: Count Down Code Options on: April 29, 2013, 02:24:28 pm
I have implemented something similar that counts down from 99 seconds that you could adapt. Look in the link below (signature block). Hope it is useful.
Pages: 1 ... 35 36 [37] 38 39 ... 84