want to replace button pin with ultrasonic sensor when distance<10 ,please help

const int ledPin = 13;
const int buttonPin = 2;

int programState = 0;

int buttonState;
long buttonMillis = 0;
const long intervalButton = 3000;

long ledMillis = 0;
const long intervalLed = 5000;

void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, HIGH);
}

void loop()
{
unsigned long currentMillis = millis();
buttonState = digitalRead(buttonPin);

if (buttonState == LOW && programState == 0) {
buttonMillis = currentMillis;
programState = 1;
}
else if (programState == 1 && buttonState == HIGH) {
programState = 0; //reset
}
if(currentMillis - buttonMillis > intervalButton && programState == 1) {
programState = 2;
ledMillis = currentMillis;

digitalWrite(ledPin, HIGH);
}

if(currentMillis - ledMillis > intervalLed && programState == 2) {
programState = 0;

digitalWrite(ledPin, LOW);
}
}

Hi ukr100,

welcome to the forum.
As this is your very first post I want to write some basic information. You should follow some rules to get better help.

If you want to post code use the insert code-Button. This butto looks like this: <|>. You can find this button if you use the reply-function instead of the quick-reply function. If you click on reply you get a menu right above the textinput-field
at the left end of this menu you wil finde the <|>-Button.

Code in such a code-section is written in a constant-distance-font which will show all the indentions the same way as in the IDE. And with a simple click the users can put the whole code into the clipboard.

about your code:
whenever you want to store a value of millis() the variable has to be an unsigned long.
Same thing if you do calculations or comparisons like in the line

if(currentMillis - buttonMillis > intervalButton

intervallButton has to be an unsigned long too.

The information you have provided so far is not sufficient. What kind of ultra-sonic sensor do you have?

Please add a description in normal words that describe what your prgram should do.

best regards Stefan

i 'm making a bootle filling mechanism, when bottle placed the ultrasonic sensor detect and turn on relay for given time and then off. i am using HC-SR04 ultasonic sensor, a simple relay module , Arduino uno.
sir , can you write suitable code for me ?

(deleted)

The others users will be in standby until you have shown some additional own effort.
The mimimum you can do is googling for "arduino ****HC-SR04 ultasonic sensor demo-code".

Testing the democode and try to understand the commands written in the demo-code.
If you have a question to the very first line of the democode this is totally OK. It still shows that you have started learning programming are stuck now and have a question.

A lot of basic questions are answered through reading and copy & paste the code-examples of this tutorial

As you are a newbee I recommend reading this tutorial: Arduino Programming Course Contents

It is very easy to understand. It has the right mixture between writing about fundamental things and going ahead to get you going. You have the choice:

  • trying to write code with very little knowledge encountering all kinds of problems: compiler-errors, code behaves different than you expected etc which will make you spending time scratching yor head

  • or spending time reading an easy to understand tutorial to gain knowledge that will enable to write code that (most of the time) works

It is completely OK to ask questions here in the forum. Whenever you have posted a question. In the meantime waiting for an answer you could go on reading some lines in this tutorial instead of just waiting or scratching your head about your own code.

best regards Stefan

okay thank you Stefan , i will definitely follow your suggestion

ukr1000:
i 'm making a bootle filling mechanism, when bottle placed the ultrasonic sensor detect and turn on relay for given time and then off. i am using HC-SR04 ultasonic sensor, a simple relay module , Arduino uno.
sir , can you write suitable code for me ?

See Ultrasonic Sensor controls Relay tutorial

ukr1000:
i 'm making a bootle filling mechanism

Hmmm....

Quite an ambitious project as Bootle is quite a large town. However it could be worse as you could be trying to fill Blackburn, Lancashire which has four thousand holes in it.

Monday morning whimsy aside, you might want to take a look at the NewPing library which takes all the grunt work out of dealing with HC-SR04 sensors. Just bring up the Library Manager and search for "NewPing", select the NewPing library and click on the "Install" button. Once it's installed you can check the examples from the File -> Examples menu.

Ian