# [OpenSCAD] Incrementing a variable doesn't work as expected.

nigeljohnson73 nigel at nigeljohnson.net
Wed Apr 1 03:14:26 EDT 2015

```I'm trying to do some morse code, but I'm struggling to work out the start
position of the 'dit' and the 'dah'... I think first, the code...

id=1;wt=1; // for a ring, don't worry about these
lu = 1; // letter unit. a dit is 1 unit wide, a dah is 3, and the gap
between them is 1.
module drawChar(s) {
echo("char: ", s);
pos = 0;
for(i = [0:len(s)-1]) {
inc = 0;
if(s[i] == ".") {
echo(s[i], "dit ",pos," ", inc);
translate([pos+lu/2,0,0]) cylinder(r=lu/2, h = (id+wt+2)/2);
inc = (1 + 1)*lu;
} else if(s[i] == "-") {
echo(s[i], "dah ",pos," ", inc);
translate([pos,-lu/2,0]) cube([3*lu,lu,(id+wt+2)/2]);
inc = (3+1)*lu;
}
pos = pos + inc; // the inc seems to have gone out of scope
}
}
drawChar("--.-"); // this is a 'q'

This is the output:

ECHO: "char: ", "--.-"
ECHO: "-", "dah ", 0, " ", 4
ECHO: "-", "dah ", 0, " ", 4
ECHO: ".", "dit ", 0, " ", 2
ECHO: "-", "dah ", 0, " ", 4

I have tried many ways to calculate 'pos' - the starting point of the
dit/dah. So the bit that's getting me is the way assignments seem to work -
basically pos always stays zero. Also at the point of echoing inc, I'm
expecting it to still be zero.

2 questions I suppose, a learning point for me, what's going on? and second,
how do I make it do what I want it to do?

I was thinking of some way to count the occurances of '.' and '-' in the
array as a function multiplying accordingly, but my brain melted a little -
it is early.

Any help much appreciated
Nigel

--
View this message in context: http://forum.openscad.org/Incrementing-a-variable-doesn-t-work-as-expected-tp12271.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

```