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
GobiasIndustries
Dec 14, 2007

Lipstick Apathy
What's a good refresher on the electronics side of things for arduino projects (and beyond I suppose, but arduino at minimum)? The kit I bought focuses almost entirely on the coding side of things and even though I was pretty comfortable with circuits and such in college, it's been about 10 years and I'm rusty.

Adbot
ADBOT LOVES YOU

Sagebrush
Feb 26, 2012

A lot of Arduino projects don't need a tremendous amount of electronics knowledge. You just kind of plug data into data, power into power, ground to ground, and maybe stick in some resistors or decoupling caps here and there. Everything else is done in software. If you can build a voltage divider you're good.

If you want the classic electronics introduction, starting with analog circuits and how to build them, get the Forrest Mims III books. Just don't read anything of his that's not about electronics.

First Time Caller
Nov 1, 2004

Idk what people think about it here but Make: Electronics is a phenomenal book IMO. I learned how to program an arduino and make poo poo blink years ago and forgot it all. While Make: Electronics doesnt talk too much about microcontrollers specifically, it does guide you through a ton of electronic theory and fundamentals while being super entertaining and project focused. I highly recommend it.

My latest arduino project:

https://github.com/chrisgillis/nextbusclock

huhu
Feb 24, 2006

First Time Caller posted:

Idk what people think about it here but Make: Electronics is a phenomenal book IMO. I learned how to program an arduino and make poo poo blink years ago and forgot it all. While Make: Electronics doesnt talk too much about microcontrollers specifically, it does guide you through a ton of electronic theory and fundamentals while being super entertaining and project focused. I highly recommend it.

My latest arduino project:

https://github.com/chrisgillis/nextbusclock

All books from Make are 50% off. Just snagged both electronics books.

quote:

Use code "BACKTOSCHOOL16" to get 50% off books through August 31st!!

Skunkduster
Jul 15, 2005




I'm trying to use multiple OR conditions. Is this correct? Something isn't working and I'm trying to narrow down the culprit.

code:
if (MODE == 1 || 2 || 3 || 4)

Seat Safety Switch
May 27, 2008

MY RELIGION IS THE SMALL BLOCK V8 AND COMMANDMENTS ONE THROUGH TEN ARE NEVER LIFT.

Pillbug
2, 3 and 4 will all evaluate to true (since they are not 0), which means that if statement will always be true.

To have it check if MODE is 1, 2, 3 or 4 you should write out the whole statement in each case:

code:
if (MODE == 1 || MODE == 2 || MODE == 3 || MODE == 4)
or better yet:

code:
if (MODE >= 1 && MODE <= 4)
You could also use enums so that each mode has a human-readable name associated with it, instead of trying to remember what 1, 2, 3 and 4 'mean.'

Seat Safety Switch fucked around with this message at 04:18 on Aug 31, 2016

Skunkduster
Jul 15, 2005




Seat Safety Switch posted:

code:
if (MODE == 1 || MODE == 2 || MODE == 3 || MODE == 4)

Thanks much. That clears it up.

Seat Safety Switch posted:

2, 3 and 4 will all evaluate to true (since they are not 0), which means that if statement will always be true.

Why would they all evaluate to be true? I assume in a simpler form, do you mean that

code:
if (2)
would be true? In human terms, what does it mean?

JawnV6
Jul 4, 2004

So hot ...
0 is the only False. Everything else is True.

asdf32
May 15, 2010

I lust for childrens' deaths. Ask me about how I don't care if my kids die.

SkunkDuster posted:

Thanks much. That clears it up.


Why would they all evaluate to be true? I assume in a simpler form, do you mean that

code:
if (2)
would be true? In human terms, what does it mean?

Yes that's true. It's common (and often convenient) in programming languages for zero to be false and anything else is true.

In the background boolean statements like (a==b) evaluate to a 1 or zero. So the If statement is always just checking for a zero or not.

Statement like (a==b)*c (which either returns c or zero) are also possible and often useful.

Fanged Lawn Wormy
Jan 4, 2008

SQUEAK! SQUEAK! SQUEAK!
speaking of enums, that's something I've been trying to sort our about how I want to use. I'm trying to develop my own "standard" of how I declare particular constants.

Granted, I know there are pluses and minuses for any of these, but these are what I'm thinking after trying out varying options on a few different programs:

-Don't use #define unless I am using a #ifdef or something like that later in the program for particular debug information or something like that (increasingly uncommon)

-use const for variables that may be tweaked during the development process (IE, calibration variables like the minimum reading and maximum reading I want a sensor to use). I think I like consts there because then it makes me more aware of when I might accidentally create a rollover situation by changing (I'm less likely to throw a value of 300 if it is a const uint8_t/byte)

-use enum only for human-readable states. Sometimes I've used them for things like Pins and whatnot (i.e., LED, SENSOR) but I'm not entirely confident in it because I'm not entirely sure I fully understand enums as is.


I always name all my constants of any of these in All Caps to make them easier to spot. I also use Visual Micro, so I have some nice syntax highlighting on #defines and enums, but I wish I could find some way to highlight const types because I prefer to use them but I wish they would highlight.

any thoughts re: this being good or bad practice?

Sagebrush
Feb 26, 2012

asdf32 posted:

Yes that's true. It's common (and often convenient) in programming languages for zero to be false and anything else is true.

In the background boolean statements like (a==b) evaluate to a 1 or zero. So the If statement is always just checking for a zero or not.

Statement like (a==b)*c (which either returns c or zero) are also possible and often useful.

Leads to weirdness like this, too:

code:
int a = 5; //value of a is 5
a = !a;  // invert the 5, value of a is 0
a = !a;  // invert the 0, value of a is 1
(led to a problem once with a student's project so I looked it up. That's the defined behavior of ANSI C)

Acid Reflux
Oct 18, 2004

I always seem to be too late to be relevant to the conversation, but I made an ISP on a breadboard with a ZIF socket to program blank 328P chips:



Hooks up to an Uno running the ArduinoISP sketch with just a few wires (there are schematics all over the place for the whole rig) and works like a champ.

asdf32
May 15, 2010

I lust for childrens' deaths. Ask me about how I don't care if my kids die.

Sagebrush posted:

Leads to weirdness like this, too:

code:
int a = 5; //value of a is 5
a = !a;  // invert the 5, value of a is 0
a = !a;  // invert the 0, value of a is 1
(led to a problem once with a student's project so I looked it up. That's the defined behavior of ANSI C)

Weird sometimes but good to know in general. Most math applications support it. In LTSpice for example arbitrary voltage sources let you insert booleans where you couldn't insert an IF statement.

unpacked robinhood
Feb 18, 2013

by Fluffdaddy
I started writing a little screen editor in Processing as a personal project to create graphics for a 5$ 128x64 screen I have. I figure it might interest some people in this thread.

It outputs C instructions for the u8glib library, the tools correspond to some of the primitives found in the library.
The window looks like this


and outputs code like this in the console (when pressing P)
code:
/* u8glib instructions */
u8g_DrawCircle(&u8g,33,21,5);
u8g_DrawCircle(&u8g,35,29,4);
u8g_DrawLine(&u8g,39,20,58,16);
u8g_DrawLine(&u8g,39,28,58,24);
u8g_DrawLine(&u8g,59,24,62,21);
u8g_DrawLine(&u8g,62,20,62,18);
u8g_DrawLine(&u8g,62,18,59,16);
u8g_DrawLine(&u8g,61,19,59,19);
u8g_DrawPixel(&u8g,14,14);
u8g_DrawBox(&u8g,112,11,9,6);
u8g_DrawBox(&u8g,99,8,8,9);
u8g_DrawBox(&u8g,85,16,8,7);
u8g_DrawLine(&u8g,116,6,120,4);
u8g_DrawLine(&u8g,109,61,4,63);
u8g_DrawLine(&u8g,29,20,27,20);
u8g_DrawLine(&u8g,33,31,31,30);
It can also generate library compatible reusable bitmaps by selecting a zone (press O)
for example selecting this zone

outputs
code:
const uint8_t bitmap[] U8G_PROGMEM ={
0x20,
0x28,
0xD8,
0x38, //remove this
};
u8g_drawBitmapP(&u8g, 51 , 22 , 1 , 4,bitmap);

Notes:
- the Z key removes the last action
- disc tool does nothing yet
- the toolbar at the bottom isn't clickable


I haven't tested the generated code yet because I need to install things on my computer to upload to a uC again and
it's sorely missing some basic functions (a dialog for file saving, freehand drawing), the code is also pretty horrific but it seems to be doing the job.

I'm aware there are probably a thousand better tools out there but hey, this one's mine.

I'll probably put in some more work to make it compatible with other screen sizes, sometime in the future. Github here

Rapulum_Dei
Sep 7, 2009

SkunkDuster posted:

I'm trying to use multiple OR conditions. Is this correct? Something isn't working and I'm trying to narrow down the culprit.

code:
if (MODE == 1 || 2 || 3 || 4)

For multiple conditions I would have thought CASE would be the way to do it, is that not right?

Sagebrush
Feb 26, 2012

Using switch/case instead of chained conditionals is equally valid, yeah. I don't know which one is more cycle-efficient -- I haven't really run into a situation where it mattered. I bet they compile into pretty much the same thing.

peepsalot
Apr 24, 2007

        PEEP THIS...
           BITCH!

If you use powers of two for values (this means each value maps to a single, unique bit in the word), then bitwise operators can sort of perform the comparison in the way you originally wrote.

code:
#define FLAG1 0x01
#define FLAG2 0x02
#define FLAG3 0x04
#define FLAG4 0x08
#define FLAG5 0x10
#define FLAG6 0x20
#define FLAG7 0x40
#define FLAG8 0x80


if (MODE & (FLAG1 | FLAG2 | FLAG3 | FLAG4 )) {
    // do stuff here if ANY of the first four flag bits are set in MODE
}
Now your MODE is a bitmap. Each bit maps to a flag and you can enable or disable multiple "flags" to describe a mode. You could also define the flags as enums, where you explicitly set the values to powers of two.

This technique is more useful if you have certain non-mutually-exclusive flags or properties that describe your "mode".

peepsalot fucked around with this message at 20:27 on Sep 2, 2016

GobiasIndustries
Dec 14, 2007

Lipstick Apathy

First Time Caller posted:

Idk what people think about it here but Make: Electronics is a phenomenal book IMO. I learned how to program an arduino and make poo poo blink years ago and forgot it all. While Make: Electronics doesnt talk too much about microcontrollers specifically, it does guide you through a ton of electronic theory and fundamentals while being super entertaining and project focused. I highly recommend it.

My latest arduino project:

https://github.com/chrisgillis/nextbusclock

Just wanted to say thanks for this! I know the Arduino doesn't require a degree in electronics but I like knowing what's going on when I'm playing with the breadboard.

babyeatingpsychopath
Oct 28, 2000
Forum Veteran


Sagebrush posted:

Using switch/case instead of chained conditionals is equally valid, yeah. I don't know which one is more cycle-efficient -- I haven't really run into a situation where it mattered. I bet they compile into pretty much the same thing.

Looking at the generated code, a case is slightly more efficient. The case evaluates into a jump table, and the chained conditionals are all evaluated. You'd have to do some serious testing and actually NEED to find optimization for that to be worthwhile. I like doing things like this:
C code:
switch(mode)
{
case 1:
case 2:
case 3:
case 4:
   some stuff;
   break;
default:
}
When something like this compiles with max optimization, the compiler will do some clever stuff with jump table generation and can unroll cases and defaults, especially if your mode value is an enum.

Skunkduster
Jul 15, 2005




How would the arduino parse this:

(A > B && C < D)

Would it be like

((A > B) && (C < D))

or

(A > (B && C) < D)

or something else?

Poffo
Oct 26, 2005
Rawr!
http://en.cppreference.com/w/cpp/language/operator_precedence

It would be ((A > B) && (C < D)), since the relational operators has higher precedence than the logical AND.

mod sassinator
Dec 13, 2006
I came here to Kick Ass and Chew Bubblegum,
and I'm All out of Ass
In general as a best practice always add parenthesis even when you know the compiler / language order of operations. It will save the next person looking at your code an incredible amount of pain and very likely prevent major bugs. And remember that next person might be you years from now trying to remember what the heck you were doing.

Collateral Damage
Jun 13, 2009

mod sassinator posted:

And remember that next person might be you years from now trying to remember what the heck you were doing.
I've had that several times. Finding some old script or piece of code in production and going "Wait what? But why? What kind of blockheaded idiot wrote this? ... Oh, I did." :downs:

nigga crab pollock
Mar 26, 2010

by Lowtax
i was gifted an arduino uno and i have minimal prior experience with any of this stuff. in high school i built breadboards and programmed 8051s but that was mostly the teacher showing us the motions rather than me actually learning anything and my magnum opus was making it beep 'in a gadda da vida' so thats the level of programming genius we're talkin about here

with an atmega u16 im not really sure what the capabilities of this thing are. i was maybe thinking about building an auxiliary audio recording box but i think that might not be an arduino project and i should just buy one

what do i need to like.. do poo poo? can i just plug a power source and a usb port in, send a program and it will work? hell i don't even know what language or programming suite. please tell me its c and just command line utilities because i dont want to learn an entire sdk

Sagebrush
Feb 26, 2012

Arduino is neither commandline utilities nor an entire SDK. It's a single text editing window with a button at the top that you click to compile and burn your code in one shot. It's super easy.

The Arduino "language" isn't a programming language of its own. It's plain old C/C++, with a big header file that predefines a number of macros and functions to make the process easy for beginners.E.g. instead of flipping port register bits, you can type"digitalWrite(13, HIGH) to set pin 13 high. Because the language is C, though, you can also flip register bits if you like, and mix-and-match code as you see fit.

The boards plug in through USB and usually don't even need a driver. They are powered by the same USB connection, at least until you start doing really high-power stuff like driving big motors or chains of LEDs.

If you like using command-line utilities or a different IDE, you can. Arduino just makes a bunch of calls to avrg++ and avrdude behind the scenes , and Atmel has their own IDE that is quite nice.

Download the software from the Arduino website, plug in your board, and follow the tutorial to upload the first blink sketch. It's pretty simple.

Sagebrush fucked around with this message at 15:58 on Sep 24, 2016

Methanar
Sep 26, 2013

by the sex ghost




Just in case none of you believed me.

Sagebrush
Feb 26, 2012

That's cool. Out of curiosity, can you keep track of how many of them, if any, are dead or damaged? My students often ask me if the SainSmart Arduinos are reliable enough to buy for the class I teach, and so far all I can tell them is the three or four that I've bought have been fine but that the real Arduino is all I can truly recommend. 670 is enough to get some real statistics, though.

Aurium
Oct 10, 2010

nigga crab pollock posted:

i was gifted an arduino uno and i have minimal prior experience with any of this stuff. in high school i built breadboards and programmed 8051s but that was mostly the teacher showing us the motions rather than me actually learning anything and my magnum opus was making it beep 'in a gadda da vida' so thats the level of programming genius we're talkin about here

with an atmega u16 im not really sure what the capabilities of this thing are. i was maybe thinking about building an auxiliary audio recording box but i think that might not be an arduino project and i should just buy one

what do i need to like.. do poo poo? can i just plug a power source and a usb port in, send a program and it will work? hell i don't even know what language or programming suite. please tell me its c and just command line utilities because i dont want to learn an entire sdk

With the uno, the chip you're usually programming is a atmega 328p, not the 16u. The 16u is there to do usb to serial translation, as it has native usb hardware. You can program it which is nice if you want to make it a like a hid(keyboard, mouse, joystick etc) or midi device, or other more esoteric usb things.

People have done sound recording before. The biggest limit is that the onboard memory is very small (2k of eeprom) external flash is kind of low bandwidth, though there are encoders out there that are fast enough to compute, and compress well enough to fit in the bandwidth constraints. Quality isn't so hot. Basically, suitable for easily discernable speech but definitely not musical.

Methanar
Sep 26, 2013

by the sex ghost

Sagebrush posted:

That's cool. Out of curiosity, can you keep track of how many of them, if any, are dead or damaged? My students often ask me if the SainSmart Arduinos are reliable enough to buy for the class I teach, and so far all I can tell them is the three or four that I've bought have been fine but that the real Arduino is all I can truly recommend. 670 is enough to get some real statistics, though.

I found two that I didn't trust during the flashing process. They seemed to be drawing a ridiculous amount of power, 8+ times what they were supposed to be, so I threw those out. Rest all seemed to be fine out of the box. In terms of tracking them: I've got an application written that queries all USB devices connected and reads the serial output. Each of the 48 connected to a server returns a different message: Hello-01, Hello-48, etc. Each device's logical device /dev/usbACM# is then related to the Hello returned to provide abstraction for what actually pushes commands down to the arduinos. If a number between 1-48 doesn't appear then something Really Bad has happened.

unpacked robinhood
Feb 18, 2013

by Fluffdaddy
I've made an u8glib editor utility in Processing to draw screen items faster.

You draw on screen using tools that replicate the library's primitives, it spits C code in the console.
Main features:
  • variable target screen size (default 128x64)
  • image import for tracing
  • library compatible bitmap creation (drawBitmap function)
  • 1:1 preview

Screens:
Basic toolbar


Preview


Sample actual output screens


There doesn't seem to be much interest for this but I'm mostly done and I figure some people would use it.

It's pretty rudimentary and missing basic features but works like I want.

CommieGIR
Aug 22, 2006

The blue glow is a feature, not a bug


Pillbug
Super excited for the ESP32, I've been doing projects in the Arduino IDE and compiling on the ESP8266.

n0tqu1tesane
May 7, 2003

She was rubbing her ass all over my hands. They don't just do that for everyone.
Grimey Drawer
Threw something together for my front porch on Halloween. Going to get either a black light or other colored bulb for the scream light, and I think I've got a paper lantern for the "normal" light to make it look more like my normal porch light. Using a PIR sensor for motion, and the audio is playing directly off a PWM pin on the Arduino.

https://www.youtube.com/watch?v=ccAL11wI_5s

Fanged Lawn Wormy
Jan 4, 2008

SQUEAK! SQUEAK! SQUEAK!

unpacked robinhood posted:

I've made an u8glib editor utility in Processing to draw screen items faster.

You draw on screen using tools that replicate the library's primitives, it spits C code in the console.
Main features:
  • variable target screen size (default 128x64)
  • image import for tracing
  • library compatible bitmap creation (drawBitmap function)
  • 1:1 preview

Screens:
Basic toolbar


Preview


Sample actual output screens


There doesn't seem to be much interest for this but I'm mostly done and I figure some people would use it.

It's pretty rudimentary and missing basic features but works like I want.

This is really cool. I only have used screens a few times, and used the adafruit libraries. This might give me a push to try out u8glib stuff

huhu
Feb 24, 2006
I'm updating my portfolio and I've got some videos saved that I thought you guys might enjoy.
https://www.youtube.com/watch?v=vDLHhMvcVL0

https://www.youtube.com/watch?v=E8Qu_i_7nHc

Hadlock
Nov 9, 2004

unpacked robinhood posted:

I've made an u8glib editor utility in Processing to draw screen items faster.

You draw on screen using tools that replicate the library's primitives, it spits C code in the console.
Main features:
  • variable target screen size (default 128x64)
  • image import for tracing
  • library compatible bitmap creation (drawBitmap function)
  • 1:1 preview

Screens:
Basic toolbar


Preview


Sample actual output screens


There doesn't seem to be much interest for this but I'm mostly done and I figure some people would use it.

It's pretty rudimentary and missing basic features but works like I want.

This is great, Ive used u8glib in several projects in the past, I will be looking at this closely in the future! Thanks sir!

Pollyanna
Mar 5, 2005

Milk's on them.


I haven't done anything interesting with embedded stuff for a while and I'm getting the itch to start messing around again. What are some good project packs/kits to pick up? Adafruit's project packs page doesn't have a whole bunch of interesting things (aside from a motherfucking Moog synthesizer holy poo poo). I've never gotten much further in than trying to get a Raspberry Pi to play nice with my Macbook, and I kinda want to branch out of plain ol' web development programming-wise.

Sagebrush
Feb 26, 2012

I dunno, what kind of stuff do you want to build?

Audio/video/media -> stick with the Raspberry Pi
Bleeping and booping audio synthesis, basic bitmapped graphics -> Arduino OK
Sensors, motors, low-level electronics -> Arduino

Lots of networking, hosting a complex web server, multiple clients -> RPi
Basic networking, building sensor nodes, uploading little bits of data to a server -> Arduino.

What do you already own?

rawrr
Jul 28, 2007
If you want something Arduino like that can connect to the internet, check out the ESP8266 based NodeMcu. It's compatible with the Arduino IDE/libraries and is around $5ish bucks iirc.

Pollyanna
Mar 5, 2005

Milk's on them.


Sagebrush posted:

I dunno, what kind of stuff do you want to build?

Audio/video/media -> stick with the Raspberry Pi
Bleeping and booping audio synthesis, basic bitmapped graphics -> Arduino OK
Sensors, motors, low-level electronics -> Arduino

Lots of networking, hosting a complex web server, multiple clients -> RPi
Basic networking, building sensor nodes, uploading little bits of data to a server -> Arduino.

What do you already own?

I've already got a Raspberry Pi B+ from 2 years ago, and an Arduino...Uno?...that I picked up around the same time.

I'm more into software than hardware by trade, and I was initially drawn to the idea of writing a VM for CHIP-8 using a Raspberry Pi and a couple digital output thingies, but I never got around to it cause I found programming on a Raspberry Pi to be too difficult (didn't know how to install stuff on it, couldn't figure out how to set up a dev environment for Python or Ruby or Go or Rust or C or something, etc.). I kinda want to actually tackle that project again, but I wouldn't know where to start.

Mechanical stuff like servos and sensors sound cool, but I have no idea what I'd use them for. I don't have many problems that can be solved with those things, and what I can think of doing is kinda piddly and dumb:

  • An attachment to my cat's collar to tweet when he meows
  • A sensor that tweets when someone enters my apartment
  • A customizable blinking fridge magnet (magnets are bad tho)
  • Maybe something for Halloween? idk

I have very little experience in networking, concurrency, nodes, etc. and I don't really know of anything I would need to apply that to or why I would do a bunch of networking...work. So that stuff doesn't excite me as much :( Audio synth is certainly interesting, and that one Moog synth sounds fun as poo poo, but it's not quite as much of a "follow-along" DIY project as I'd hoped. I don't really have need of streaming media around my apartment.

Basically, it looks like low-level electronics and trinket-y graphics are where I'm headed? And apparently that's Arduino. Arduino is primarily C though and I have jack-all C experience, much less making a big project in it :(

rawrr posted:

If you want something Arduino like that can connect to the internet, check out the ESP8266 based NodeMcu. It's compatible with the Arduino IDE/libraries and is around $5ish bucks iirc.

I might need to do internet stuff, actually! I'll take a look at that and see if I can figure out what to do with it :)

Adbot
ADBOT LOVES YOU

politicorific
Sep 15, 2007
I pulled a 32 LED display off of a dead 16 port switch. I'd like to turn it into something useful, like a VU meter or CPU/memory meter. The circuit is just 4 AHC164 shift registers (no latch) and the LEDs. I have a general idea of how to make this work, but my solution seems really inelegant.

pre:
        Link       16 15 14 13 12 11 10  9  8  7  6  5  4  3  2  1
Power X Activity   16 15 14 13 12 11 10  9  8  7  6  5  4  3  2  1
Each AHC164 is wired up so data shifts from bottom to top, right to left. For example A1, L1, A2, L2, A3, L3, A4, L4 - then to the next shift register controlling 5-8, then 9-12, then 13-16.
Therefore, if I want to have 2 different types of data, I need to have my values pre-computed and combined before I execute my shift register control code.

pre:
128 32 8 2
 64 16 4 1
A point I need confirmation on:
As I understand it, the DSA & DSB inputs are ANDED together. I'm only using 1 input, so is this why I am having to invert my 0's and 1's in my code to get the LED to light?

Let's say my CPU usage is at 70% and memory usage is at 30%. I want the display to look like this (0's on, X's off):

pre:
            Link XXXXX00000000000
Power 0 Activity XXXXXXXXXXX00000
The lazy way I see to do this is to build a truth table for each variable and add them together and write that out. Each 6.25 percent is equal to an "LED" so that:
code:
if (x >= 93.75 && x <=100)[{myCPU= {170,170,170,170}]; // 170 in the current state turns on  4 top LEDS on
if (x >= 87.5 && x < 93.75)[{myCPU= {234,170,170,170}];
if (x >= 81.25 && x < 87.5)[{myCPU= {250,170,170,170}];
if (x >= 75  && x < 81.25)[{myCPU= {254,170,170,170}];
if (x >= 68.75 && x < 75)[{myCPU= {255,170,170,170}];
...

if (x >= 93.75 && x <=100)[{myMEM= {85,85,85,85}]; //85 tells all 4 bottom LEDs on
if (x >= 87.5 && x < 93.75)[{myMEM= {213,85,85,85}];
if (x >= 81.25 && x < 87.5)[{myMEM= {245,85,85,85}];
if (x >= 75  && x < 81.25)[{myMEM= {253,85,85,85}];
if (x >= 68.75 && x < 75)[{myMEM= {255,85,85,85}];
...
64 16 4 1
There's got to be a better way.

Related question:
How do I make binary counter using an Arduino nano which will count higher than 65,535? Another For loop which increments if j=0?
code:
void loop() {
  for (unsigned int j = 65535; j > 0; j--) {
    blank(); //these blank out the other "unused" leds
    blank();
    shiftOut(ds1, cp, MSBFIRST, highByte(j));   
    shiftOut(ds1, cp, MSBFIRST, lowByte(j));   
    delay(20);
    }
}
Here's my dumb demo code for directly manipulating the ports of my Arduino Nano, I have the clock pin on D6 and datapin on D3... why the hell does this code work this way?
The 16's place should be pin 4, not pin 3
The 128's place should be pin 7, not pin 6
code:
void blank(){
  for (int j = 0; j < 33; j++) { //33 to blank it out totally
  PORTD = 0b00000000;  // clock pin low
  PORTD = 0b00101000;//rising clock high with "off" in this inverted state 
  }
  delay(1000);
}

void setup(){
DDRD = DDRD | 0b11111100;
blank();
}

void loop(){
  for (int j = 0; j < 32; j++) { //clocks in OFF's
  PORTD = 0b00001000; 
  PORTD = 0b01001000; 
  }
  delay(3000);
  for (int j = 0; j < 32; j++) { //clocks in ON's
  PORTD = 0b00000000;
  PORTD = 0b01000000;
  }
  delay(1000);
}
  

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