Show Posts
Pages: 1 ... 35 36 [37] 38 39 ... 84
541  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.
542  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.
543  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.
544  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.
545  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.
}
546  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();"
547  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
548  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).
549  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.
550  Using Arduino / Displays / Re: Does anyone know how to address character map of a 16x2 LCD? on: April 27, 2013, 06:49:12 pm
My reading of documentation has been that the LCD displays come with one rom set. Most of the ones I have seen have been the Asian extended characters. I have not found any way to 'switch' the rom code to one of the others sets - they are different part numbers from the manufacturer.
551  Using Arduino / Displays / Re: Does anyone how to address character map of a 16x2 LCD? on: April 27, 2013, 03:31:09 pm
As far as I know you just use the ASCII code for the characters and they will work. Internally they may well be arranged as rows and columns, but that is too detailed for me to want to worry about when I am just trying to print a character.
552  Using Arduino / LEDs and Multiplexing / Re: RGB LED easy smooth colour transitions on: April 25, 2013, 06:41:14 pm
What Headroom said is correct. You wil notice that there are lower and upper thresholds for the LEDs defined in the code. This was to ensure that the really big jumpiness that you get at the lower end (when the LED suddenly turns on) was avoided. In hindsight, it may have been better to have a threshold for each colour (R, G, B) as they seem to respond differently. However, it was good enough for me at the time and I still enjoy my little LED changer on the desk at work smiley

@Headroom - Thanks, this was the first real project I did and on the Arduino early in 2012, about 2 months after I started with the hardware. Lots of water under the bridge since then and I post most of my reasonable stuff on the site in my signature below.
553  Using Arduino / Audio / Re: Piezo MIDI Drums on: April 25, 2013, 02:47:50 am
Quote
why it doesnt work with two midi msg at the same time?

Probably because of the 5ms delay waiting for the dignal. If you hit botrh at the same time one will be registered and the other has probably disappeared by the time you have finished checking and sent the messge out the port.

To fix this you would have to rewrite the functions without delay statements. One approach may be to just remove the delay you have now and just see if that works better.
554  Using Arduino / Programming Questions / Re: Help Needed changing PID constants using a DIP-Switch on: April 23, 2013, 05:01:22 pm
THe scope of your PID object is just within the loop function. Every time the loop function ends the PID object is delected and recreated again the next time tru loop. You need to make the PID object global scope or a static variable.
555  Using Arduino / Audio / Re: Piezo MIDI Drums on: April 23, 2013, 02:49:15 pm
I have tried to simplify the code to make it easier to debug. I have created a function that does everything your code was repeating once to avoid cut an paste and make things more consistent.

Code:
#define  MAX_PIEZO  5

uint8_t piezo[MAX_PIEZO] = { A0, A2, A4, A6, A8 };
uint8_t drumType[MAX_PIEZO] = { 5, 4, 3, 2, 1 };

const int threshold = 500;  

void setup()
{
  for (uint8_t i=0; i<MAX_PIEZO; i++)
    pinMode(piezo[i], INPUT);

  Serial.begin(57600);      
}

void MidiTX(unsigned char StatusByte, unsigned char Note, unsigned char Val)
{
  Serial.write(StatusByte);
  Serial.write(Note);
  Serial.write(Val);
}

void ProcessPiezo(uint8_t p)
{
  uint16_t sensorReading = analogRead(piezo[p]);
  uint16_t  val;

  if (sensorReading > threshold)
  {
    uint32_t oldcas = millis() + 5;
    
    // this part of the code looks complex
    // explain what you are trying to do?
    while (analogRead(piezo[p]) > threshold)
    {
      if (millis() >= oldcas)
      {
        val = analogRead(piezo[p]) / 8;
      }
    }
    
    MidiTX(144, drumType[p], val);
    MidiTX(128, drumType[p], val);
  }
}

void loop()
{
  for (uint8_t i=0; i<MAX_PIEZO; i++)
    ProcessPiezo(i);
}

So the question is what do you mean by delay? How long? milliseconds, seconds, minutes?

There is a 5ms delay built into you code when you detect a value > threshold, but I imagine that you don't mean that.
Your code also used int for a millis() value instead of unsigned long, so I changed that. In some cases that could have caused delays until the counter wrapped around again.

Try what is there now, verify that you still have a problem and then give us more details.

You can also try to capture the output using the serial monitor on the computer (avoid all the MIDI) and if the message is there instantaneously, you knoew the delay is after that in the MIDI software chain on the PC.
Pages: 1 ... 35 36 [37] 38 39 ... 84