Jump to content

Anyone here good with C?

Recommended Posts

As in the language?


I'm playing with an Arduino and it basically is C.


I am wanting to write some code for an HSI.


But I'm stuck on how to do it.


For now - and probably always - there is a ring of 12 LEDs. Each addressable and colour mappable.


At normal (S&L) two leds are on. 3 and 9. (think of a clock)


Climbing is like 4 and 8, etc


Descending is 2 and 10, etc.


But with only 12 LED's the resolution is a bit poor. 30 degrees / LED.


As the brightness is adjustable, I can "Bleed" the colours, but can't get my head around how to do that.


I'll upload what I have so far soon. At work now so am limited in time and don't have my notes with me.



Link to comment
Share on other sites

Here is the initial code:


Sorry if it is messed up. I have been trying for a while to upload and not having any luck.


// HSI sketch to display Horizontal Attitude on an LED ring.


int ZVO = 3;


int OS = 0;


int LED_Loop = 12;


int QL = LED_Loop/4;


void setup()




// put your setup code here, to run once:




void loop()




// put your main code here, to run repeatedly:










// end of code








This is the caculator to display the correct LEDs on the display.




You tell it how many LEDs are in the circle, and the "offset"


number which is how far from the 12 o'clock position the first


LED is.


This rouine does the rest.


REQUIRED: Number of LEDs in loop to be defined as LED_Loop


LED_Loop / 4 as QL


An OFFSET value to be set as OS


A Zero Value Offset so when level this is the LED to use (ZVO)


All values are counted CLOCKWISE aound the circle.




// Called with X and Y values


// Returns LED values




int HSI(int x,int y)






// Basic Pitch


int _A1 = (x / (90/QL)) + ZVO;


int _A2 = LED_Loop - _A1;


// Basic Roll


int _B1 = (y / (90/QL)) + ZVO;


int _B2 = (_B1+QL+QL);










// end of code







Link to comment
Share on other sites

The variables:


ZVO is to "offset" when on a flat surface.


So when "x" is saying level, it offsets to +3 and so LED 3 is used.


(9 is on the opposite side. Easily enough calculated)


Sorry for the slack way I have done it.


I am still at the "starting out" phase of writing the program.


There is a bit more added in later where _A1 and _B1 are averaged together to give the result.


As with the other two.


So basically it is something like:


int _C1 = (average(_A1+_B1);


int _C2 = (average(_A2+ _B2);


And _C1 and _C2 are the two leds to use.


But as you can see that is 30 degrees / LED accuracy. Which is pretty bad.


I want to work out how to "include" extra LED's so at (say) 15 degrees climb:


LEDs 3,4 9 and 8 are on.


I hope you get my idea.


Hear back from you soon I hope.



Link to comment
Share on other sites

You will have to come up with a calculation that turns your error output or angle output to a reference that can then be used to operate a bit on the port or you could do it a easier way just use a lm3914 barograph display driver it only needs a voltage input on a scale it can be calibrated as such...the output of your gyro sensor can go into the adc then you can use a pam to drive the lm3914 for a easier display



Link to comment
Share on other sites

Adafruit are a real push in the homebrew/kit electronics market...They have a heap of Eagle libraries I use for my pcb stuff. Now I see you display it is individually addressed and colour RGB must be some sort of I2C bus. Now it would be a trick to display what you want using this led module as it needs to be "decoded" with your software to be able to drive the appropriate display... Bit out of my level of programming....I am much better with hardware than the software although I can usually get stuff working ok. Got a lift controller working at my farm it is run with arduino and xbee units. The lift carriage up and down is wireless operation the upstairs and downstairs controls are wired to the main arduino master control. Lift carriage has a gate interlock etc. This was taken when I first got it going..it is used all the time just needs a bit of tidying up which happens from time to time when we get up there



Link to comment
Share on other sites

Thanks Kyle.


Yeah, well I am using an Arduino to do the "number crunching" and driving the "display".


I am just playing with that ring of LEDs as a good looking "display".


I am making an "analogue clock" using the 60 LED ring.


It is just I am not fluent enough with "cheating" with numbers getting INT() values, remainders and all that.


So the question still stands.


Can anyone help me with how to get the answer on how to resolve the X and Y attitudes to be displayed on a ring of LEDs with only 12 LEDs in the circle. Coz alas 30 degrees / LED isn't good resolution.



Link to comment
Share on other sites

I don't have one of these displays to test, and I'm no expert...


But give this a try - https://www.dropbox.com/sh/4q2yqq3pxmvr0vc/AAAWd3eWNc_gODwGMo0BF-WEa


The Library gives a function to call with Pixel number and Colour,


So basically it's just dividing the Pitch and Roll values by 15Deg to get a 0 -11 range for the 15Deg steps,


Then if they fall between Pixels (an odd number) then light the Pixel either side at reduced brightness


If the Pitch and Roll give an even number then just light that one Pixel at full brightness on each side.


You'll have to add your code to get the Pitch and Roll values. (this sketch assumes degrees output, change for radians)


This should be easy to change for different colours, or to vary the colour, or to change the step size.


Have fun!





Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...