Hot answers tagged

11

The pulsein() function is very lossy, in that it is a hard loop and returns a number * the assumed clock cycles it takes for per loop ... // wait for the pulse to stop while ((*portInputRegister(port) & bit) == stateMask) { if (numloops++ == maxloops) return 0; width++; } // convert the reading to microseconds. The loop has been determined // ...


10

There's one important thing that you need to remember when working with time on an Arudino of any form: Every operation takes time. Your foo() function will take an amount of time. What that time is, we cannot say. The most reliable way of dealing with time is to only rely on the time for triggering, not for working out when the next triggering should be....


5

So I was wondering if I could attach an ISR to timer0 without affecting the above Arduino functions, Yes. A few ways, depending on your level of comfort: You can declare the stock Arduino Timer0 OVF "weak" and write your own where you can insert your ISR. But you have to handle the interaction between the millis() / micros() related variables. You can ...


4

You could split Request() into two separate functions: You call sendRequest(), then go do other stuff, then, some time later, you call getResponse(). If you get RESPONSE_NOT_READY, then you know you have to call it later: #define REQ_WAIT_DELAY 250 // Wait for 250 ms #define RESPONSE_NOT_READY ((uint16_t) -1) uint32_t time_request_sent; void COZIR::...


4

I generally calculate whether the led must be on or off, based on the current time. //Making a 5/4 Polyrythm with 2 LEDs const int LED13 = 13; const int LED7 = 7; int i = 5; void setup() { pinMode(LED13, OUTPUT); pinMode(LED7, OUTPUT); } void loop() { // led 13 flashes 5 times per interval of 6 seconds. So the led is on/off for 600ms ( 6000/(2*...


4

Generating a linear frequency ramp with accurate timings is not an easy task. I am providing here only a partial answer, where I first go through the math of the problem, then give some ideas for the implementation. The math We are dealing with a frequency-modulated signal, with the frequency ramping linearly from f = 0 at t = 0 up to a ...


4

After correcting the syntax error (missing semicolon) I tried your code on my Teensy 3.0. The results look OK to me: Start... 1250 2250 Finished 1000 milliseconds elapsed Start... 2250 3250 Finished 1000 milliseconds elapsed Start... 3250 4250 Finished 1000 milliseconds elapsed Start... 4250 5250 Finished 1000 milliseconds elapsed Start... 5250 6250 ...


4

Yes, the ports of an Uno are addressed in C/C++ as PORTB, PORTC, and PORTD respectively, each 8 bits wide. You can read them or assign to them. Other processors' ports have similar names. Of course the pins' port assignments and functions differ from one to the other, which is one reason for providing the single-pin pinMode() and digitalRead(), digitalWrite()...


4

In setup(), add randomSeed(analogRead(0)); This reads the value of an analog input pin, which if not connected, will float to relatively random values between 0 and 1023. This "seeds" the random number generator so the pattern of random numbers you will get later doesn't always give the same results each time you start the sketch. In your delay() call, ...


3

The primary downside to calling a function each time is that it may take more CPU cycles to complete the overall loop. The difference in CPU cycles, if any, depends on the actual implementation of the function and how the compiler optimises the call. For low-CPU sketches, this difference is probably unnoticeable. Generally speaking, it is good programming ...


3

For your external source to be an SPI bus master, it has to be the only one generating the clock signal. Yet, calling SPI.transfer() also generates a clock signal from the Arduino; both devices are attempting to drive the clock line simultaneously, hence your problems. You have to bit-bang this or manipulate the SAM registers yourself to configure it as a ...


3

If you need to do more than 8 (especially LOTS more), and timing is very critical, you can use a Serial In, Parallel Out (SIPO) chip, such as the 74HC595 - it also saves a lot of pins on the Arduino. To use it, you need 3 pins connected to the Arduino. You send one bit to the chip from the Arduino for each output, then when you have them all set, you toggle ...


3

As mentioned in comments and other answers, using timer 1 for hardware-timed bit toggling is likely to produce better results than code using delays based on calls like delayMicroseconds(), primarily because of overhead causing poor resolution or jitter. Using timer 1 in a PWM mode offers an advantage: loading of the OCR1A register is double buffered in PWM ...


3

Some time ago, I timed digitalWrite() and direct port write commands by looping over 10,000 executions of each and timing them with millis(). I made both tests on a 16MHz Atmega 328p at 16MHz. For digitalWrite(13, LOW), I got 2.7usec / call. For PORTB &= 0x20;, I got 0.15usec / statement. (The direct port access was executed 10 times within the loop to ...


2

Test the LED: Connect it between +5v and ground with its current-limiting resistor. Make sure the cathode is connected to ground and not the other way round. Inside the plastic bubble are two tiny bits of metal. The larger one is the cathode. Does it light? If not, the LED is faulty or the resistor is too high. Reconnect the LED to pin 8, which is physical ...


2

Here is some test data of a pulseIn test. One Arduino sent what were supposed to be 14us pulses, and the other spat out this data: 18,18,18,12,18,18,18,18,18,18,18,18,18,18,18,18,18,24,19,18,18,18,18,18,24,18,18,18,19,18,18,12,18,18,19,18,18,18,18,18,18,18,18,18,18,18,19,18,19,24,18,18,18,18,18,18,18,24,18,18,18,18,18,18,18,18,18,18,18,18,18,19,18,18,18,...


2

You can use the optimize function attribute to change the compiler optimization level for an individual function. That's the closest you'll get. optimize The optimize attribute is used to specify that a function is to be compiled with different optimization options than specified on the command line. Arguments can either be numbers or strings. ...


2

As per comment and Majenko's answer you are current creating local variables called secLeft and secLeft2 inside the 'if block. These take precedence over the ones you have defined globally. Removing the in declarations from inside the if block will stop doing that and point you at the globally defined variables. As to the rest of the code. If you have ...


2

What you want, namely something to “smooth the received (GPS) time”, is called a GPS disciplined oscillator. Obviously, you are not after the high accuracy of commercial GPSDOs, but the working principle of what you are describing is essentially the same. what do I do when the GPS can't find a signal? In GPSDO lingo, this is called the “holdover” state. ...


2

As I wrote in my comment, you don't actually set the value of interval to the new value, when the encoder position changes. So the blinking interval does not change. You need to use the formula to set the new value to interval. Insert interval = 60.00 / encoder0Pos * 1000; before your Serial.print() statements. Also, you need to remove the const keyword at ...


1

As majenko answered in the comments, the blinking rate will be so fast you can not see it. Also it is possible that without the delay the optimizer removes the whole counting. So increasing the number as proposed by majenko may not even work due to compiler optimisations. Using a method based on counters is a very unreliable way to use timings. Look at ...


1

Arduino is universal platform, lot of wrappers to hide real hardware and it is slow. For example digital write is about 100times slower than direct access to hardware. For example for AVR Mega328p based Arduino analogRead looks like (without macros): int analogRead(uint8_t pin) { uint8_t low, high; if (pin >= 14) pin -= 14; // allow for channel ...


1

This statement: static unsigned long timing=micros(); ...only gives it an initial value. It is not executed during loop. These kinds of variables should be assigned an initial value in an executable section, perhaps when the alarm time is set. This requires the definition to be pulled out, to the file scope: static unsigned long timing; void loop(){ ...


1

It has come to my attention I never posted a followup to this but I was actually able to solve this problem by shielding the wire going from the switch to the arduino. The switch's wire was about 2 to 3 inches long and I wrapped aluminum foil around it, which shielded the signal from what I suspected was EMI induced by the switching converters.


1

See https://arduino.stackexchange.com/a/17929/6628 with code like: const int onTime = 500; const int measureTime = (int) (60*1000/10); // 10 BPM, or 1500*4ms/measure const int off4 = measureTime/4 - onTime; // take care these don't go negative const int off5 = measureTime/5 - onTime; // unsigned long next5; unsigned long next4; ... loop(){ unsigned ...


1

Never mind; this was me making a mess of the bit masking. See this github repository for working code with Arduino Mega.


1

Try this: unsigned long pressureOnTime; //millis() time when pressure switch is first turned on const unsigned long ledOnAfterTimeThreshold = 300000; //5 minutes * 60 sec * 1000 mSec int pressureThreshold = 1; //minimum analog reading from pressure sensor considered to be "on" const int fsrAnalogePin = 0; const int ledPin = 11; int ledState = LOW; int ...


1

If you're looking to speed your code up I would recommend not using digitalWrite or Serial. When using digitalWrite it can take quite a few unneeded clock cycles to turn the pin on or off, as a replacement I would suggest looking into using port manipulation. Example of turning a pin on or off with a preprocessor macro using port manipulation #define ...


1

You need to make sure that the LED is connected with the right polarity, you need to connect the negative to Ground and the positive to the digital pin 8. Try the led by connecting 3.3v and GND so you are sure that the led is not broken.


1

I see that the code is OK, are you sure that the led pis is 8 ? what is your input voltage ? (is it between 7 to 12 ?) did you tried lower time values ? (like 1:2 minuts ?) if you answer to all of these questions is "yes" then probably the arduino board is broken.


Only top voted, non community-wiki answers of a minimum length are eligible