"Translating" from Basic Stamp to Arduino language

Hi guys, I am working on some lab manuals converting from Basic Stamp to Arduino. I got stuck with this code. It is simple, but I am not understanding how to write this in Arduino language. This is supposed to work with a seven segment display. I am not sure what exactly it does because I don't have the Basic Stamps anymore. Can you please help me "translate"? Thank you

‘Program to Test Looped Display
‘Put your name here
‘Put the date here

counter var nib             ‘defines the variable counter as a 4 bit variable
OUTA=$0     ‘sets the lower 4 bits in the ouput register to b0000, i.e., makes sure P0-P3 will go low when DIR is set to output
DIRA=$F ‘sets the lower 4 bits in the DIRS register to b1111, i.e., makes pins P0-P3 be outputs, so values in OUTA show up there. Should see ‘0’ on display now
loop:
FOR counter=0 to 9
OUTA=counter
pause 1000
NEXT
goto loop

OUTA and DIRS lines look like they're the awful awkward basic-stamp version of digitalWrite and pinMode (well, more like direct port manipulation )

I'm pretty sure the code you posted doesn't actually work on BASIC... At least not with normal "dumb" 7-segment LED displays; If I'm reading it right, it's going to output the binary values for 0~9 on a set of 4 pins. But to drive a 7-segment display, you normally have to control, uh, well, 7 segments. For that to work, I think you'd need some sort of "smart" 7-segment display that takes the binary value of the digit instead of having you control the segments.

If you google around, you will find oodles of resources for 7-segment displays on Arduino.

Looks like its Basic code for driving a 4511 BCD to 7 segment driver IC.

Cant see why you would want to use an Arduino to drive a 7 segment display via a 4511, just as easy to drive the 7 segment display direct.

Based on DrAzzy’s suggestion; note that this uses the lower nibble port B.

void setup()
{
  // pins 8..11 output
  DDRB = 0x0F;
}

void loop()
{
  for (int counter=0; counter <9; counter++)
  {
    // write the value
    PORTB = counter;
    delay(1000);
  }
}

Not perfect as it affects the other pins on port B as well. But it can get you stsrted. Not tested or compiled.

See Arduino - PortManipulation and the datasheet for more information.

DrAzzy, I am sorry, it does use a 7447 decoder.

DrAzzy and srnet, I need to use the same equipment and I need the output of the code to work in the same exact way, since this is for students in a lab.

sterretje, I will give that a try. I was not familiar with the way ports work in arduino. Thank you.

sterretje, that worked great! Thank you.

One more question: so this code puts a 1, in the 7 segment display, if some buttons are pressed, and 0 if not. Is that correct?

‘Program for a Switch Controlled Display Circuit
‘Put your name here
‘Put the date here
OUTA=$0     ‘sets the lower 4 bits in the ouput register to b0000, i.e., makes sure P0-P3 will go low when DIR is set to output
DIRC=$0     ‘sets the lower 4 bits of the upper byte in the DIRS register to b0000, i.e., makes P8-P11 inputs
DIRA=$F     ‘sets the lower 4 bits in the DIRS register to b1111, i.e., makes pins P0-P3 be outputs, so values in OUTA show up there. Should see ‘0’ on display
loop:
IF IN8=1 AND IN9=1 THEN zero
IF NOT IN8=1 AND IN9=1 THEN one
OUTA=2
goto loop 
one:
OUTA=1
goto loop 
zero:
OUTA=0
goto loop

I am working on some lab manuals converting from Basic Stamp to Arduino. : I don't have the Basic Stamps anymore.

It might be worth getting a Basic Stamp. https://www.parallax.com/catalog/microcontrollers/basic-stamp They aren't so much obsolete, overpriced, and unobtainable as just ... overshadowed.

so this code puts a 1, in the 7 segment display, if some buttons are pressed, and 0 if not. Is that correct?

That's what it looks like to me. Although probably the input pins use pullups, so "1" means they are NOT pressed.

I remember BASIC without "else", and when all you could do from the "if" was goto a lable... Shudder. That's the sort of thing that have BASIC such a bad name! On Arduino, you'd end up with something like:

void loop() {
  if (digitalRead(9) && digitalRead(8)) {
    display7(0);
  } else {
    display7(1);
  }
}

(display7() would be a function you'd written that writes the binary value to the pins, either using direct IO like sterretje's example, or using some obnoxious-looking loop with multiple digitalWrite() statements (which would be more portable to other other arduino variants, or other pin-use choices.)

There is no such thing as "Arduino language". Arduinos are programmed in c/c++, arguably the most ubiquitous language on the planet. Read ANY c/c++ tutorial.

Regards, Ray L.

If anyone's interested - I have a few Basic Stamp-2 (BS2) 'chips' in my parts box. I'm in Australia - so getting them is your problem. Otherwise FREE

((Curiously enough - they were leftovers from a mains power-control system - for the Wheel Of Fortune letter board! Later I moved to a naked PIC solution.))

Read ANY c/c++ tutorial.

“Any C/C++ tutorial” is likely to be pretty useless when it comes to the details of porting old BASIC-stamp code to an Arduino. You need the translation from STAMP Basic to Arduino library functions (if available. Otherwise to some combination of C, Arduino Functions, and bare AVR coding.)