Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Delta-Wye
Sep 29, 2005

TwystNeko posted:

I haven't gotten around to doing anything with the analogread function or FHT/FFT yet. I'm trying to get my matrix working. I'm trying to use a MAX7219 to control it, but it's not working at all. I've got it hooked up as per the datasheet and what it says on the arduino site, but all I get is all LEDS on, except the ones on row 2. I've redone all the connections twice now, and no dice. I wonder if I've shorted out the chip.. but I've never given it more than 5v, and it's rated for 6v.

Any ideas? I'd really just like a 16x8 blue matrix in the helmet. I suspect I may just have to use more of the RGB leds to make a grid. I have 50 of them left.

I pulled the datasheet and glanced at it but I didn't see any obvious gotchas. Do you have a schematic of the circuit you've wired up?

Adbot
ADBOT LOVES YOU

TwystNeko
Dec 25, 2004

*ya~~wn*
Sure, it's just the basic one from the arduino site: http://playground.arduino.cc//Main/MAX72XXHardware

.. Maybe here's a stupid question - would the LEDs all be on, if I accidently swapped the direction of connections? ie, I have the anode/cathode backwards? I would think I'd not get anything at all. Maybe I misread the datasheet - it should be Seg0-7 > Anode, and Digit0-7 to Cathode, right?

edit: Okay, I'm dumb. Got it working, I had the pins reversed. :hurr:

TwystNeko fucked around with this message at 05:09 on Oct 4, 2013

TVarmy
Sep 11, 2011

like food and water, my posting has no intrinsic value

I have an idea for a project that would essentially call for asking an Arduino or similar microcontroller to accept a tone as input.

In other words, recognition of multiple single tones, from a flute, recorder or ocarina. Preferably, it would detect 8 tones.

I saw guides for the 567 tone decoder, but it seems to only detect one tone at a time based on how the circuit is hardwired. I could build 8 variations of the circuit and feed that into the Arduino chip, but I was hoping to fit this into a relatively small space. However, I've been looking into trying SMD soldering, and a small circuit using the SOIC package might work.

But I was wondering if there was a way to do this in software or with an integrated circuit that could detect multiple tones from one package? I see a lot of DTMF tone detection packages, but I don't really get if they can detect arbitrary tones other than the phone system's tones.

Based on a quick google search, some sites say you can kind of do this in software with an FFT library for the Arduino, but people report that it's slow, and often people get mad in the threads and say that FFT is not the optimal way to detect tones.

I'm also open to checking out other microcontrollers for this project if there are any that are better suited for audio processing. Preferably, it'd be one that has an arduino-style library for it so I don't have to learn some platform specific ASM or C libraries. I tried programming AVR chips from straight C, and while it was great for learning how to use timers and interrupts, it also took me a while to get things working.

To be clearer, I don't need the output from the tone detection to be complicated. I just want a simple binary test for if the tone is playing or not.

TVarmy fucked around with this message at 19:56 on Oct 13, 2013

Delta-Wye
Sep 29, 2005
You may get away with a zero-crossing detector? I've been wanting to try this so you should do it for me :colbert:

The basic idea gist is to count the zero-crossings and average out a series of samples hoping that will more-or-less count the number of times the fundamental has crossed the zero axis. All of the extra frequency data complicates it, but if you have a set number of frequencies to search for it can be as easy as counting zero crossings and see if the average number over a period of time fits within a band around each of your pitches. If it works it would be a pretty straight-forward solution.

TwystNeko
Dec 25, 2004

*ya~~wn*
So finally, I've gotten my matrix working. I ended up going with the Adafruit 16x8 matrix driver board. I did have to rewrite the LED Backpack library, since it didn't give me access to anything but raw access - no drawing functions.

That being said, now that I have all the functions working, it's lots of fun to work with. I just need to figure out some neat animations - preferably algorithmic. I was going to do a spiral, but I can't figure out how to do it without a dozen for loops. Any suggestions? I have a nice random matrix-like effect working, since I can manipulate the display buffer at will. Any suggestions for effects? Psuedo-code is good, as it all comes down to a drawPixel function I implemented.

The Adafruit_GFX library is actually pretty nice, as it implements things like Bresenham's line-drawing algorithm for point-to-point lines, and circles/triangles/rounded rectangles. All decently abstracted. I added screen scrolling, which shifts everything one step in a given direction, with wrap-around.

Also, does anyone have a decent explanation of arduino to arduino communication? I want to have the arduino running the LED strip tell the other one when it starts and stops an animation, so they can sync up. I suppose I'll have to get it to change mid-pattern if needed, too. hmm.

Aurium
Oct 10, 2010
When you have a limited set of tones that you want to detect, the canonical solution is the Goertzel Algorithm.

Basically what it does you give it a sound and a set of frequencies and it tells you how much of each frequency is in the sound.

If you're only looking for a few frequencies, it's much faster than a FFT.

This DTMF library uses the Goertzel Algorithm to look at the relative magnitudes of the 8 tones in DTMF. The download link is about halfway down the first page.

The demo program that it comes with a debug flag that gives you the relative magnitudes of those 8 signals.

You should be to change the recognized frequencies, strip out the combination logic to only report the relative magnitudes of the fundamental frequencies of the note being played.

That's where I'd start.

Bad Munki
Nov 4, 2008

We're all mad here.


Can someone take a look at this and tell me what I'm missing?

http://pastebin.com/191AWfV2

What it is: a pretty simple bit of code to read from a keypad, sending the entered numbers via serial and reading a response from the same. It does some animation based on what's going on via fastspi2.

What's going wrong: after a while (not sure exactly, but maybe like 10-15 seconds?) it stops taking input. It's not that the keypad stops responding: if I just hit one of the "terminate" keys (# or *) it jumps straight to the deny animation, without actually sending anything via serial. However, if I punch any of the other keys, they are apparently not appended to the currently-being-entered code because a) the animation doesn't change to show a code being entered, which happens based directly upon the current code.length(), and b) when a terminate key is pressed, I get the quick-deny where it just denies based on the length of the current code, instead of posting it to the serial connection and waiting for a response.

I can describe what's happening in more detail if needed, but the code is pretty straight forward. Just looking for a second set of eyes, mostly.

e: I put in a bunch of debugging output to serial and it's very clearly getting stuck on the timeout, resetting the code all the time after a while. I honestly can't see why, though: if a key is pressed, the first thing it does is reset that timeout. So it resets the timeout, appends the key, and then somehow decides that the timeout has still expired and clears the code.

Bad Munki fucked around with this message at 05:01 on Oct 22, 2013

Bad Munki
Nov 4, 2008

We're all mad here.


Argh, I figured it out, I'm a dummy. I was using an int to track the timeout start, which the unsigned long returned by millis() was rapidly overflowing.

Eugenics
Mar 25, 2007
Because I'm better than you
Hopefully this is the right thread.

So I have a microscope with a mechanically operated stage. Basically it has an X and a Y motor with each motor connected with what looks to be a db25 adapter. The problem is the thing is at least 25 years old and the software is almost definitely lost. I'd like to be able to program an arduino( or a raspberry pi) to interface with the microscope motors and move the stage around with a small joystick. I don't have much arduino experience, but I know how to solder pretty well. I do have a raspberry pi if that would make it easier.

What is the level of difficulty for this? easy/medium/hard?

Sagebrush
Feb 26, 2012

Easy to medium, depending on the type of motors. Can you see how many wires come out of each one?

Pudgygiant
Apr 8, 2004

Garnet and black? More like gold and blue or whatever the fuck colors these are
I woke up today and I said to myself, "Self, goddammit, even though you have barely any coding experience and no EE experience, you're going to do some home automation poo poo with an Arduino".

Step 1, figure out which Arduino to order. I think I'm in over my head.

Bad Munki
Nov 4, 2008

We're all mad here.


Just get an Uno. It's the most common board and the one 99% of arduino-based guides and tutorials will be using. I'd be shocked if it was lacking some capability you need that another arduino has.

Acid Reflux
Oct 18, 2004

I'll second Bad Munki. I have all sorts of Arduino boards laying around here...Uno, Mega, Nano, Leonardo, even a few DIY flavors...and 9 times out of 10, I've got the Uno plugged in. The tenth time out of ten is usually having one of my smaller homebuilt ones jammed in a breadboard. The only thing an Uno might be lacking for a big home automation project is a sufficient number of inputs or outputs, but there are several ways around it that don't require buying a new dev board.

If you're in the U.S., have a Radio Shack nearby, and don't mind spending a few extra bucks for instant gratification, you can usually grab one right off the shelf along with various gizmos and widgets to plug in.

Bad Munki
Nov 4, 2008

We're all mad here.


Acid Reflux posted:

The only thing an Uno might be lacking for a big home automation project is a sufficient number of inputs or outputs, but there are several ways around it that don't require buying a new dev board.

Yeah, I usually recommend NOT getting a mega or something with a bazillion IO pins for that very reason: learning how to use a mux/demux is a pretty basic and critical thing, so there's no reason to avoid it by just using an oversized board. :)

Sagebrush
Feb 26, 2012

Bad Munki posted:

Just get an Uno. It's the most common board and the one 99% of arduino-based guides and tutorials will be using. I'd be shocked if it was lacking some capability you need that another arduino has.

I personally like the Leonardo these days because it has native USB HID support and uses a micro-USB instead of that big honking printer cable. It's also about $10 cheaper. The Uno is effectively the same board they've been making since 2006, though, so pretty much everything is guaranteed to work on it.

echinopsis
Apr 13, 2004

by Fluffdaddy
I love budget as gently caress faux uno from fasttech.com you can pay a premium to get it in a week or so and still only costs about $12 or so [free shipping] so no excuses really

Although an uno by itself is kinda worthless until you buy a wire and another thing

Bad Munki
Nov 4, 2008

We're all mad here.


Teensyduino is killer, for what it's worth.

TVarmy
Sep 11, 2011

like food and water, my posting has no intrinsic value

Bad Munki posted:

Teensyduino is killer, for what it's worth.

Teensy is great, if you don't need shields. I find myself using my teensy more than a "real" arduino because it fits nicely on a breadboard, but if you find yourself looking at guides that use shields, get an Uno.

Sagebrush
Feb 26, 2012

Yeah, same. I love the teensys and end up embedding them in most of the projects I build where I can't be arsed to make a new PCB for a DIP chip. But I wouldn't recommend anyone buy one of those to learn on, because you lose all of the standardization of pins and the female headers that make an Arduino so non-threatening.

Bad Munki
Nov 4, 2008

We're all mad here.


Definitely agreed, that's why I advised the uno for starting out. For a second or third or nth arduino, though, something like a teensy is way more fun. Only thing I don't like about the teensy 3 is that I hate usb micro, those connectors mount way too flimsily.

turn it up TURN ME ON
Mar 19, 2012

In the Grim Darkness of the Future, there is only war.

...and delicious ice cream.
I talked with a few people about Arduino/Raspberry Pi/Beaglebone yesterday, and finally got some good ideas for projects that I could use the arduino for. Looking forward to going down this rabbit hole, but I'm really not sure what exact board I should start with (more than likely the Uno).

Basically the idea I want to pursue is using the Arduino as a way to take temperature samples in a house (multiple rooms), average them and display them on a very simple website. I could use this to see how effective heating/AC is overall throughout the day.

Would the Uno let me plug 4-5 temperature sensors and a wi-fi board into it, and store temperature from 5 rooms taken at 30 minute intervals throughout the day? That's only something like 3600 samples, so maybe 7 kilobytes of data each day for just storing the text?

I think I'm understanding that right...

Delta-Wye
Sep 29, 2005

SquadronROE posted:

I talked with a few people about Arduino/Raspberry Pi/Beaglebone yesterday, and finally got some good ideas for projects that I could use the arduino for. Looking forward to going down this rabbit hole, but I'm really not sure what exact board I should start with (more than likely the Uno).

Basically the idea I want to pursue is using the Arduino as a way to take temperature samples in a house (multiple rooms), average them and display them on a very simple website. I could use this to see how effective heating/AC is overall throughout the day.

Would the Uno let me plug 4-5 temperature sensors and a wi-fi board into it, and store temperature from 5 rooms taken at 30 minute intervals throughout the day? That's only something like 3600 samples, so maybe 7 kilobytes of data each day for just storing the text?

I think I'm understanding that right...

The problem ends up mostly being how do you get the temperature data to the arduino? Do you have a sensor in mind? The 'proper' solution I usually see is 4-5 embedded sensor nodes communicating with each other and/or a base station, so basically using multiple arduinos and wifi shields but that is getting expensive and complicated. Getting the physically spread out sensor data back to the single arduino may also be a pain in the rear end.

TVarmy
Sep 11, 2011

like food and water, my posting has no intrinsic value

SquadronROE posted:

I talked with a few people about Arduino/Raspberry Pi/Beaglebone yesterday, and finally got some good ideas for projects that I could use the arduino for. Looking forward to going down this rabbit hole, but I'm really not sure what exact board I should start with (more than likely the Uno).

Basically the idea I want to pursue is using the Arduino as a way to take temperature samples in a house (multiple rooms), average them and display them on a very simple website. I could use this to see how effective heating/AC is overall throughout the day.

Would the Uno let me plug 4-5 temperature sensors and a wi-fi board into it, and store temperature from 5 rooms taken at 30 minute intervals throughout the day? That's only something like 3600 samples, so maybe 7 kilobytes of data each day for just storing the text?

I think I'm understanding that right...

Temperature sensors would be pretty easy. There's a lot of ways to do that, with ones that work via serial, i2c, or analog output, all of which are really easy to read on an arduino. With shift registers, pins are no issue for analog pins, and i2c is designed to work with multiple sensors on a few shared pins.

Wifi is where it gets harder. You could get a yun that has built in wifi, or get a wifi shield, but it's surprisingly expensive because the arduino doesn't have enough processing to handle much networking without another processor to handle it (so all the shields that are any good have another processor for wifi). You might be better off hooking up the arduino to a raspberry pi with a USB wifi dongle, or running the whole thing off a raspberry pi or beaglebone (it also has support for i2c). Or you could plug the arduino into an always-on PC and have it handle the networked stuff. PC-arduino communication is pretty easy.

turn it up TURN ME ON
Mar 19, 2012

In the Grim Darkness of the Future, there is only war.

...and delicious ice cream.

TVarmy posted:

Temperature sensors would be pretty easy. There's a lot of ways to do that, with ones that work via serial, i2c, or analog output, all of which are really easy to read on an arduino. With shift registers, pins are no issue for analog pins, and i2c is designed to work with multiple sensors on a few shared pins.

Wifi is where it gets harder. You could get a yun that has built in wifi, or get a wifi shield, but it's surprisingly expensive because the arduino doesn't have enough processing to handle much networking without another processor to handle it (so all the shields that are any good have another processor for wifi). You might be better off hooking up the arduino to a raspberry pi with a USB wifi dongle, or running the whole thing off a raspberry pi or beaglebone (it also has support for i2c). Or you could plug the arduino into an always-on PC and have it handle the networked stuff. PC-arduino communication is pretty easy.

Hmm. I'm not against the Beaglebone Black or just using a raspberry pi for it if it's a better fit. I was figuring on using some sensors like this:

https://www.sparkfun.com/products/11050

I didn't realize that the wi-fi shield added so much overhead, but I suppose that makes a lot of sense. This is what I was gonna use as the shield, but you're right - that's not cheap.

http://store.arduino.cc/ww/index.php?main_page=product_info&cPath=11_5&products_id=312

Sagebrush
Feb 26, 2012

Delta-Wye posted:

The problem ends up mostly being how do you get the temperature data to the arduino? Do you have a sensor in mind? The 'proper' solution I usually see is 4-5 embedded sensor nodes communicating with each other and/or a base station, so basically using multiple arduinos and wifi shields but that is getting expensive and complicated. Getting the physically spread out sensor data back to the single arduino may also be a pain in the rear end.

I think the proper solution is to get a bunch of those 2wire temperature sensors off eBay for $3 each and punch a series of holes in the walls and run a big spiderweb of cables all over the house, and use a $10 bluetooth module to talk to the nearest computer.

JawnV6
Jul 4, 2004

So hot ...
Anyone know a cheap BLE module? I know a few that get cheap at volume, but for one off hobby projects the lowest single unit prices I see are still in the ~$20 range, $13.95 being the lowest I can actually source.

As much as I'd like to chuck wireless comms onto everything, $20 bucks a pop is too expensive for it. $5 would be a lot more palatable.

Dynamite Dog
Dec 12, 2012

SquadronROE posted:

Hmm. I'm not against the Beaglebone Black or just using a raspberry pi for it if it's a better fit. I was figuring on using some sensors like this:

https://www.sparkfun.com/products/11050

I didn't realize that the wi-fi shield added so much overhead, but I suppose that makes a lot of sense. This is what I was gonna use as the shield, but you're right - that's not cheap.

http://store.arduino.cc/ww/index.php?main_page=product_info&cPath=11_5&products_id=312

I'm actually working on something similar to this with a buddy. What we did was buy the components for a breadboard-able raw Uno-equivalent (ATMega328p, crystal, voltage regulaters, etc), a few TMP36 sensors and some nice wireless units from ebay. I didn't order the wireless boards so I don't have a link handy but they use a TI CC1101 chip and they come with an antenna and pretty good range (we've tested them through two stories so far).

It's a lot cheaper than going with a full Uno and wifi shield but the wireless communication is a bit more difficult (we built off the drivers from elechouse, here).

Salami Surgeon
Jan 21, 2001

Don't close. Don't close.


Nap Ghost
I've been thinking about getting an Arduino to get back into microcontroller programming. They are cheap and easy. And ubiquitous, possibly to a fault. The problem that I have with them is they hold your hand A LOT, and it's a bit of a mystery how inefficient that hand holding becomes. Some of the programs I've seen out there are, in a word, terrible. Is there a best coding practices guide for the Arduino, explaining how the functions and libraries get turned in to machine code?

Sagebrush
Feb 26, 2012

Well, the nice thing about the Arduino environment is that all of the simplified functions like digitalWrite() and millis() are essentially just C macros. You can ignore those functions entirely and write plain AVR C into the Arduino IDE, and it will compile and upload just like any other sketch using the Arduino bootloader. In that case, any reference on AVR programming will give you all the information you need.

Furthermore, the nice thing about the Arduino board is that it can function as an ISP if you want to program standalone chips. I'd suggest picking up a handful of ATMega328 DIPs and maybe some Tiny85 and Tiny2313s (they're all like a dollar or two on Mouser) and looking into ArduinoISP. If you're writing plain C in the Arduino IDE and burning chips through ISP, you're basically just one step away from using a text editor and avr-gcc and avrdude like the pros do.

Alternately, if you don't care about the Arduino stuff at all and just want to program microcontrollers, you could always just buy a handful of ATMega DIPs and an inexpensive USB programmer https://www.sparkfun.com/products/9825 and go nuts with the usual toolchain.

Dr. Despair
Nov 4, 2009


39 perfect posts with each roll.

I'm working on making a water heater (so that I can control the temperatures of my film development chemicals). First stage, the temp sensor, is done.



I've got a little box that will hold a power outlet (to plug an immersion heater or two into), as well as a relay circuit, and the arduino with this temp sensor and an lcd shield (that will also have some buttons on it so I can dial in a desired temp) will bolt onto the outside of the box.

Waiting on the mail for the relay, lcd, and heater, but so far so good.

e. began mocking up the enclosure today too.

Dr. Despair fucked around with this message at 05:04 on Dec 2, 2013

Delta-Wye
Sep 29, 2005
If anyone is interested, Sparkfun is running a pretty decent sale for cyber monday on electronics including a ton of Arduino-centric stuff.

TVarmy
Sep 11, 2011

like food and water, my posting has no intrinsic value

Adafruit also has 15% off everything, and they also are offering free shipping above $75 and some free goodies for large orders, such as a free Raspberry Pi on orders over $250. The freebies aren't groundbreaking deals, but they're nice if you had a big project in mind that needed a lot of components. And 15% off is pretty awesome regardless of the other stuff.

ejstheman
Feb 11, 2004
So, if I've been meaning to get into this hacker stuff for ages and today is a particularly good day to do it, what's a good shopping list for a first-timer on those sites? I already have a few soldering irons with various tips and wattages, but other than that I've got nothing. I am especially interested in making sound-reactive blinky-light things and MIDI-controlled blinky-light things, but I'd also like to have enough simple projects to give myself some early successes and help get over the learning curve. I have a dozen years of amateur experience and a year and a half of pro experience programming in various languages, including C, if that affects the answer. Let's say I have a budget of about $300 for this. Anybody have any thoughts to share in the next two hours and 28 minutes?

Stereotype
Apr 24, 2010

College Slice

ejstheman posted:

So, if I've been meaning to get into this hacker stuff for ages and today is a particularly good day to do it, what's a good shopping list for a first-timer on those sites? I already have a few soldering irons with various tips and wattages, but other than that I've got nothing. I am especially interested in making sound-reactive blinky-light things and MIDI-controlled blinky-light things, but I'd also like to have enough simple projects to give myself some early successes and help get over the learning curve. I have a dozen years of amateur experience and a year and a half of pro experience programming in various languages, including C, if that affects the answer. Let's say I have a budget of about $300 for this. Anybody have any thoughts to share in the next two hours and 28 minutes?

A breadboard and a spool of solid core wire that will fit in it (18 gauge is good), plus a wire stripper that will let you breadboard your ideas. Bread boarding before soldering will save you a ton of headache. A multimeter is good for diagnostics too when things don't act the way you expect.

Besides that you just need whatever your projects require. In this case it sounds like LEDs and a microphone.

This is a good walk through for doing an audio ADC if you want to plug a headphone cable into the system.

ejstheman
Feb 11, 2004
I already have a Radio Shack 22-602; will that be sufficient?

I'm browsing around those two sites now, and I'm really digging the physically tiny microcontrollers like Adafruit's Gemma and Trinket. Are they a good place to start with this, or should I get an Uno for the benefit of lots of other people having the same thing?

I don't have a specific project in mind yet; I kind of want to understand better what's possible before trying to be creative.

Sagebrush
Feb 26, 2012

Theoretical question: are array accesses slower than other variable accesses? If so, by how much?

Basically I need to access the same element of an array multiple times in a function, and I know that the value won't be changing within this function, so I'm wondering if it is faster to assign the value to an int first and then access that variable later on, or just to access the array[value] every time? Or if it makes no difference at all?

ejstheman
Feb 11, 2004

Sagebrush posted:

Theoretical question: are array accesses slower than other variable accesses? If so, by how much?

Basically I need to access the same element of an array multiple times in a function, and I know that the value won't be changing within this function, so I'm wondering if it is faster to assign the value to an int first and then access that variable later on, or just to access the array[value] every time? Or if it makes no difference at all?

Are you accessing the array element with a variable index or a constant index? If it's a constant index and you're using an optimizing compiler, it shouldn't matter. If it's a variable index, it still might not matter, depending on the structure of your program and how smart your compiler is, or it might be somewhat slower. Worst case, it might be a fetch (of the value of the index), optionally a left shift or multiply (depending on the size of the array elements) to convert from number of elements into the array to memory offset into the array, an add (pointer to array plus offset into array), and then another fetch (value from array). The fetches might happen as fast as the arithmetic, if they're cache hits, or if you're using a relatively slow processor and relatively fast RAM. If not, they might be significantly slower.

If it's a performance-critical thing, I would just test it rather than trying to work it out theoretically.

Stereotype
Apr 24, 2010

College Slice

ejstheman posted:

I already have a Radio Shack 22-602; will that be sufficient?

I'm browsing around those two sites now, and I'm really digging the physically tiny microcontrollers like Adafruit's Gemma and Trinket. Are they a good place to start with this, or should I get an Uno for the benefit of lots of other people having the same thing?

I don't have a specific project in mind yet; I kind of want to understand better what's possible before trying to be creative.

It really matters what you're trying to do. Something like an Arduino Nano, or an Uno, might be more up your alley if you want a lot of flexibility. If you only need 3 IO pins the Gemma would be fine. Since you probably don't have a HV programmer you need to be sure whatever you buy has the USB bootloader preloaded. Most of the options have this.

I have an Mega2560 and a Nano. They are both great, though I've never gotten close to using the Mega2560's full resources.

Delta-Wye
Sep 29, 2005

Sagebrush posted:

Theoretical question: are array accesses slower than other variable accesses? If so, by how much?

Basically I need to access the same element of an array multiple times in a function, and I know that the value won't be changing within this function, so I'm wondering if it is faster to assign the value to an int first and then access that variable later on, or just to access the array[value] every time? Or if it makes no difference at all?

If the C is naively converted into assembly, the array access may look something like a pointer added to an offset for every lookup. I can't imagine there are a whole lot of reasonable situations where a compiler wouldn't do exactly as you describe though - why do all the extra work if the value is already loaded in a register?

I'd recommend checking the compiler's output - either by having it dump it out directly like gcc does or disassembling your binary. See what your current code looks like, what storing it in an int ahead of time looks like, and I recommend trying to store the address in a pointer and de-referencing it to see what that looks like.

What platform and compiler?

Adbot
ADBOT LOVES YOU

Bad Munki
Nov 4, 2008

We're all mad here.


If you know the value won't be changing within the function, why not just pass the specific array element by value and call it a day?

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply