Hi and thank you for being here.
This code functions well for my app but need to decrease on time or up to 2kHz for full cycle.
Would a timer or micro instead of milli seconds be needed. Very limited experience and understanding.
Thanks,
bro d
/*
Switch 2 pins on and off alternately.
*/
void setup() {
// initialize digital pins as outputs.
pinMode(8, OUTPUT);
pinMode(13, OUTPUT);
}
// the loop routine
void loop() {
digitalWrite(13, HIGH); // SW 1 ON
delay(1); // ms wait
digitalWrite(13, LOW); // SW 1 OFF
digitalWrite(8, HIGH); // SW 2 ON
delay(1); // ms wait
digitalWrite(8, LOW); // SW 2 OFF
}
Moderator edit:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.
Thankyou luislva,
I put microseconds in the delay and with 9 zeros (max value) both leds stay on solid.
Shouldn't they be switching off for 1 second at a million micro seconds delay?
Thanks,
bro d
void loop() {
digitalWrite(13, HIGH); // SW 1 ON
delayMicroseconds(1000000000); // us wait
digitalWrite(13, LOW); // SW 1 OFF
digitalWrite(8, HIGH); // SW 2 ON
delayMicroseconds(1000000000); // us wait
digitalWrite(8, LOW); // SW 2 OFF
}
Moderator edit:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.
delayMicroseconds is defined this way
void delayMicroseconds(unsigned int us)
that means that your 1000000000 is truncated to a 16 bits unsigned int,
so the delay is max 65 milliseconds.
long delays are programmed this way
delay(9);
delaymicros(500);
for a delay of 9.5 milliseconds.
If you want to delay 1 second you must use the function delay()
. the function delayMicroseconds
is good to delay less that a millisecond.
luisilva:
If you want to delay 1 second you must use the function delay()
. the function delayMicroseconds
is good to delay less that a millisecond.
in my experience delayMIcroseconds() works well up to ~10 milliseconds.
but you can always use the "blink without delay" technique - http://arduino.cc/en/Tutorial/BlinkWithoutDelay -
to get precise timing, either with millis() or micros()
Decided to look for a pattern with the delayMicroseconds(); entries.
Table with explanantion and code below.
/*
dualaltSW_jul26c
The first num in the equations is the delay micros entry
The 2nd number in the equations is the O-scope elapsed time for the particular delay micros entry
Blinking LEDS alternately
30MHz dual trace scope
1) 999900us=.5ms
2) 999800us=.4ms
3) 999700us=.3ms
4) 999600us=.2ms
5) 999500us=.08ms
6) 999490us=~.07ms
7) 999480us=.06ms
8) 999470us=.05ms
9) 999460=40us(.04ms)
10) 999450us=30us
11) 999440us=20us
12) 999430us=~10us LOW SPEED LIMIT?
13) 999410 and 999420us=22milli seconds
14) 999400us=22milli seconds
Perhaps one can make good sense of this.
*/
// Switch 2 pins on and off alternately.
void setup() {
// initialize digital pins as outputs.
pinMode(13, OUTPUT);
pinMode(8, OUTPUT);
}
// the loop routine
void loop() {
digitalWrite(13, HIGH); // SW 2 ON
delayMicroseconds(999490); // us wait
digitalWrite(13, LOW); // SW 2 OFF
digitalWrite(8, HIGH); // SW 2 ON
delayMicroseconds(999490); // us wait
digitalWrite(8, LOW); // SW 2 OFF
}
Moderator edit:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.
@brodonhaas
Wrong call
** delayMicroseconds(999490); // us wait **
As said in my previous post the MAXIMUM VALUE you can put in the delayMicroseconds is 16 bit unsigned int (==65535)
999490 is a 32 bit integer. It just does not fit in a 16 bit var.
Please reread my previous post then you should know that you should write
** delay (999); // whole millis part to wait **
delayMicroseconds(490); // the fractional millis part to wait
Thank you robtillaart,
I understand better about the info in your previous post and I than you for your time and patience.
I attempted to show, with a scope, what arduino was actually putting out with those input numbers
I'll apply your instructions and learn to use "delayMicroseconds()" correctly.
I need to learn how to switch to pins alternately without "delay()", also.
I'm looking at Control Structures and then Timer1.
In the sketch examples for these matters I'm unable to rewrite the code to incorporate 2 switches.
"blink without delay" is one example.
Thanks again for your time and patience,
I greatly value your input.
bro d
you must try not to use delay or delayMicroseconds at all except for the most simple sketches.
const int LED1 = 5;
const int LED2 = 6;
unsigned long oneLed= 0;
unsigned long otherLed = 0;
unsigned long interval = 100000;
unsigned long checkSwitch1 = 0;
void setup()
{
}
void loop()
{
if (micros() - oneLed > 100000UL) // 10 x per second
{
digitalWrite(LED1, !digitalRead(LED1)); // toggle the led
oneLed += 100000UL;
}
if (micros() - otherLed > interval )
{
digitalWrite(LED2, !digitalRead(LED2)); // toggle the led
otherLed += interval ;
}
if (millis() - checkSwitch1 > 250) // test switch 4 times per second
{
checkSwitch1 += 250;
state = digitalRead(switchPin);
}
if (state == HIGH) // change the interval of the LED 2 depending on the state of the switch
{
interval = 100000;
}
else
{
interval = 400000;
}
}
Hi robtillaart,
Checked the code from your last post.
Compiler report at bottom.
const int LED2 = 6;
unsigned long oneLed= 0;
unsigned long otherLed = 0;
unsigned long interval = 100000;
unsigned long checkSwitch1 = 0;
void setup()
{
}
void loop()
{
if (micros() - oneLed > 100000UL) // 10 x per second
{
digitalWrite(LED1, !digitalRead(LED1)); // toggle the led
oneLed += 100000UL;
}
if (micros() - otherLed > interval )
{
digitalWrite(LED2, !digitalRead(LED2)); // toggle the led
otherLed += interval ;
}
if (millis() - checkSwitch1 > 250) // test switch 4 times per second
{
checkSwitch1 += 250;
state = digitalRead(switchPin);
}
if (state == HIGH) // change the interval of the LED 2 depending on the state of the switch
{
interval = 100000;
}
else
{
interval = 400000;
}
}
Compiler says:
sketch_jul27a.ino: In function 'void loop()':
sketch_jul27a:28: error: 'state' was not declared in this scope
sketch_jul27a:28: error: 'switchPin' was not declared in this scope
sketch_jul27a:31: error: 'state' was not declared in this scope
Hi All,
How do I write this for microseconds?
const unsigned long PERIOD1 = 1000;
Thanks,
bro d