Larson scanner coding issue. please help

hi, I am a beginner and I have this code below for Arduino to make Larson scanner and I want to fix it so that it turns the Larson scanner off when I send a letter to the serial monitor and different letter to turn the scanner on ( I don't want to change the whole code because I am using a shift register chip to light 8 leds). In other words, what can I write for case 48 and 49 to do so or maybe functions to start/stop ? please help

the code for Larson scanner is:

int datapin = 2; 
int clockpin = 3;
int latchpin = 4;
byte data = 0;
int i;
int dt = 100; 
char incomingByte = 0;


void setup()

{
  pinMode(datapin, OUTPUT);
  pinMode(clockpin, OUTPUT);  
  pinMode(latchpin, OUTPUT);
   Serial.begin(9600);
    
}

void loop()
{

    
  if (Serial.available() > 0) {   
    incomingByte = Serial.read();
    if (incomingByte) {
      switch (incomingByte)
      {
        case 48:  
          break;
        case 49:   
          break;
        default:
          break;
      }
      Serial.flush();
    }
  }
  
  for(i = 0; i <= 7; i++)
  
  {
    shiftWrite(i, HIGH);   
    delay(dt);      
    shiftWrite(i, LOW); 
  }

  
  for(i = 7; i >= 0; i--)
  
  {
    shiftWrite(i, HIGH);   
    delay(dt);       
    shiftWrite(i, LOW);
  }
  
}


}


void shiftWrite(int desiredPin, boolean desiredState)

{
 
  bitWrite(data,desiredPin,desiredState);
  
  shiftOut(datapin, clockpin, LSBFIRST, data);

  digitalWrite(latchpin, HIGH);
  digitalWrite(latchpin, LOW);
}

Please edit your post to add code tags. Instructions are in the "How to get the best out of the forum" post.

Post a link to a "Larson scanner", so forum members will have some idea what you are talking about.

1 Like

Read the forum guidelines to see how to properly post code and some information on how to get the most from this forum.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

1 Like

I got curious, so, a Larson Scanner seems to be:

According to Wikipedia, the Larson Scanner is named after Glen A. Larson, who produced the Battlestar Galactica and Knight Rider series, and is responsible for introducing scanning red light effects to the sci-fi TV viewing population of the 70s and 80s.

1 Like

I didn't look any further than this, largely because until you do a control-T and use the code tags properly it's well nigh impossible to read:

if (incomingByte);

That ; prematurely ends the if, so you need to lose that.

1 Like

thanks for the reply I have read the "How to get the best out of the forum" and edited my post. thank you

Thank you all for the replies, I have read the "How to get the best out of the forum" and edited my post. I am still stuck though.

Did you make the change suggested by @kangaroodle in reply #5. Did it have any affect?

Look into using a state machine approach.

1 Like

As always, I recommend working on the input (keyboard) and output (LEDs) separately before putting everything together.

...It's not "easy" to merge two programs but it's easier than doing two things at once. And it's not too hard if you understand both programs.

So first, I recommend "hard-coding" incommingByte (initialize it to 48 or 49) and make sure you can make it run or not-run depending on that variable.

If-statements may be better than switch/case but that's up to you.

If there are multiple possibilities switch/case may be better.

If there are only two possibilities you don't might not need to check for both... i.e. The input might be 48, or "something else". ...I use some programs at work where the user has to enter "Y" or "N", but there is a default (sometimes yes and sometimes no) and you can just hit Enter for the default or if you hit the wrong key you get the default.

For the input, I've never used keyboard but try starting with the Keyboard Serial Example and then modify it by adding the switch/case or if-statement to simply to turn on & off the built-in LED or maybe "print out" "run" or stop".

Then, when you can read the keyboard and "make a decision" you're ready to put everything together.

Larson Scanner. (I think it was related to KITT's vision or maybe it was the car's heartbeat. :stuck_out_tongue: )

...I've done similar sound activated effects where the speed changes with loudness.

1 Like

would it be something like this ? ( the edited version above, I removed the ; after if)
. Also, since I am using the switch, isn't that the state machine approach ? I just don't know what to write inside the cases to stop the scanner.

Thank you for the reply!

Thank you for sharing the example, I went through it and I feel like I am still confused about the "output part" of the program which is to stop or start the scanner after I send some letter. like if I have an Led and I want to turn it off I would write something like digitalWrite(led, LOW) , but since I am using a shift register I am not sure what would work instead of " digitalWrite(led, LOW) " .

My question is what can I write after case 48: to turn everything off if I send the word stop to the serial monitor.

shiftWrite(led, LOW);

If you want to turn them all off:

  shiftOut(datapin, clockpin, LSBFIRST, 0);
  digitalWrite(latchpin, HIGH);
  digitalWrite(latchpin, LOW);

If you want them to stay off until you turn the display on again, you need a variable to keep track of that. Since there are only two states, a global 'boolean' variable is appropriate.

boolean DisplayIsOn = true;

After you turn off the LED, set DisplayIsOn to false. If the DisplayIsOn variable is false, don't turn on any LEDs. One way to do that is to force shiftWrite() to only write LOW when the display is off:

void shiftWrite(int desiredPin, boolean desiredState)
{
  if (!DisplayIsOn) // the '!' means 'not'
    desiredStaate = LOW;
  bitWrite(data,desiredPin,desiredState);
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.