Thank you! I'll try to give it a shot with a version of this.
With this code, I'm receiving an input pulse from another system, which triggers the arduino to send a pulse to a laser, which triggers the laser to burst at 10hz. This happens once every 10 mins, for exactly 1 seconds.
I can't have a delay() function as I'm getting other inputs from that same system which triggers the laser, and I need to know exactly what time those inputs are coming after the laser has been triggered.
Let me know if that clarifies things!
Thanks!
In reading the other responses... I think having the sender count the ten minutes and initiate the receiver to do the one-second pulse train will eliminate the need for synchronizing. Occam's razor.
(I did have fun playing with the simulation and practice timing)
My thoughts exactly! But the sender has other issues that can't be reconciled when I try this approach. I think those issues are beyond the scope of this thread.
So, is the other program doing the 10 minute timing? Is it on the same Arduino as the one sending the pulse?
No, so, the only two things the other program is doing is starting the session (with switchState_cue) and occasionally sending another pulse with (cueState_Retrieval). The arduino will have to trigger the laser once every 10 minutes, for 1 second, at 10hz.
Let me know if you have other questions!
What if the Arduino were interrupted (power, for example). Would LASER firing still be desired at 10 minute intervals? If so, would a real time clock (with battery and date/time memory) be better to keep track of the time, and just let the Arduino to the one-second of pulses?
Hello all, thanks, I think I have somewhat of a working code now. I do need help in one other thing in printing the data using serial.println(). When I ask arduino to send information regarding an input from the other program, I'm getting too many lines since the serial communication is sending all the times when the signal is high (for a full seven seconds), as such:
16:56:11.176 -> Retrieved at time...17661
16:56:11.210 -> Retrieved at time...17661
16:56:11.245 -> Retrieved at time...17661
16:56:11.278 -> Retrieved at time...17678
16:56:11.313 -> Retrieved at time...17705
16:56:11.313 -> Retrieved at time...17734
16:56:11.347 -> Retrieved at time...17762
16:56:11.381 -> Retrieved at time...17790
16:56:11.414 -> Retrieved at time...17818
16:56:11.447 -> Retrieved at time...17847
16:56:11.481 -> Retrieved at time...17874
16:56:11.481 -> Retrieved at time...17902
16:56:11.516 -> Retrieved at time...17931
16:56:11.550 -> Retrieved at time...17958
16:56:11.584 -> Retrieved at time...17987
16:56:11.618 -> Retrieved at time...18015
16:56:11.652 -> Retrieved at time...18042
16:56:11.652 -> Retrieved at time...18071
16:56:11.686 -> Retrieved at time...18099
16:56:11.719 -> Retrieved at time...18127
16:56:11.753 -> Retrieved at time...18155
16:56:11.788 -> Retrieved at time...18184
16:56:11.822 -> Retrieved at time...18211
16:56:11.822 -> Retrieved at time...18239
16:56:11.856 -> Retrieved at time...18267
16:56:11.890 -> Retrieved at time...18295
16:56:11.925 -> Retrieved at time...18324
16:56:11.959 -> Retrieved at time...18351
16:56:11.959 -> Retrieved at time...18380
16:56:11.993 -> Retrieved at time...18408
16:56:12.027 -> Retrieved at time...18435
16:56:12.061 -> Retrieved at time...18464
16:56:12.096 -> Retrieved at time...18492
16:56:12.129 -> Retrieved at time...18520
16:56:12.129 -> Retrieved at time...18548
16:56:12.163 -> Retrieved at time...18576
16:56:12.196 -> Retrieved at time...18604
16:56:12.230 -> Retrieved at time...18632
16:56:12.264 -> Retrieved at time...18661
16:56:12.298 -> Retrieved at time...18688
16:56:12.298 -> Retrieved at time...18717
16:56:12.332 -> Retrieved at time...18745
16:56:12.366 -> Retrieved at time...18772
16:56:12.400 -> Retrieved at time...18801
16:56:12.435 -> Retrieved at time...18829
16:56:12.435 -> Retrieved at time...18857
16:56:12.469 -> Retrieved at time...18885
16:56:12.503 -> Retrieved at time...18914
16:56:12.537 -> Retrieved at time...18941
16:56:12.571 -> Retrieved at time...18969
16:56:12.605 -> Retrieved at time...18998
16:56:12.639 -> Retrieved at time...19025
16:56:12.639 -> Retrieved at time...19054
16:56:12.674 -> Retrieved at time...19082
16:56:12.709 -> Retrieved at time...19110
16:56:12.743 -> Retrieved at time...19138
16:56:12.777 -> Retrieved at time...19166
16:56:12.777 -> Retrieved at time...19194
16:56:12.811 -> Retrieved at time...19222
16:56:12.845 -> Retrieved at time...19251
16:56:12.879 -> Retrieved at time...19278
16:56:12.912 -> Retrieved at time...19306
16:56:12.946 -> Retrieved at time...19334
16:56:12.946 -> Retrieved at time...19362
16:56:12.980 -> Retrieved at time...19391
16:56:13.014 -> Retrieved at time...19418
16:56:13.047 -> Retrieved at time...19447
16:56:13.081 -> Retrieved at time...19475
16:56:13.081 -> Retrieved at time...19502
16:56:13.116 -> Retrieved at time...19531
16:56:13.150 -> Retrieved at time...19559
16:56:13.184 -> Retrieved at time...19587
16:56:13.218 -> Retrieved at time...19615
16:56:13.252 -> Retrieved at time...19644
16:56:13.252 -> Retrieved at time...19671
16:56:13.286 -> Retrieved at time...19699
16:56:13.321 -> Retrieved at time...19728
16:56:13.356 -> Retrieved at time...19755
16:56:13.391 -> Retrieved at time...19784
16:56:13.391 -> Retrieved at time...19812
16:56:13.424 -> Retrieved at time...19839
16:56:13.457 -> Retrieved at time...19868
16:56:13.491 -> Retrieved at time...19896
16:56:13.525 -> Retrieved at time...19924
16:56:13.560 -> Retrieved at time...19952
16:56:13.560 -> Retrieved at time...19981
16:56:13.594 -> Retrieved at time...20008
16:56:13.629 -> Retrieved at time...20036
16:56:13.664 -> Retrieved at time...20065
16:56:13.699 -> Retrieved at time...20092
16:56:13.734 -> Retrieved at time...20121
16:56:13.734 -> Retrieved at time...20149
16:56:13.768 -> Retrieved at time...20177
16:56:13.801 -> Retrieved at time...20205
16:56:13.836 -> Retrieved at time...20233
16:56:13.869 -> Retrieved at time...20261
16:56:13.869 -> Retrieved at time...20289
16:56:13.903 -> Retrieved at time...20318
16:56:13.936 -> Retrieved at time...20345
16:56:13.971 -> Retrieved at time...20373
16:56:14.005 -> Retrieved at time...20401
16:56:14.038 -> Retrieved at time...20429
16:56:14.038 -> Retrieved at time...20458
16:56:14.073 -> Retrieved at time...20485
16:56:14.108 -> Retrieved at time...20514
16:56:14.141 -> Retrieved at time...20542
16:56:14.176 -> Retrieved at time...20569
16:56:14.176 -> Retrieved at time...20598
16:56:14.222 -> Retrieved at time...20626
16:56:14.243 -> Retrieved at time...20654
16:56:14.277 -> Retrieved at time...20682
16:56:14.311 -> Retrieved at time...20711
16:56:14.345 -> Retrieved at time...20738
16:56:14.345 -> Retrieved at time...20766
16:56:14.378 -> Retrieved at time...20795
16:56:14.412 -> Retrieved at time...20822
16:56:14.446 -> Retrieved at time...20851
16:56:14.480 -> Retrieved at time...20879
16:56:14.515 -> Retrieved at time...20906
16:56:14.515 -> Retrieved at time...20935
16:56:14.549 -> Retrieved at time...20963
16:56:14.583 -> Retrieved at time...20991
16:56:14.618 -> Retrieved at time...21019
16:56:14.652 -> Retrieved at time...21048
16:56:15.607 -> Retrieved at time...22095
16:56:15.641 -> Retrieved at time...22095
16:56:15.674 -> Retrieved at time...22095
16:56:15.708 -> Retrieved at time...22112
16:56:15.742 -> Retrieved at time...22139
16:56:15.776 -> Retrieved at time...22168
16:56:15.776 -> Retrieved at time...22196
16:56:15.810 -> Retrieved at time...22223
16:56:15.845 -> Retrieved at time...22252
16:56:15.879 -> Retrieved at time...22280
16:56:15.914 -> Retrieved at time...22308
16:56:15.948 -> Retrieved at time...22336
16:56:15.948 -> Retrieved at time...22364
16:56:15.982 -> Retrieved at time...22392
16:56:16.016 -> Retrieved at time...22420
16:56:16.050 -> Retrieved at time...22448
16:56:16.084 -> Retrieved at time...22476
16:56:16.084 -> Retrieved at time...22505
16:56:16.118 -> Retrieved at time...22532
16:56:16.153 -> Retrieved at time...22561
16:56:16.188 -> Retrieved at time...22589
16:56:16.222 -> Retrieved at time...22616
16:56:16.257 -> Retrieved at time...22645
16:56:16.257 -> Retrieved at time...22673
16:56:16.291 -> Retrieved at time...22701
16:56:16.325 -> Retrieved at time...22729
16:56:16.360 -> Retrieved at time...22758
16:56:16.393 -> Retrieved at time...22785
16:56:16.393 -> Retrieved at time...22813
16:56:16.428 -> Retrieved at time...22842
16:56:16.461 -> Retrieved at time...22869
16:56:16.495 -> Retrieved at time...22898
16:56:16.529 -> Retrieved at time...22926
16:56:16.564 -> Retrieved at time...22953
16:56:16.564 -> Retrieved at time...22982
16:56:16.598 -> Retrieved at time...23010
16:56:16.632 -> Retrieved at time...23038
16:56:16.666 -> Retrieved at time...23066
16:56:16.699 -> Retrieved at time...23095
16:56:16.699 -> Retrieved at time...23122
16:56:16.734 -> Retrieved at time...23150
16:56:16.768 -> Retrieved at time...23179
16:56:16.802 -> Retrieved at time...23206
16:56:16.836 -> Retrieved at time...23235
16:56:16.870 -> Retrieved at time...23263
16:56:16.870 -> Retrieved at time...23291
16:56:16.904 -> Retrieved at time...23319
16:56:16.938 -> Retrieved at time...23347
16:56:16.973 -> Retrieved at time...23375
16:56:17.007 -> Retrieved at time...23403
16:56:17.041 -> Retrieved at time...23432
16:56:17.041 -> Retrieved at time...23459
16:56:17.075 -> Retrieved at time...23487
16:56:17.109 -> Retrieved at time...23515
16:56:17.143 -> Retrieved at time...23543
16:56:17.178 -> Retrieved at time...23572
16:56:17.178 -> Retrieved at time...23599
16:56:17.212 -> Retrieved at time...23628
16:56:17.245 -> Retrieved at time...23656
16:56:17.280 -> Retrieved at time...23683
16:56:17.314 -> Retrieved at time...23712
16:56:17.348 -> Retrieved at time...23740
16:56:17.348 -> Retrieved at time...23768
16:56:17.382 -> Retrieved at time...23796
16:56:17.416 -> Retrieved at time...23825
16:56:17.451 -> Retrieved at time...23852
16:56:17.486 -> Retrieved at time...23880
16:56:17.486 -> Retrieved at time...23909
16:56:17.520 -> Retrieved at time...23936
16:56:17.554 -> Retrieved at time...23965
16:56:17.588 -> Retrieved at time...23993
16:56:17.622 -> Retrieved at time...24020
16:56:17.657 -> Retrieved at time...24049
16:56:17.657 -> Retrieved at time...24077
16:56:17.692 -> Retrieved at time...24105
16:56:17.727 -> Retrieved at time...24133
16:56:17.761 -> Retrieved at time...24162
16:56:17.795 -> Retrieved at time...24189
16:56:17.829 -> Retrieved at time...24217
16:56:17.829 -> Retrieved at time...24246
16:56:17.862 -> Retrieved at time...24273
16:56:17.897 -> Retrieved at time...24302
16:56:17.932 -> Retrieved at time...24330
16:56:17.965 -> Retrieved at time...24358
16:56:17.965 -> Retrieved at time...24386
16:56:18.000 -> Retrieved at time...24414
16:56:18.034 -> Retrieved at time...24442
16:56:18.068 -> Retrieved at time...24470
16:56:18.102 -> Retrieved at time...24499
16:56:18.136 -> Retrieved at time...24526
16:56:18.136 -> Retrieved at time...24555
16:56:18.170 -> Retrieved at time...24582
16:56:18.204 -> Retrieved at time...24610
16:56:18.237 -> Retrieved at time...24639
I really only need the first line in this string. How do i edit the code for only the first line? The code I used to get this is such:
if (cueState_Retrieval == HIGH){
retrieved();
}
void retrieved() { // Again, just trying to write down when I'm getting a pulse from another program.
Serial.print("Retrieved at time...");
Serial.println(currentMillis-sessiontimestart);
}
Also, is there any way to export directly to excel from this? How do I generate a .xlsx file from serial communication?
See the PLX DAQ thread
Covers pretty much all you need.
We really only needed a few dozenâŚ
Anyway, what you need to do is call retrieved() not because cueState_Retrieval is high, but only when it becomes high.
It's called "state change detection" in general, and there is an example of it in the IDE.
I think it's Examples/02.Digital/StateChangeDetection or something.
You need to save the previous value of cueState_Retrieval and when the new value is different and high, do the printing.
Anyway, all I can type in transit on the tiny window.Google state change detection arduino and find a few articles one will hit you.
a7
...
Thank you! I tried a similar version from alto777, worked nicely. Thanks again!
Thank you!! Yeah googled it, and it works like a charm. Although I'm having trouble with the increments, as it reads through. I'm going to post my edited code shortly, maybe you can take a look and see what's up.
Thanks Ballscrewbob! Unfortunately, the PLX DAQ interface doesn't allow to record from multiple COMs at the same time. I have 4 different setups currently and I need to output from all 4 arduinos at the same time. Is there any easy way to do this?
This is my updated code. Thanks again for all the input. However, I'm still having some issues. One issue is that the laser pulses twice the first loop, which doesn't make any sense especially because I have this if statement:
if (currentMillis - Millis_stop >= 10000){
Serial.print ("Current millis...");
Serial.println(currentMillis);
Serial.print ("Millis_stop...");
Serial.println(Millis_stop);
Somehow, it's getting inside this statement even though the value for the first go should be negative. Then after the second loop, it works normally, as is shown in the COM:
13:17:31.334 -> Current millis...0
13:17:31.334 -> Millis_stop...10155
13:17:32.455 -> Current millis...11279
13:17:32.455 -> Millis_stop...0
13:17:42.473 -> Current millis...21279
13:17:42.473 -> Millis_stop...11279
13:17:52.459 -> Current millis...31279
13:17:52.459 -> Millis_stop...21279
Can this be explained?
The other bit of trouble I'm having is that the laser is on for 1.020 seconds. Is there any way I can it be on for exactly 1 second? Is this the error for actually reading the code? I don't have a delay function, but it seems that it's still sort of blocking.
13:31:44.411 -> Laser bursting for (in milliseconds)...1020
13:31:45.536 -> Laser bursting for (in milliseconds)...1020
13:31:55.530 -> Laser bursting for (in milliseconds)...1020
13:32:05.523 -> Laser bursting for (in milliseconds)...1020
13:32:15.525 -> Laser bursting for (in milliseconds)...1020
The third issue i'm having is that the cueState_Retrieval_count++ function is not working. It shows increments of two instead of one. For example, if I retrieve 3 times, it's showing 5:
13:42:56.842 -> Retrieved at time...5946
13:42:56.842 -> Number of retrievals so far...1
13:43:08.368 -> Retrieved at time...17476
13:43:08.368 -> Number of retrievals so far...3
13:43:53.471 -> Retrieved at time...62597
13:43:53.471 -> Number of retrievals so far...5
This is the entire code and again, thanks for everyone's help with this! I appreciate it.
//COM1
int switchState_Cue = 0;
int cueState_Retrieval = 0;
int cueState_Retrieval_count = 0;
int last_cueState = 0;
int reset_Millis_stop = 0;
int reset_start_Laser = 0;
int reset_startoffperiod = 0;
int reset_realLaser = 0;
long sessiontime;
long sessiontimestart;
long sessiontimestop;
long totalsessiontime;
long currentMillis;
long cueState_Retrieval_millis;
long stopLaser;
long laserlimit;
long byeLaser;
long startoffperiod;
long endoffperiod;
long startLaser;
long endLaser;
long realLaser;
unsigned long Millis_start = 0;
unsigned long Millis_stop = 0;
void setup() {
// put your setup code here, to run once:
pinMode(3, OUTPUT); //Laser output
pinMode(2,OUTPUT); //LED output
pinMode(12,INPUT);//Cue input
pinMode(11,INPUT);//Second cue input from another program (TTL converter).
laserlimit = 1000; //laser will stop after 1 second
sessiontime = 4200000; // 1hour 10min
Serial.begin(115200);
digitalWrite(2, HIGH);
delay(2000);
digitalWrite(2, LOW);
delay(0);
}
void loop() {
switchState_Cue = digitalRead(12); // First time cue goes off, that's when session begins.
if (switchState_Cue == HIGH) {
sessiontimestart = millis();
digitalWrite(2, HIGH); // this is just pulsing the LED, to make sure that the system is working still.
reset_Millis_stop = reset_Millis_stop +1; //resetting millis to start 10 second increments.
while (currentMillis - sessiontimestart < sessiontime){
// Every loop, checking to see if pellets were retrieved by animal.
cueState_Retrieval = digitalRead(11);
if (cueState_Retrieval != last_cueState){
if (cueState_Retrieval == HIGH){
cueState_Retrieval_count++;
retrieved();
}
}
last_cueState = cueState_Retrieval;
// Done with checking.
// Resetting millis_stop to figure out if ten seconds have passed.
if (reset_Millis_stop> 0){
Millis_stop =millis();
reset_Millis_stop = reset_Millis_stop-1;
}
// Pulsing laser.
if (currentMillis - Millis_stop >= 10000){
// Serial.print ("Current millis...");
// Serial.println(currentMillis);
// Serial.print ("Millis_stop...");
// Serial.println(Millis_stop);
Millis_stop = currentMillis;
reset_realLaser = reset_realLaser + 1;
if (reset_realLaser>0){
realLaser = millis();
reset_realLaser = reset_realLaser - 1;
}
// Every ten seconds, the laser should go off for 1 second.
while ((byeLaser - realLaser) <= 1000){
byeLaser = millis();
// Resetting the start time for laser initiation!
reset_start_Laser = reset_start_Laser+1;
if (reset_start_Laser>0){
startLaser = millis();
reset_start_Laser = reset_start_Laser-1;
}
//Laser on HIGH for 10 milliseconds.
while ((endLaser - startLaser) <= 10){
digitalWrite(3, HIGH); // 10hz laser ON
endLaser = millis();
}
// Resetting the off period in the 10hz cycle!
reset_startoffperiod = reset_startoffperiod+1;
if (reset_startoffperiod>0){
startoffperiod = millis();
reset_startoffperiod = reset_startoffperiod-1;
}
// Laser on LOW for 90 milliseconds.
while(endoffperiod-startoffperiod <= 90){
digitalWrite(3,LOW);
endoffperiod = millis();
}
}
// Checking how long the laser is on for - is it on for 1 second exactly?
// Serial.print("Laser bursting for (in milliseconds)...");
// Serial.println(byeLaser-realLaser);
}
currentMillis = millis();
}
digitalWrite(2, LOW); // Session is over
sessiontimestop = millis();
totalsessiontime = sessiontimestop - sessiontimestart;
Serial.print("Total Session Time...");
Serial.println(totalsessiontime);
Serial.end();
}
}
void retrieved() { // Again, just trying to write down when I'm getting a pulse from another program.
Serial.print("Retrieved at time...");
Serial.println(currentMillis-sessiontimestart);
Serial.print("Number of retrievals so far...");
Serial.println(cueState_Retrieval_count++);
}
Ah I actually figured out the bit about the laser pulsing twice. Just had to move the currentMillis at the beginning of the main while loop, as such:
while (currentMillis - sessiontimestart < sessiontime){
currentMillis = millis();
Now the laser pulses only after 10 seconds have passed, not before.
Any of the CLOUD based items may suit your needs better.
Eg. Thingspeak, AWS (free version), UBIdots etc.
Have used thingspeak here and was quite impressed.
There are more and a lot of them are FREE but don't go just on what I say as there are lots of IOT clouds out there.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.