Go Down

Topic: Question about sensors (Read 331 times) previous topic - next topic

casampot

Oct 10, 2017, 02:38 pm Last Edit: Oct 10, 2017, 02:44 pm by casampot
Hi .. im a newbie here...

Could someone help me please..

How to code a detection for certain time

Like if(value>400) the condition's program will be executed if it detected it for 5secs ... if not 5secs then it will not execute the condition's program

It must satisfy the condition for 5 seconds before it execute the condition's program

Please help me ...


wvmarle

Using millis().

Record time when value is over/under threshold, set a flag that you're counting, and after the interval has passed start your action. Use flags to keep track of the status.

Code: [Select]

uint32_t lastGood = 0;
bool counting = false;
bool actionTaken = false;
loop() {
value = readValue();
if (value>400 && counting == false) { // Start counting when the threshold is breached.
  counting = true;
  lastGood = millis();
}
if (millis() - lastGood > interval && counting && actionTaken == false) {
  // After the interval has passed and we're counting and we haven't taken action yet, take action.
  takeAction();
  actionTaken = true; // only once for a too low value
}
if (value < 400) { // Value is good; reset the flags.
  actionTaken = false;
  counting = false;
}
}
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

casampot

Where should i put the "digitalWrite(kPinLed, HIGH);" if it satisfy the conditions

wvmarle

There's the takeAction() function that does stuff when the conditions are met.

To stop taking the action (e.g. switch off the LED, assuming HIGH is ON), modify if a bit (even without calling the stopAction() this is an optimisation as it's not run all the time):

Code: [Select]

if (value < 400 && actionTaken) { // Value is good; reset the flags.
  stopAction();
  actionTaken = false;
  counting = false;
}


So whatever you want to do upon reaching the conditions and when the conditions end, goes in the takeAction() and stopAction() functions.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

casampot

Im very sorry.. i can't understand.. but here's my code.. this code execute the condition's program if it satisfy the value>400 only.. no time.. please help me.. sorry

wvmarle

Much better to post your code with code tags, then it looks like this:
Code: [Select]
const int kPin_Photocell = A0;
const int kPinLed = 2;
const int kPinMotor1 = 3;
const int kPinMotor2 = 4;
int integer;

void setup() {

pinMode(kPinLed, OUTPUT);
pinMode(kPinMotor1, OUTPUT);
pinMode(kPinMotor2, OUTPUT);
Serial.begin(9600);
}

void loop(){
 int value=analogRead(kPin_Photocell);
Serial.print("Analog Reading = ");
Serial.print(value);
  if(value<500&&integer==0){
  Serial.println(" -   Dark");
  digitalWrite(kPinLed, HIGH);
  digitalWrite(kPinMotor1, HIGH);
  digitalWrite(kPinMotor2, LOW);
  delay(15000);
  integer=1;
}

  else if(value>500){
  Serial.println(" -  Bright");
  digitalWrite(kPinMotor1, LOW);
  digitalWrite(kPinMotor2, LOW);
  digitalWrite(kPinLed, LOW);
  }
}


You set the threshold to 500, not 400.
If it equals 500, nothing evaluates as true. Use a <= for dark or a >= for light (as you prefer) to catch the 500 reading as well.
You don't reset this oddly named "integer" flag - why is that even an int, not a bool? Your other constants can be a byte instead of an int. Saves memory. Not important for this sketch but for larger ones it is, so make it a habit to choose the smallest possible data type.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

casampot

Code: [Select]
if(value<500&&integer==0){
  Serial.println(" -   Dark");
  digitalWrite(kPinLed, HIGH);
  digitalWrite(kPinMotor1, HIGH);
  digitalWrite(kPinMotor2, LOW);
  delay(15000);
  integer=1;


I use "integer" so that if this condition has been executed .. it will just execute it once...

Could you fix my code please.. where should i put your codes ... because this code will executed the condition's program immediately .. no time for condition before it execute it ... i want the condition to execute the program when it satisfy the condition fof 5seconds..

Please im very sorry.. this is my first time to use arduino ... sorry please help me

wvmarle

I know the function of this "integer" variable, just don't understand why you name it so. Descriptive names go a long way in understanding your own code - especially when you come back to it next month or next year.

Now of course I could fix your code but you're not learning anything.

The snippet I gave in #1 should be clear enough - read it, analyse it, understand what it's doing and how, and you'll be able to easily add it to your bits of code. Also by understanding what you're doing, you can do it yourself the next time.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

casampot

#8
Oct 15, 2017, 02:10 am Last Edit: Oct 15, 2017, 02:32 am by casampot
Im very sorry.. it just really my first time to use arduino ... Please im begging you.. its for my project ... .tomorrow is the submission... please fix it ...i'll understand it ... i can understand program .. but im not familar on arduino codes.. please ..
Today im creating it.. please help me ...
And also im just very stressed and frustrated because i never had an INC :/
Im sorry.. hope you'll understand

casampot

Also ... how to set the recordtime/countingtime/delaytime in 5seconds?

Please include it in your code from your reply #1 ... please 5seconds

wvmarle

The delay is there already, all you have to do is set the variables to whatever value you need.

You say you can understand programming but not Arduino programming. That doesn't make much sense, and this question makes me think you actually don't understand programming.

For me, I learnt programming in MSX-BASIC. Dabbled in assembly for the MSX with it's Z80 processor, never got that really. Years later in college we had to use TurboPascal and touched FORTRAN and SQL. Then I had some projects so took up Python, still my favourite. Java followed to create an Android app. Now on Arduino it's C. Yet many things I learnt in BASIC still apply, it's still programming. If you really understand programming, the language doesn't matter much.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

casampot

What part is the delay? I'll just put 5 inside the open and close parenthesis?

Could you please code tag that part please

I understand Pickit 2 ... because my professor c reatea library to make it easy for us.. that's all i know.. basics.. 7 segment,lcd, motion sensor, temperature sensor...
But arduino is very new to me.. and im not familliar with the terms..

Thanks for your time and replies ...
Appreciated

wvmarle

What part is the delay? I'll just put 5 inside the open and close parenthesis?
There's this function called "delay". What do you think that would do?

Quote
I understand Pickit 2 ... because my professor c reatea library to make it easy for us.. that's all i know.. basics.. 7 segment,lcd, motion sensor, temperature sensor...
But arduino is very new to me.. and im not familliar with the terms..
If you run into a term you don't understand, put it in Google's search field and very soon you'll know all there is to know about it.

One thing you're supposed to learn in university (I assume you're in one as you talk about a professor) is how to study. How to find and process new information. That's exactly what you have to do now - if you already understand programming as you said a few replies ago, it shouldn't be that hard for you.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

jremington

#13
Oct 17, 2017, 03:25 am Last Edit: Oct 17, 2017, 03:26 am by jremington
Quote
its for my project ... .tomorrow is the submission... please fix it
You should start working on classroom projects earlier.

casampot

Code: [Select]
   
uint32_t lastGood = 0;
bool counting = false;
bool actionTaken = false;
loop() {
value = readValue();
if (value>400 && counting == false) { // Start counting when the threshold is breached.
  counting = true;
  lastGood = millis();
}
if (millis() - lastGood > interval && counting && actionTaken == false) {
  // After the interval has passed and we're counting and we haven't taken action yet, take action.
  takeAction();
  actionTaken = true; // only once for a too low value
}
if (value < 400) { // Value is good; reset the flags.
  actionTaken = false;
  counting = false;
}


Please tell me ... where's the delay? Is the millis()? Should i put 5000 inside the open and close parenthesis?

And the interval .. it is not declared ... interval of what?

TakeAction means on Led or motor right?


Go Up