Categories
Uncategorized

OpenSCAD Local / Modules Variables

I will start with the disclaimer that I’ve never held the title of “Software Developer” or even “Programmer” and my career has been built on being a “Hacker with Many Hats” kind of person. So I tend to go broad and not go deep on a lot of things. I’ll confess, I’ve used a fair amount of global variables in my code, probably some where I should not have. As an old-timer Perl Hacker, it’s… meh. My code was often quick & dirty to get the job done, not elegant and meant to last.

That said, even though I read an entire book on OpenSCAD I somehow had it in my mind that variables in OpenSCAD could not be changed during run-time, as it were. OpenSCAD is… weird, but I’m okay with that. Supposedly variables in OpenSCAD should be thought of as “override-able constants” rather than as variables in the traditional sense.

Also, I’ll admit that often I find examples of code either poorly written or too complex for me to immediately grasp. (I’ll let you decide which is which.) Here, you can read about scope in OpenSCAD User Manual/User-Defined Functions and Modules and OpenSCAD User Manual/General.

Or you can look at my example above… Because yes, you can change the value of a variable in a function / module! Variables are local within a module!

Here’s the demo code shown above. I am guessing 98% of software developers are saying “Duh!” right now… so this post is for me, and people like me, who may have missed this, may have been confused by this, or just needed a simple demo.

Categories
Uncategorized

OpenSCAD Improvements

When writing code for yourself you can choose to leave it messy and confusing (though you may not want to) and when writing code that other people will see it may be a good idea to attempt to be clear and organized. I’m trying to be more clear an organized. Which you know, can help your Future Self as well.

Here’s a recent project. It’s a 3D model of a simple foot switch. There are two 3D printed parts (a base and a cover) and one non-printed part (a push button) and sometimes I want to see one part, and sometimes I want to see all of the parts together.

Another thing I’m hoping this helps with is that since you need to export each part individually for slicing and printing I used to just add a line that said “Uncomment this to export” above each item to be exported.

I figure this is a bit cleaner as you just need to toggle some values between 0 and 1. (And yes, you can do an export that is all parts laid out ready to print but I tend to not do that.)

Anyway, I’ve never really read up on best practices for writing OpenSCAD code. I’ve picked up things over the years from looking at code others have written, and from making my own mistakes and wanting to improve them.

If you know of any good tricks or have some tips, let me know!

Update: Someone asked why I was using 0 and 1 instead of false and true. I am not an expert on this but I believe since OpenSCAD is not strongly typed that 0 and 1 pretty much work as Boolean values and the only real difference is readability. If so, it’s really just a personal choice. (Let me know if that is incorrect.)

Categories
Uncategorized

Arduino – Random Colors

Colors

One of my PCOMP students wants to randomly select a set of RGB values from a predetermined list. It’s the sort of things where I know the concept is easy but the execution is a bit more difficult only because I’ve done it in other languages/environments, but not in an Arduino sketch.

The nice thing about programming is that once you know how to do something using one language, the concepts transfer over to other languages, and it becomes mostly a matter of figuring out the syntax and method to make it all work.

Here’s an example sketch that allows you to have a list (array) of RGB values and then randomly select one and return it, split the r, g, b into their own integer variables, and then print them to the serial monitor. (The final version will use analogWrite to control RGB LEDs.)

// RandomColor.ino

void setup() {
  Serial.begin(9600);
  randomSeed(analogRead(A0));
}

void loop() {
  char* rgb = returnColors();
  int r, g, b;
  if (sscanf(rgb, "%d,%d,%d", &r, &g, &b) == 3) {
    Serial.print(r);
    Serial.print(", ");
    Serial.print(g);
    Serial.print(", ");
    Serial.println(b);
  }
  delay(500);
}

char* returnColors() {
  char* myColors[6];
  myColors[0] = "128,0,0";
  myColors[1] = "0,128,0";
  myColors[2] = "0,0,128";
  myColors[3] = "255,0,0";
  myColors[4] = "0,255,0";
  myColors[5] = "0,0,255";
  int colorIndex = random(0,6);
  char* result = myColors[colorIndex];
  return (result);
}

Obviously the list can be much larger than just six elements, but this is example code to be expanded upon.

You can also grab this code from github.

Update: Royce has his own version you can check out. He suggests it should be more efficient because the textual RGB values are converted to binary at compile time rather than runtime.

Categories
Uncategorized

Learning to Code

Copying and Pasting

I saw many friends share this amusing image of a fake O’Reilly book titled “Copying and Pasting from Stack Overflow”, and if you’re not in on the joke, Stack Overflow is a web site where you can post programming questions and get answers to those questions. It’s also a site (like many others) where people who are not sure what they are doing grab bits of code from when they are not 100% sure what they are doing. (And if you don’t know about O’Reilly, they publish technical books, many about programming.)

This is the first semester I’ve taught a class that includes programming. I was upfront with students about how I’ve learned to write code over the years, and that includes looking at examples, using existing code, and (eventually) reusing my own code. I didn’t force them to type out each line of an Arduino sketch because I didn’t think it was completely necessary. They already know how to type, and once I showed them how to hit the compile button, they learned about syntax errors. Occasionally in class I’d demo things by writing code on the fly and inviting them to type up what I just typed, and to play around with the values, add more lines, etc. (I showed them the “Examples” menu early on, which contains plenty of useful code to get started with.)

Here’s something that may be news to some readers – I’m not a great programmer. I’m a hacker, and I can write enough code to get by, but more importantly I can find enough sample code and examples and tutorials to write the code that I need to do what I want to do. I think that’s the key to things.

If you’ve ever heard the expression “To be a great writer, you need to be a great reader” then perhaps this also applies to programming, and reading code is an important part of writing code.

Now, with all that said, I still expect students to learn some of the basics of writing code and understanding it. If at the end of the semester all they’ve done is copied & pasted code that someone else wrote, and by some miracle it compiles and runs and works for their project, that’s still sort of good, but not quite as good as having a basic understanding of what exactly those lines of code are doing.

In the meantime, keep copying code, and pasting code, and while you’re at it, try to read it and understand it.