Digital Write for x seconds after pushbutton

Thread Starter

fieryfire

Joined Feb 14, 2017
150
Hi All,

How do i write an arduino code, so that i maybe able to digitalwrite for 'x' number of seconds from a single press of a button
 

BobaMosfet

Joined Jul 1, 2009
2,211
Normally, you purchase books, or better yet, go online to Google, and search for beginner arduino code writing and learn how to program. Learning electronics on top of programming is a whole 'nother field. Good luck!
 

Thread Starter

fieryfire

Joined Feb 14, 2017
150
elapsedMillis Push;

void setup() {
Serial.begin(38400);
pinMode(7, INPUT);
pinMode(9, OUTPUT);
}

int val;

void loop()
{

if (digitalRead(7) == HIGH) {

if (Push > 2500) {
Push = 0;
digitalWrite(9,HIGH);

val = analogRead(0);
Serial.println(val);

}
digitalWrite(9,LOW);
}

}


This is what i have so far. although i fear the logic i need doesnt work :(
 

Thread Starter

fieryfire

Joined Feb 14, 2017
150
Draw a pic of the signal you would like to generate.

Regards, Dana.
Basically just a square wave. for example OFF, then ON for couple of ms, and then OFF again. Inorder to covnert it into a sinus pulse i would just use an RC filter at the output
 

shteii01

Joined Feb 19, 2010
4,644
Its impossible to get an analogRead for the signal while the delay takes place
That is correct.
The delay() stops code execution, the code execution resumes when delay() is finished.
However, you never specified that you want to continue to execute code while you are toggling the pin On and Off.

Since you are doing some kind of square wave, you can use PWM and do custom frequency: https://www.arduino.cc/en/Tutorial/SecretsOfArduinoPWM

Another option is to have a separate chip to do the square wave that you want. That way you offload the square wave generation from your master chip, that will simplify the code for both the master and the square wave generating chip, and the square wave generating chip can be some simple ATmega48/88/168 chip.
 

danadak

Joined Mar 10, 2018
4,057
Basically just a square wave. for example OFF, then ON for couple of ms, and then OFF again. Inorder to covnert it into a sinus pulse i would just use an RC filter at the output
Are you trying to generate a sine burst ? Not sure I understand.



Regards, Dana.
 

Picbuster

Joined Dec 2, 2013
1,058
Hi All,

How do i write an arduino code, so that i maybe able to digitalwrite for 'x' number of seconds from a single press of a button
Start a second counter on button press and set a signal high.(eq flag > Start_Count= 1; the counter TimeCount++; EndCount=xx;)
Button_Flag is the flag to signal that button is pressed and de-bounced.
Hitting more than once is not effecting a set Button_Flag.
Catching the button should be done in an interrupt routine.
xx number of seconds to wait before a stop digital write.
The second timer in interrupt routine.

if ( Button_Flag)
{
if (Start_count) { // start digital write}
if (EndCount=>xx)
{
Start_Cont=0;
TimeCount=0;
// stop digital write
Button_Flag=0; // give button_flag free to act again
}
}

Picbuster
 
Attached are two "simple" ways of getting a square wave of a specific frequency from a port pin on an Arduino. You would trigger one or the other with your button press, if I understand at all what you are trying to do (which, by the way, could benefit from some clarity).

If you listen to the sound, you will hear a slight "waver" in the first method due, I believe, to the time taken to read and evaluate millis(); If you use the second method, look into using noTone().

Hope this helps.

C:
int OutPortPin = 2;
unsigned long startTime;  
unsigned long currentTime;
unsigned long interval = 2000; // milliseconds

void setup() {
  pinMode(OutPortPin, OUTPUT);  
  noTone(OutPortPin);
}

void loop() {
  // 2 sec burst of a 1000 Hz square wave
  interval=2000;
  startTime = millis();  //get the current millisecond count
  while ( (currentTime=millis())-startTime<=interval)
  {  
  // 1000 Hz
  digitalWrite(OutPortPin, HIGH);
  delayMicroseconds(500);
  digitalWrite(OutPortPin, LOW);
  delayMicroseconds(500);
  }
  delay (2000);  // delay to seperate the two methods
  // 2 sec burst of a 1000 Hz square wave
  // 1000 Hz
  tone(OutPortPin,1000,interval);
 here: goto here; // just do this once
}
 
Top