Do you understand the code and does it work I wonder ?
CosmicRavioli:
i've already gotten the code from someone who eish to remain anonymous, so i guess case's closed, thanks
Please, post your codes so that we can see how it goes with your Flow Chart and adjust the codes if necessary.
How do you know if the 'person/dog' has left the room ?
No further testing of the sensor state...
The other problem I see in the flowchart is that if the NO condiiton is reached, the LED is left on 'forever'... I'm sure you will want it to turn off tomorrow or next week.
Here's what you effectively need...!

The 'something else' is more thamn you've explained

CosmicRavioli:
Im doing a project and the teacher basicly just give a pass on copying other people's code
Either this is a not true, or your teacher does not really care whether or not you learn anything.
If your teacher does not care whether or not you learn anything, the question becomes whether or not you care. Perhaps you simply want some sort of paper qualification. But you'll find that a paper qualification, without the means to answer the simplest questions about programming, will not get you far.
to GolamMostafa :Here's the code Golam (big props to the anonymous guy who help me for putting instruction in there so i can actually understand and learn something)
/ <Solved> Can Someone Please Create a code based on my flowchart please - Programming Questions - Arduino Forum
// 27 jan 2020
// LED_BUILTIN comes on when body detected within threshold
// stays on while detected
// goes out a few seconds after detection stops
// set the values in the lines marked <<<< to suit real life
// run with monitor open at 9600 to see distance
int trigPin1 = 8; //set to real pin <<<<<<
int echoPin1 = 7; //set to real pin <<<<<<
bool inRange = false;
bool weAreTiming = false;
byte threshold = 10; //cm, set to real value <<<<<<
unsigned long wentOutOfRangeAt;
long ledStaysOnFor = 5000; //ms, set to real value <<<<<<
void setup()
{
Serial.begin(9600);
Serial.println("ultrasonic in and out of range, forum 661187");
Serial.println(FILE);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
Serial.println("setup() done");
Serial.println(" ");
delay(2500); //to read the setup() screen
} //setup
void loop()
{
//get the distance
long duration1, distance1;
//static long distance1Previous;
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
duration1 = pulseIn(echoPin1, HIGH);
distance1 = (duration1 * .0343) / 2;
Serial.println(distance1);
//led on if close
if (distance1 <= threshold && distance1 > 0) //in range now ...
{
digitalWrite(LED_BUILTIN, HIGH);
inRange = true;
}
//see if body has just gone out of range from when it was in range
if (distance1 > threshold * 1.1 || distance1 == 0) //out of range ... || means "or"
// the 1.1 is to give a bit of leeway if body sitting near the threshold distance
{
if (inRange == true) //was in range last time we checked
{
Serial.println("** Newly out of range ****");
inRange = false;
weAreTiming = true;
wentOutOfRangeAt = millis();
}
}
//if body out of range, and "ledStaysOnFor" milliseconds elapsed, turn led off
if (weAreTiming && millis() - wentOutOfRangeAt >= ledStaysOnFor)
{
weAreTiming = false;
digitalWrite(LED_BUILTIN, LOW);
}
delay(30000); //ultrasonic sensor can't read too often
} //loop
}
to lastchancename : unfortunately i've already sent the flowchart and i'll have to to stick to it until the teacher says we can fix it,but i'll keep your advice in mind or bookmark if the oppertunity to fix it is opened.
to PaulMurrayCbr : here's the thing that i forgot to mention : he said he has little knowledge of code himself and i don't know if he cares or don't cares on one hand sending student off on their own to well learn to code themselves and give a tiny deadline seems like he doesn't care but on the other hand when he's teaching about sensors and stuff he does seems to cares but is limited by a 40 min only class but i don't really know,i don't speak to him that much now on the matter about if i care then yes i do like coding and stuff but not enough that i will be spending 5-6 hour trying to fix a broken code or write something that i do not understand yet,also yes i am a little lazy to be honest .
PaulMurrayCbr:
Either this is a not true, or your teacher does not really care whether or not you learn anything.
Most likely the OP is true in his post as he has attached Flow Chart of a process.
This is your sketch with code tags(</>). Let us see how it does correspond with your Flow Chart of Fig-1.
int trigPin1 = 8; //set to real pin <<<<<<
int echoPin1 = 7; //set to real pin <<<<<<
bool inRange = false;
bool weAreTiming = false;
byte threshold = 10; //cm, set to real value <<<<<<
unsigned long wentOutOfRangeAt;
long ledStaysOnFor = 5000; //ms, set to real value <<<<<<
void setup()
{
Serial.begin(9600);
Serial.println("ultrasonic in and out of range, forum 661187");
Serial.println(__FILE__);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
Serial.println("setup() done");
Serial.println(" ");
delay(2500); //to read the setup() screen
} //setup
void loop()
{
//get the distance
long duration1, distance1;
//static long distance1Previous;
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
duration1 = pulseIn(echoPin1, HIGH);
distance1 = (duration1 * .0343) / 2;
Serial.println(distance1);
//led on if close
if (distance1 <= threshold && distance1 > 0) //in range now ...
{
digitalWrite(LED_BUILTIN, HIGH);
inRange = true;
}
//see if body has just gone out of range from when it was in range
if (distance1 > threshold * 1.1 || distance1 == 0) //out of range ... || means "or"
// the *1.1 is to give a bit of leeway if body sitting near the threshold distance
{
if (inRange == true) //was in range last time we checked
{
Serial.println("*** Newly out of range ****");
inRange = false;
weAreTiming = true;
wentOutOfRangeAt = millis();
}
}
//if body out of range, and "ledStaysOnFor" milliseconds elapsed, turn led off
if (weAreTiming && millis() - wentOutOfRangeAt >= ledStaysOnFor)
{
weAreTiming = false;
digitalWrite(LED_BUILTIN, LOW);
}
delay(30000); //ultrasonic sensor can't read *too* often
} //loop
Figure-1:
I like how the anonymous benefactor has mixed the relatively advanced (the use of FILE) with the basically awful (variable pin numbers, the ultrasonic ranger handling), producing a result that really does mark it out as "not-all-your-own-work". Not that it sounds like your teacher will notice.
That, and the fact that code as posted (badly) doesn't compile.
TheMemberFormerlyKnownAsAWOL:
If the answer to both of those is "NO" it raises some interesting possibilities
That the OP is actually . . . the teacher?
Dum-dum-daaaah
What I had in mind is that all one would have to do is Google for "arduino program" and submit the first thing that was found ![]()
...R
