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
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.
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.
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. )
...I've done similar sound activated effects where the speed changes with loudness.
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 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) " .
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);