const int ledPin = 9; // the number of the LED pin
// Variables will change:
int ledState = LOW; // ledState used to set the LED
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0; // will store last time LED was updated
// constants won't change:
const long interval = 15; // interval at which to blink (milliseconds)
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
}
void loop() {
// here is where you'd put code that needs to be running all the time.
// check to see if it's time to blink the LED; that is, if the difference
// between the current time and last time you blinked the LED is bigger than
// the interval at which you want to blink the LED.
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}
As seen in the line where I defined " const long interval" I set my interval to be 15 miliseconds. Yes I know that is fast and it is desired. I am having a bit of trouble wrapping my head around how to make it so that I can blink this led only when I tell it to.
I have a pulse generator that will send a voltage high to one of the arduino pins every 15 miliseconds. The LED is then connected to pin 9 as I defined on the code. However, I wonder if Arduino is capable of detecting a pulse, processing it, and then blinking the LED that quickly. Right now I could just have it blink crazy using the above example, but it gives me no control over when to start and stop it, how many times I want it to blink etc.
I am aiming to capture the light effects using a highspeed camera and the reason that the LED has to flash so quickly is so that the exposure is short. I would really value your input on this. Thank you.
EDIT: If it is in fact impossible, which I suspect it might be (I hope Im wrong), then modifying the code so that it starts the blinking process with just a voltage high would be better than not being to control it at all.
That is good news. My initial assumption of it being not fast enough was being I tried to do it using digitalWrite(High), DigitalWrite(Low), etc which I am guessing adds a lot of lag to it. Thank you very much, Ill try to implement that.
Sorry I miswrote, I had an analogRead as a detection for the voltage which I read was significantly slower. I have implemented the things you mentioned but now the led does not appear to flash at all. I tried altering the values of voltage so that the if condition is trigPin>100 etc.
const int ledPin = 9; // the number of the LED pin
const int trigPin = 4;
// Variables will change:
int ledState = LOW; // ledState used to set the LED
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0; // will store last time LED was updated
// constants won't change:
const long interval = 15; // interval at which to blink (milliseconds)
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
pinMode(trigPin,INPUT);
}
void loop() {
// here is where you'd put code that needs to be running all the time.
// check to see if it's time to blink the LED; that is, if the difference
// between the current time and last time you blinked the LED is bigger than
// the interval at which you want to blink the LED.
unsigned long currentMillis = millis();
boolean blink = false;
if (digitalRead(trigPin)){
blink = true;
}else {
blink = false;
digitalWrite(ledPin, LOW);
}
if ((blink == true) && (currentMillis - previousMillis >= interval)) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}
I checked the syntax a few times and changed some of the values for interval to see if it would make a difference but I dont see how it should since as long as the it detects the voltage from pin 4 (reading the boolean true), and the timing is greater than the interval, then it should blink.
Sorry I should have specified that part. I meant that that nanosecond pulse is meant to just tell the arduino to go ahead and start the process of turning on. But it only sends this pulse to it every 15 miliseconds.
I am working with a synchronizer used for high speed imaging of flows by illumination using a light.
edit: the actual time it is spent being on for is trivial as long as it is less than 7.5 miliseconds or so. Otherwise the image looks blurry due to long exposure.
I see. I tried just using the blink at 15 milisecond intervals and honestly it doesn't look awful but it might get worse at higher speed flows. The fact that it isnt synchronized with the high speed cameras means itll lead to lots of inconsistencies though.
Perhaps i can look into ways to elongate the pulse or something which is faster than the arduino which can detect that pulse then send arduino a 1 microsecond pulse.
so the 7.5 miliseconds is the time the light can be on for to produce a clear image. the 10 nanosecond pulse is just the signal to kickstart it to turn on.
Detect pulse sent by pulse gen - 10 nanosec width pulse
pulse gen waits for 15 ms, at the same time light turns on for 7.5 ms
3)light switches off
then it just repeats, the 15 ms interval is independent of when the light turns off, it just turns on for 7.5 ms regardless of how long the detection pulse is
int ledPin = A2;
int triggerPin = 5;
long dtime = 0;
long time;
//long etime;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(triggerPin,INPUT);
pinMode(9,OUTPUT);
}
void loop() {
//Serial.println(analogRead(triggerPin));
//Serial.println(time);
//digitalWrite(triggerPin,HIGH);
//if (digitalRead(triggerPin)== HIGH){
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if(analogRead(triggerPin >800){
//Serial.println("Trigger Detected.");
digitalWrite(ledPin,HIGH);
dtime = micros()- dtime;
Serial.println(dtime);
delay(15);
digitalWrite(ledPin,LOW);
//etime = micros();
//Serial.println( etime);
//digitalWrite(triggerPin,LOW);
//dtime = micros();
//Serial.println(dtime);
}
//else{
// digitalWrite(laserPin,LOW);
//}
}
Funnily enough, this actually works and can detect that pulse coming from the synchronizer, it just doesnt do it properly. The timing is messed up and it doesnt actually blink every 15 miliseconds. In fact the flashing is way slower and the rate at which it happens is altogether inconsistent.
It is a synchronier made by a company called quantum composers. And looking at the software that controls it, and the timing setup it is about 1/100 of a microsecond. The actual value might vary but it is within that magnitude.
us. Sorry the button comments are commented out cuz theyre no longer in use. Shouldve deleted. I had a thing earlier where I tried to press a button to kickstart the entire pulsing process, but i realized having the pulse generator control it is negligible in clocking differences.
Fair enough. Thank you for all your help thus far. Any idea why that analogRead code was able to catch that pulse though? I found that just a bit strange. Like it registered the voltage the pulse generator gave it for sure because using milis() i found that it detected it exactly at the 15 ms mark from the moment the board activated. Just weird
Arduino compatibles with AMD chips running 200 to 600 MHz might catch it, not sure about analog read though. The PJRC Teensy's are one source, there are other Frankenduinos out there.
AVR ADC reading the same pin every time takes about 109 micros to make a read running at 16 MHz.
Sorry I did mean micros(). Also Thanks for helping me identify those possibilities. I just figured Id try to find out how its able to do that before diving into another rabbit hole. Plus I have like 0 funding and budget lol so im just trying to make the most with what the school has.
This sketch has an interruptable blink function and a function that makes it blink in a pattern and a function that shows how many times void loop() ran every second. They all run together without code structure "rails" (read: more code) coordinating them. The loop count tells you average latency only, it's a dev tool to measure the effect of changes on the speed. It could run faster using port register manipulation than using Arduino-Safetied digitalRead/Write.
// add-a-sketch_on-off_blinker 2018 by GoForSmoke @ Arduino.cc Forum
// Free for use, Apr 30/2018 by GFS. Compiled on Arduino IDE 1.6.9.
// This sketch shows a led blink task turned on-off by another task.
// This led blinker has a built-in on-off switch, if wait > 0 it is ON.
// BlinkSwitcher is a demo that uses OnOffBlinker.
#define microsInOneSecond 1000000UL
// blinker vars
byte ledState, ledPin = 13;
word startBlink, waitBlink; // 16 bit millis is good to 65.535 seconds
// blink switching vars
byte blinkCounter, lastLedState; // these are to change the blinking
word startOffTime;
const word waitOffTime = 3000; // millis
void setup()
{
Serial.begin( 115200 );
Serial.println( F( "\n\n\n Blink Led, free by GoForSmoke\n" ));
Serial.println( F( "This sketch shows a led blink task turned on-off by another task." ));
pinMode( ledPin, OUTPUT );
waitBlink = 250;
}
void LoopCounter() // tells the average response speed of void loop()
{ // inside a function, static variables keep their value from run to run
static unsigned long count, countStartMicros; // only this function sees these
count++; // adds 1 to count after any use in an expression, here it just adds 1.
if ( micros() - countStartMicros >= microsInOneSecond ) // 1 second
{
countStartMicros += microsInOneSecond; // for a regular second
Serial.println( count ); // 32-bit binary into decimal text = many micros
count = 0; // don't forget to reset the counter
}
}
void OnOffBlinker() // only blinks if there's a wait time, can be switched on/off
{
if ( waitBlink > 0 ) // this is the on/off switch
{
// word( millis()) gets the low 16 bits of the 32-bit millis() return.
if ( word( millis()) - startBlink >= waitBlink ) // difference in time by subtracting start from end
{
ledState = !ledState; // ! is NOT: not_0/true becomes 0/false else 0 becomes 1.
digitalWrite( ledPin, ledState ); // the led changes state.
startBlink += waitBlink; // next blink starts when it should, where diff > wait.
}
}
else if ( ledState > 0 ) // waitBlink == 0 turns blinking off
{
digitalWrite( ledPin, ledState = LOW ); // make sure the led is OFF
} // yes, you can set a variable during calculation in C, the write here is LOW.
}
void BlinkSwitcher()
{
// this task pauses the blinker after 5 blinks then turns blinking back on, repeat.
if ( waitBlink > 0 ) // while the led is blinking
{
if ( ledState != lastLedState ) // blink has just transitioned
{
if ( ledState == 0 ) // count a blink only if turned off, blink is finished
{
blinkCounter++;
if ( blinkCounter == 5 ) // 5 blinks then none for 3 seconds
{
blinkCounter = 0; // the if has the ++ first, count stats at 1.
waitBlink = 0; // turn the blinking off, start off timing
startOffTime = millis(); // set start to off time
}
}
lastLedState = ledState;
}
}
else // this only runs when blinking is off
{
// word( millis()) gets the low 16 bits of the 32-bit millis() return.
if ( word( millis()) - startOffTime >= waitOffTime )
{
startBlink = millis();
waitBlink = 250;
}
}
}
void loop()
{
LoopCounter();
// this is the interruptable blink task
OnOffBlinker(); // sets 'now' only when blinking
// this task pauses the blinker after 5 blinks then turns blinking back on, repeat.
BlinkSwitcher();
}
I've checked one of my old (1988 or so) databooks. A 74F74 D-flipflop can be clocked at 100 MHz; that should allow you to detect your 10ns pulse and output a signal for the Arduino (for unlimited duration). The Arduino can, as said, reset the D-flipflop.