I am trying to generate pulses at the same time I checked (Gammon Forum : Electronics : Microprocessors : How to do multiple things at once ... like cook bacon and eggs) it works great but the problem is I want to start generating the pulses after 0.8333 ms not from zero what I have to do? Also if I want to generate several pulses from the same pin without using delay and by using gammon way what I have to do ??
walkalone:
I am trying to generate pulses at the same time I checked (Gammon Forum : Electronics : Microprocessors : How to do multiple things at once ... like cook bacon and eggs) it works great but the problem is I want to start generating the pulses after 0.8333 ms not from zero what I have to do?
Put things that have to be done once on startup in void setup()
delayMicroseconds(833300);
walkalone:
Also if I want to generate several pulses from the same pin without using delay and by using gammon way what I have to do ??
You could use several of those 'timed' variables, like 'bacon, 'eggs', 'coffee'.
And an 'if' or 'if else' statement to combine them.
if (bacon == HIGH && eggs == HIGH && coffee ==HIGH)
{
eatNow = HIGH;
}
else
{
eatNow = LOW;
}
I attached a new file with the topic which are the pulses I want to generate
What have you tried?
walkalone:
I am trying to generate pulses at the same time I checked
@walkalone, I would very much appreciate it if you would be kind enough to modify your Original Post and change your Title so that newcomers do not confuse it with my Demonstration code for several things at the same time tutorial. Perhaps just change your title to "Question about several things at the same time"
Thank you.
I also hope my tutorial may help with your project.
...R
@Robin2 can you help in generating these pulses?
Wawa:
Put things that have to be done once on startup in void setup()
delayMicroseconds(833300);This delay will happen once I want to do the same delay each cycle
"This delay will happen once I want to do the same delay each cycle"
Then it seems you didn't understand 'several things at the same time'.
Try to write code for one signal, before you add the second and third one.
The process of writing the first one might help you understand.
Post your code if you want help with that.
Leo..
Edit
No use sending a PM with multiple delays. Not going to help you with that.
Try to understand the concept of millis().
Look at the BlinkWithoutDelay sketch in the examples of the IDE.
walkalone:
@Robin2 can you help in generating these pulses?
Thank you very much for changing your title.
I will certainly try to help but it would be much easier if you start by posting the program that represents your best attempt. Then it will be much clearer where you are stuck.
...R
const int led_A_Pin=12;
const int led_B_Pin=11;
const int led_C_Pin=10;
const int led_A_Interval= 20;
const int led_B_Interval= 20;
const int led_C_Interval= 20;
const int blinkDuration_A = 1.61111;
const int blinkDuration_B = 5;
const int blinkDuration_C = 1.61111;
byte led_A_State = LOW;
byte led_B_State = LOW;
byte led_C_State = LOW;
unsigned long currentMillis = 0;
unsigned long previousLed_A_Millis = 0;
unsigned long previousLed_B_Millis = 0;
unsigned long previousLed_C_Millis = 0;
void setup() {
pinMode(led_A_Pin, OUTPUT);
pinMode(led_B_Pin, OUTPUT);
pinMode(led_C_Pin, OUTPUT);
}
void loop(){
currentMillis = millis();
updateLed_A_State();
updateLed_B_State();
updateLed_C_State();
switchLeds();
}
void updateLed_A_State() {
if (led_A_State == LOW) {
if (currentMillis - previousLed_A_Millis >= led_A_Interval) {
led_A_State = HIGH;
previousLed_A_Millis += led_A_Interval;
}
}
else {
if (currentMillis - previousLed_A_Millis >= blinkDuration_A) {
led_A_State = LOW;
previousLed_A_Millis += blinkDuration_A;
}
}
}
void updateLed_B_State() {
if (led_B_State == LOW) {
if (currentMillis - previousLed_B_Millis >= led_B_Interval) {
led_B_State = HIGH;
previousLed_B_Millis += led_B_Interval;
}
}
else {
if (currentMillis - previousLed_B_Millis >= blinkDuration_B) {
led_B_State = LOW;
previousLed_B_Millis += blinkDuration_B;
}
}
}
void updateLed_C_State() {
if (led_C_State == LOW) {
if (currentMillis - previousLed_C_Millis >= led_C_Interval) {
led_C_State = HIGH;
previousLed_C_Millis += led_C_Interval;
}
}
else {
if (currentMillis - previousLed_C_Millis >= blinkDuration_C) {
led_C_State = LOW;
previousLed_C_Millis += blinkDuration_C;
}
}
}
void switchLeds() {
digitalWrite(led_A_Pin, led_A_State);
digitalWrite(led_B_Pin, led_B_State);
digitalWrite(led_C_Pin, led_C_State);
}
This is the code I am using I want to put a delay of (0.8333ms ) for A, (5ms)for B and (0.8333ms ) for C by keeping the same frequency for all of them. Also I want to know if there is any way I can generate all three from same pin with the desired delays.
This is the code I am using I want to put a delay of (0.8333ms )
I think you misspelled 833.3us
Please remember to use code tags when posting code.
Big difference between milliseconds and microseconds. Sort that out first.
For your simple project, delayMicroseconds will further simplify it immensely.
The delay I want is 0.83333 msec or 833 usec
Well there's your clue. You can't measure time values less than one millisecond when your measuring stick is one millisecond long.
millis() returns an integer. Specifically, an unsigned long integer. It will never show you 0.833.
Why do you want to avoid using delay?
Why are there things like this
const int blinkDuration_A = 1.61111;
trying to define an INTEGER as a decimal?
walkalone:
This is the code I am using I want to put a delay of (0.8333ms ) for A, (5ms)for B and (0.8333ms ) for C by keeping the same frequency for all of them. Also I want to know if there is any way I can generate all three from same pin with the desired delays.
Try setting the initial values of led_A_Interval etc to include the delay - something like this
// at the top of the program
const standardBinterval = 20;
int led_B_Interval= standardBinterval + 5; // note that it is no longer const
// and in your function
void updateLed_B_State() {
if (led_B_State == LOW) {
if (currentMillis - previousLed_B_Millis >= led_B_Interval) {
led_B_Interval = standardBinterval; // set the value back to the standard
led_B_State = HIGH;
previousLed_B_Millis += led_B_Interval;
}
}
else {
if (currentMillis - previousLed_B_Millis >= blinkDuration_B) {
led_B_State = LOW;
previousLed_B_Millis += blinkDuration_B;
}
}
}
I have used the B LED as an example because you want to add 5 millis. If you want to add a number of microseconds for your other functions you will have to change your code to use micros() rather than millis() - otherwise it will be much the same.
...R