Hi I would like to know how to use the condition ''for '' in an program plz
thank you
Have you read this?
An example is that when you take a reading from a sensor, one reading may be way off, perhaps due to external factors like AC near the sensor, or some other "funnies". So I take 100 readings and average them. Here's the code that does that, where the variable "x" is just a counter to see how many times we've been through....
// read and calc the temp
temptotal=0; //where we keep the running total
for(int x = 0; x < 100; x++) { //setup the loop, which will go from 0 to 99, ie 100 times
rawVal=analogRead(5); //read the sensor
temp=(500 * rawVal) /1024; // convert
temptotal = temptotal + temp; //update the running total
} //go back to the "for" line
temp=temptotal/100; //average
thank you
can i do it one time until an event ?
I don't know what you mean- it's purpose is not to go one time- it loops according to the values set for the counter.
So mine went from 0 to and looped while x<100, ie until 99.
100 times through: x=0, x=1, x=2..... x=98, x=99 and exits the "for" and goes to next line, in my case divide the total by 100.
but the question is where to insert this condition and is it obligatorial to insert increment or decrement
here is my code;
float prevtension;// global variables are retained on each iteration of loop()
int valeurLue;
int analogPin = A0;
const int led = 13;
float tension; //variable pour stocker la valeur lue après conversion
void setup() {
pinMode(analogPin, INPUT);
pinMode( led, OUTPUT );
digitalWrite(led, HIGH);
delay (1000);
digitalWrite(led, LOW); //
Serial.begin(115200);
}
void loop() {
prevtension = tension;
valeurLue = analogRead(analogPin);
//on traduit la valeur brute en tension (produit en croix)
tension = valeurLue * 5.0 / 1024;
if (tension > 2.53)
{if (tension < prevtension) { // compare them
digitalWrite(led, HIGH);
delay (200);
digitalWrite(led, LOW); // do something, they are different
{if (tension > 2.52){}
} }
}
Serial.print("previous= ");
Serial.println(prevtension);
Serial.print("current= ");
Serial.println(tension);
Serial.println();
Serial.println();
}
Moderator edit:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.
Upload your code using the # during the comment.
Or use [/code] for the end and
for the beginning of the code.
You can use different increments using a while loops as well or you can just make different conditions like if(one for loop with i++) else (another for loop with i--).
but the question is where to insert this condition and is it obligatorial to insert increment or decrement
Only you can answer the first question, since only you know what you want to do...
And the second question: Did you read the instructions I linked?
Any or all of the three header elements may be omitted, although the semicolons are required.
What is your event, an interrupt?
And are you just trying to control the loop() function rather than a loop construct like for or while?
pYro_65:
What is your event, an interrupt?
And are you just trying to control the loop() function rather than a loop construct like for or while?
OP has 4-5 threads going, none of which is actually clear to me, but which seem to be related while subtly different......
This is like the fourth post this guy's made asking the same question. Enough...