HELP--Running a code one time when a sensor is activated

Hi,
I have the following code that runs in loop and i only want it to run once when the sensor is activated. Ive tried putting it in the setup and leaving the loop section blank, however Im told that the loop section is required in an arduino program. Can someone tell me how to rewrite this code to only run one time when the sensor is activated?

int sol = 13;

int sen = 3;

void setup() {

pinMode(sol, OUTPUT);

pinMode(sen, INPUT);

}

void loop() {

int val = digitalRead(sen);

if(val == HIGH){

digitalWrite(sol, LOW);

delay(250);

}else{

digitalWrite(sol, HIGH);

delay(1000);

}

}

Do just that, your loop function would be empty.

void loop( )
{
}

I did this and got an error code
(sketch file) [Blink.ino:9:1:](javascript:void(0)) error: expected unqualified-id if(val == HIGH){ ^ 1 error generated.

int sol = 13;
int sen = 3;
void setup() {
	pinMode(sol, OUTPUT);
pinMode(sen, INPUT);
}
	int val = digitalRead(sen);
	
if(val == HIGH){
	digitalWrite(sol, LOW);
	delay(250);
	digitalWrite(sol, HIGH);
	delay(1000);
}
void loop() 
{
}

If you want the section of code to only run when an input goes true, you set a flag that prevents the block of code from rerunning until the flag is cleared by the input signal going false.

in your example above your code is OUTSIDE the setup() block

Im a beginner and have no idea what you mean. haha Im trying to learn by reading the arduino book...but it doesnt address this specific issue.
Could you write it out for me?


int sol = 13;
int sen = 3;
void setup() 
{
 pinMode(sol, OUTPUT);
 pinMode(sen, INPUT);
 int val = digitalRead(sen);
	
 if(val == HIGH)
 {
 digitalWrite(sol, LOW);
 delay(250);
 digitalWrite(sol, HIGH);
 delay(1000);
 } 

}//END of setup()

void loop() 
{
}

thanks Larry. The issue i have is that each time this runs, the relay is switched three quick times before the program runs. it activates a solenoid for a pneumatic piston. So it rapid fires for 3 times before running the program.

I need to find a way to make the program run one time, when a limit switch is closed so that the pneumatic piston extends for a second and then retracts.


int sol = 13;
int sen = 3;
void setup() 
{

digitalWrite(sol, HIGH); //solenoid de-energized
pinMode(sol, OUTPUT);

pinMode(sen, INPUT);
int val = digitalRead(sen);
	
 if(val == HIGH)
 {
 digitalWrite(sol, LOW);  //solenoid energized
 delay(250);
 digitalWrite(sol, HIGH); //solenoid de-energized
 delay(1000);
 } 

}//END of setup()

void loop() 
{
}

What solenoid are you using ?

SNS solenoid

Schematic, please!

Here is what i have
arduino unoR3
a limit switch
a relay
solenoid
a pneumatic cylinder
Here is what i want to happen:
When something hits the limit switch, i want it to turn the relay on for 1 second and then off. so that the pneumatic cylinder goes down and then up again. Its needs to stay up, until the limit switch is tripped again.

Correction--Here is what i have
arduino unoR3
a limit switch
laser and sensor
a relay
solenoid
a pneumatic cylinder
Here is what i want to happen:
When something hits the limit switch, it turns off the laser which causes the relay to turn on for 1 second and then off. so that the pneumatic cylinder goes down and then up again. Its needs to stay up, until the limit switch is tripped again.

Hello jkacorn

Post the schematic of your project.

Welcome to the forum. As the resident curmegeon I will get this out of the way...

  • Read the sticky post at the top of the forum that tells how to get the most from the forum.
  • When you post code, press CTRL-T in the IDE then copy and post the code using code tags.
  • If you post a schematic, not a useless Fritzing picture, the pool of people who will try to help you grows considerably.

I cant´t find the mentioned laser device.

i removed it as i found it isnt required when i use the IOREF port to bridge to the digital pin 3(input)

Thanks Steve. Im trying my best...haha
Im a welder/fabricator and know nothing about electronics. Im trying to learn this so i can complete the project Im building.

There are multiple ways to approach this.

One way is to put all your code in the setup() function, using an if statement to test when the switch has been activated.
Then as mentioned above add:

void loop()
{
}

What will happen is your solenoid would operate one time, the program will proceed to the loop() which has no instructions. The processor will continually go through the loop but not do anything except test the loop function.

The only way to have it run again is to cycle power to the board or push the reset button.

There are ways to control how often an action can be activated. So it is possible to write code so your solenoid would actuate no more than once a minute, or once an hour or once every