How would I read strings to turn the project off and on?

I am working on a project using an Arduino uno + 8 LEDs and a chip to make a Larson scanner. I have the code for that below, how would I add or fix this code so that I would turn the project on and off by typing something in the serial monitor. please help. Thanks

the code:

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

void setup()

{
pinMode(datapin, OUTPUT);
pinMode(clockpin, OUTPUT);
pinMode(latchpin, OUTPUT);
}

void loop()
{

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);
}

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

void setup()

{
pinMode(datapin, OUTPUT);
pinMode(clockpin, OUTPUT);
pinMode(latchpin, OUTPUT);
}

void loop()
{

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);
}





1 Like

Take a look at the Serial Input Basics tutorial on the forum: Serial Input Basics - updated

1 Like

This. Learn it, copy it, study it and thank Robin2 after everything is working. I pulled my hair out for eons trying to make what you're describing work reliably. I found that simply toggling a project on and off, the best way is to stick with using two chars to get the job done, KISS. I don't know what input you hoped to use, but I can say I tried, for HOURS, nay DAYS to get it right (I'm just a hobbyist with no formal computer training or experience) to get more than single char toggle inputs working reliably in my spaghetti code and if it worked, it didn't work reliably.
This is my PSA to the world of non professional Arduino users like me after almost 10 years now playing around with these things: Robin2 is wise. Take his example code for serial comms and copy paste it in your sketchbook for convenient reference. All hail Robin2, seriously.

my suggestion for future programming:
divide your code into functions where each function does one single thing.
Test the function for all possible (and all IMpossible - I'm serious! ) circumstances.
only if it works reliable add next function that does one single thing.

You can invest time in testing short functions and be finished after a defined time or you can invest time in guessings changing this, changing that for a non-defined amount of time.

best regards Stefan

Thanks for the tip! In fact, I do that already since troubleshooting anything of any degree of complexity becomes a major headache if I don't build off function prototypes if that's what they're called. For the OP's question about Serial comms, my tip is just to keep the comms as simple as possible to get the job done, avoiding strings entirely if it doesn't interfere with the integrity of your project. If strings are a must, I have used with good success a password library such as this New Password Library
Most times you might get away with a single char though. Probably saves memory, too.
Cheers!

1 Like

Taking the idea far beyond on/off, imagine implementing your own "verbs" to have the Arduino perform your every wish:

Another command could be constructed for Digital Pin Write, DPW, and another for Analog Pin Read, APR. As you can see from the snipplets of code below, the implementation is very easy. While manipulating a remote uC’s state over RS232 may seem completely insane, not every design requires near instanteous control; sometimes, the external signals only need to be monitored occasionally and RS232 is a nearly ‘free’ resource in the Arduino world.

void setup()
  pinMode(3, INPUT);      //  sets the digital pin 3 as input
  pinMode(6, INPUT);      //  sets the digital pin 6 as input
  pinMode(7, INPUT);      //  sets the digital pin 7 as input
  pinMode(8, INPUT);      //  sets the digital pin 8 as input
  pinMode(9, INPUT);      //  sets the digital pin 9 as input
  pinMode(10, INPUT);      // sets the digital pin 10 as input
  pinMode(11, INPUT);      // sets the digital pin 11 as input
  pinMode(12, INPUT);      // sets the digital pin 12 as input
...
    case 22:    //DPR DigitalPin Read # valid 2 - 13  Return 0, 1 for state or 2 for Error
            if (verbose) {Serial.print(F("Prompting for Digital Pin Number 2 - 13: ")); }
            DigPinNo = Serial.parseInt(); 
            if (DigPinNo <2 || DigPinNo > 13) {
              if (verbose) { Serial.print(DigPinNo);
                Serial.print(" Pin# Error"); break; }
                Serial << Err; break; }
            if (verbose) { Serial.print(DigPinNo); }
            if (verbose) {Serial.print(F(" Logic State = ")); }
            Serial << digitalRead(DigPinNo);
            if (verbose) {Serial.println(); }
            break;
1 Like

Thanks for all the replies but I am still confused and not sure how to add something to my code so that I can press some key on the keyboard in the serial monitor to turn the leds off then send something else to turn the larson scanner on (or whatever was going on).

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

void setup()

{
  pinMode(datapin, OUTPUT);
  pinMode(clockpin, OUTPUT);
  pinMode(latchpin, OUTPUT);
}

void loop() {
  if (Serial.available() > 0) {    // read the incoming byte
    incomingByte = Serial.read();
    if (incomingByte); {
      switch (incomingByte)
      {
        case 48:  // numeric 0
          // call OFF code
          break;
        case 49:   // numeric 1
          // call ON code
          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);
}

YOU need to create 2 functions for ON and OFF.

        case 48:  // numeric 0
          // call OFF code
          break;
        case 49:   // numeric 1
          // call ON code

Just curious… did you take the link @jremington provided in #3 above?

What part of that did you have trouble with?

a7

1 Like

@mrburnette

your code puts all lines inside function loop. Your code uses variants for analysing the incoming byte in a hard to understand way

below is a version of this code that

  • makes consequent use of functions with self-explaining names
  • with very intensive commenting what the lines of code do

My hope is that this version makes it easier to understand for @alkaothm

as a general advice @alkaothm:
first of all it is completely OK to be a beginner. Everyone here was once a total beginner
(at least the few seconds after leaving his mother's body ) .

Your questions show that you are a beginner. I recommend to work through this tutorial
This will take some extra time. But it will save you dozens hours of time because you really understand the basics.

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

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

void setup() {
  pinMode(datapin, OUTPUT);
  pinMode(clockpin, OUTPUT);
  pinMode(latchpin, OUTPUT);
}

char myCommand;

void loop() {
  // lookup the receive-buffer if a byte was received return this byte
  // otherwise return value zero
  myCommand = readReceiveBuffer(); 

  executeReceivedCommand(myCommand);

  if (myCommand != 0) { // whenever variable named "myCommand" has a value different than zero
    switchOnOffLEDs();  // execute lines of code defined in function with name "switchOnOffLEDs()"
  }
}


char readReceiveBuffer() {
  char incomingByte = 0; // initialise variable with name "incomingByte" with zero

  // if there is a byte in the receive-buffer the function Serial.available()
  // the function available gives back a value bigger than zero
  if (Serial.available() > 0) {
    // if there is a byte in the receive-buffer
    incomingByte = Serial.read(); // read this byte and store it in the variable with name "incomingByte"

    Serial.flush(); // clear serial receive-buffer
    return incomingByte; // hand-over the content of variable named "incomingByte" to variable named "myCommand"
  }
}


void executeReceivedCommand(char p_command) {

  switch (p_command) { // depending on the value stored inside variable named "p_command"

    case '0':  // in case the char inside variable named "p_command" is a 0
      // call OFF code
      break; // immitiately jump to END OF SWITCH

    case '1':   // in case the char inside variable named "p_command" is a 1
      // call ON code
      break; // immitiately jump to END OF SWITCH

    default:
      break; // immitiately jump to END OF SWITCH
  } // END OF SWITCH
}



void switchOnOffLEDs() {
  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);
}

best regards Stefan

1 Like

yes I went through it. It is the part where I send a character to the serial monitor but my question is how would I create a function that would turn the scanner off when I send something to the serial monitor send a letter for example.

Thank you vey much Stefan!, I really appreciate it. Yes I am a beginner and I will for sure study the course and let you know after I finish it.

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