Show Posts
|
|
Pages: [1] 2 3 ... 48
|
|
1
|
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.
|
|
|
|
|
4
|
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.
|
|
|
|
|
6
|
Using Arduino / Programming Questions / Re: Noob question regarding variable positioning in the sketch
|
on: May 05, 2013, 05:56:43 am
|
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: 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.
|
|
|
|
|
8
|
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.
|
|
|
|
|
9
|
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.
|
|
|
|
|
10
|
Using Arduino / Programming Questions / Re: Calling functions from string
|
on: May 03, 2013, 10:47:08 am
|
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 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 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. switch (character) { case 'A': functionA(); break; case 'B': functionB(); break; etc. }
|
|
|
|
|
11
|
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();"
|
|
|
|
|