Arduino UserInput, with LEDs

Hello!

Im working on an Arduino UNO project that will take a string from user input (maximum 20 characters), (also to be a single word or number at once, with no space or punctuation).

Then print out the string but each character or number is to be printed individually on a separate line.

Also, turn on one LED at a time corresponding to the letter or number. With only using a 10 LED bar.

All LEDs are to be turned on to represent zero.

FOR EXAMPLE:
Number "1918" has been inputted,
1 (LED nr. 1 has been turned on) [delay(1500)] then..
9 (LED nr. 9 has been turned on) [delay(1500)] then..
1 (LED nr. 1 has been turned on) [delay(1500)] then..
8 (LED nr. 8 has been turned on)

Then Ask User Input For Another String..

FOR EXAMPLE:
String "cat" has been inputted,
"c" (is the 3rd letter in the alphabet) and so (LED nr. 3 has been turned on) [delay(1500)] then..

"a" (is the 1st letter in the alphabet) and so (LED nr. 1 has been turned on) [delay(1500)] then..

"t" (is the 20th letter in the alphabet) and so (LED nr. 2 has been turned on, [delay(500)] then all LEDs turn on to represent the 0)

Then Ask User Input For Another String and so on...

The code I have so far:

int delayChoice = 1500;
int delayPause = 500;

char userInput[20];
int i;

int pin3 = 3;
int pin4 = 4;
int pin5 = 5;
int pin6 = 6;
int pin7 = 7;
int pin8 = 8;
int pin9 = 9;
int pin10 = 10;
int pin11 = 11;
int pin12 = 12;

String askStr = "Enter a word or number from 20 Characters: "; // String object
String num; // String object

void setup()
{
Serial.begin(9600);

pinMode(pin3,OUTPUT);
pinMode(pin4,OUTPUT);
pinMode(pin5,OUTPUT);
pinMode(pin6,OUTPUT);
pinMode(pin7,OUTPUT);
pinMode(pin8,OUTPUT);
pinMode(pin9,OUTPUT);
pinMode(pin10,OUTPUT);
pinMode(pin11,OUTPUT);
pinMode(pin12,OUTPUT);
}

void loop()
{
#include <string.h>

Serial.println(askStr);
while (Serial.available() == 0)
{
}

if(Serial.available() > 0)
{
Serial.readBytesUntil(' ', userInput, 20); //Read 20 characters from Serial into userInput
for(i = 0; i < 20; i++) //Loop 20 times
{
if(userInput[i] == '\0') //If the character at i is the null character
{
break; //Break out of the loop
}

  else //If the character is not the null character 
  { 
    Serial.println(userInput[i]), digitalWrite(pin5, HIGH); //Print the character 
  }
}

}

delay(delayChoice);
}

Hello andreighet

Consider this small example sketch using C++.

// https://forum.arduino.cc/t/arduino-userinput-with-leds/1057844
constexpr byte Leds[] {2,3,4,5,6,7,8,9,10,11};
void setup() 
{
  Serial.begin(9600);
  for (auto Led:Leds) pinMode(Led,OUTPUT);
}
void loop() 
{
  if (Serial.available()!=0)
  {
    char inChar=Serial.read();
    switch(inChar)
    {
      case '0' ... '9':
      digitalWrite(Leds[inChar-'0'],HIGH); 
      break;
      case 'a' ... 'j':
      digitalWrite(Leds[inChar-'a'],HIGH); 
      break;
      case 'A' ... 'J':
      digitalWrite(Leds[inChar-'A'],HIGH); 
      break;
    }
    delay(1500); 
    for (auto Led:Leds) digitalWrite(Led,LOW); 
  }
}

BTW:

This is an exelant source how to learn coding in C++.

https://www.learncpp.com/

2 Likes

Hi @andreighet
welcome to the Arduino-forum.
@Coding_badly:

deleted as requested

1 Like

there are several issues with your code which is why @paulpaulson suggested a simpler and cleaner approach, and seeing well written code is the best way to learn

  • as paul suggested, arrays can be used to manage the LED pins
  • no need to include string.h on arduino because a .ino is processed by a java script to make it easier for newbies
  • unlike a terminal, the prompt string is in a different space and doesn't really make sense
  • there's no need for a while (Serial.available() just before an if (Serial.available()
  • i might have started out using readBytesUntil() but paul's approach doing just a read is simpler
  • you expect the terminaing character for readBytesUntil() to be a space, ' '. why not a linefeed, '\n'?
  • looks like you never attempted to turn on anything other than pin5
  • i thought you wanted a delay between the processing of each character, not at the end of the line

in your example, "1918" turns LED 1 on twice and a i see no way to turn an LED off. maybe you want to toggle each LED, on/off.

another approach for translating an ASCII character to an "index" is to subtract '0', 'a' or 'A' from the char value. (you can check for a valid index before toggling the LED

not sure why you want digits, lower and upper case letters to be commands to turn on LEDs. you could use letters for various amounts of delay

hopefully this is a good learning experience

1 Like

Hello!

Only reason why im using LED's is because later I would like to replace the LEDs with a set of 2 X 3 of solenoids. For this to print out the numbers and characters from a string to form braile language.

Hello andreighet

To do do you can extend the C++ sketch very easy and simple.

Enjoy coding in C++ and have fun.

1 Like

@StefanL38
No need to exaggerate the vulnerability of newcomers
Even incomprehensible, but beautiful code can be an incentive to learn. In my opinion, there is no need to simplify things more than necessary. By doing this, you are doing newcomers a false favor. There is nothing complicated about arrays, and the faster a person learns to use them, the easier it will be for him to understand other people's projects and code.

Besides, I think your speeches are simply impolite. Instead of writing three dozen lines of remarks to @paulpaulson , help the newbie yourself. If you don't like the code that Paul wrote - write your own, simpler and more beautiful.

1 Like

Hello andreighet

Do you can provide a data sheet of set of 2 X 3 of solenoids.

Hello andreighet

Consider this small propsal for one set of solenoids.
For test purposes the characters SPACE to # have been coded.
You have to extent the Text2Braille array for all letters needed.

// https://forum.arduino.cc/t/arduino-userinput-with-leds/1057844
constexpr byte Leds[] {2, 3, 4, 5, 6, 7};
// https://en.wikipedia.org/wiki/Braille_ASCII

constexpr int Text2Braille[][6]
{
  {0, 0, 0, 0, 0, 0}, // space
  {0, 1, 1, 1, 0, 1}, // !
  {0, 0, 0, 0, 1, 0}, // "
  {0, 0, 1, 1, 1, 1}, // #
};

void setup()
{
  Serial.begin(9600);
  for (auto Led : Leds) pinMode(Led, OUTPUT);
}
void loop()
{
  if (Serial.available() != 0)
  {
    char inChar = Serial.read();
    if (inChar >= ' ' && inChar <= '_')
    {
      for (auto Led : Leds) digitalWrite(Led, LOW);
      int element = 0;
      for (auto Led : Leds) digitalWrite(Led, Text2Braille[inChar - ' '][element++]);
      delay(1500);
    }

  }
}

Enjoy coding in C++ and have fun.

1 Like

So I googled for tutorials about arrays and among others I found this tutorial
https://www.simplilearn.com/tutorials/cpp-tutorial/cpp-array
@Coding_badly:

deleted as requested

Demo-code that explains arrays

// open the serial monitor of the Arduino-IDE adjust baudrate to 115200
// and read the serial output of the code.
// the output shall visualise what the code is doing

// "byte" is the datatype of the array
// "myPhoneDigits" is the name of the array 
// the "4" defines how many elements the array shall have
byte myPhoneDigits[4]; // declaring an array of byte with 4 elements named "myPhoneDigits"

void setup() {
  // make sure to adjust baudrate of the 
  //Arduino-IDE serial monitor to 115200 baud
  Serial.begin(115200); 
  Serial.println( F("Setup-Start") );

  // store values in each array-element
  myPhoneDigits[0] = 4; // element  1 which has index nr 0
  myPhoneDigits[1] = 9; // element  2 which has index nr 1
  myPhoneDigits[2] = 5; // element  3 which has index nr 2
  myPhoneDigits[3] = 1; // element  4 which has index nr 3

  Serial.println();
  Serial.println();
  Serial.println("all phone-digits print as fixed characters");
  Serial.println("Index-Nr   0123");
  Serial.println("the digits 4951");
  Serial.println();
  Serial.println("now printed in a for-loop");

  Serial.print("the digits ");

  for (int i = 0; i < 4; i++) { // count up 0 to 3
    Serial.print(myPhoneDigits[i]); // print content of each array-element
  }
  
  Serial.println();
  Serial.println();
  Serial.println("now printing with explanation");
  Serial.println("each array-element and its content");

  for (int i = 0; i < 4; i++) { // count up 0 to 3
    Serial.print("array-element myPhoneDigits[");
    Serial.print(i); // print index-number
    Serial.print("] holds value ");
    Serial.println(myPhoneDigits[i]); // print content of each array-element
  }
  
  Serial.println();
  Serial.println();
  Serial.println();
}


void loop() {
}

I respect your efforts to make things easier for beginners... but I don't think you have the right to teach others how they should help newbies. This is not acceptable on the forum.... even if you will use "very clear words".
Spend that effort writing new tutorials.... that are better than the old ones

2 Likes

the are some very good books and web pages that an OP can google for an explanation, better than i can

well written code makes the OP aware of what's possible.

1 Like

Tutorials how to write spaghetti code? :nerd_face:

3 Likes

I see there's a significant amount of cross-talking going on. While writing, ask yourself this simple question, is what I'm writing likely to directly help the original poster? If the answer is "no" or "probably not" then stop writing / start erasing. This will be the only warning. Possibly for the rest of time.

Post #2 from @paulpaulson is a fine example. While the original poster may not understand some, most, or even all of the sketch, it does work and it is a good starting point for further discussion. Thank you @paulpaulson.

3 Likes

not sure what you mean. Also still waiting on the solenoids to arrive then I can start building the circuit.

Hello andreighet

You can start simply with the sketch from post #9 to continue.

Hey,
I love your code, it works nicely!! But how do I modify it for it to work with other alphabetical letters and digits?
I attached pic showing what pattern is required for each letter and number.

First you need the Text2Braille array for all letters.

I took the idea of paul and wrote two examples that IMHO makes it easier to understand how to define the braille-"code" for each letter / each number

Here is a first version for learning with a part of the code as hard coded numbers


struct Letter2Braille_type {
  char letter;
  byte brailleDots[6];
} ;

Letter2Braille_type myLetter2BrailleArray[]
{ { .letter = 'A', // first element has index 0
    1, 0,
    0, 0,
    0, 0
  },

  { .letter = 'B', // second element has index 1
    1, 0,
    1, 0,
    0, 0
  },

  { .letter = 'C', // third element has index 2
    1, 1,
    0, 0,
    0, 0
  }

};



void setup() {
  // make sure to adjust baudrate of the
  //Arduino-IDE serial monitor to 115200 baud
  Serial.begin(115200);
  Serial.println( F("Setup-Start") );
  Serial.println();

  // brailledot-numeration
  // 1,4 equals index-No 0 3
  // 2 5 equals index-No 1 4
  // 3 6 equals index-No 2 5

  Serial.print("letter=#");
  Serial.print(myLetter2BrailleArray[0].letter);
  Serial.println("#");
  // first braille-line
  Serial.print(myLetter2BrailleArray[0].brailleDots[0]);
  Serial.println(myLetter2BrailleArray[0].brailleDots[1]);
  
  // second braille-line
  Serial.print(myLetter2BrailleArray[0].brailleDots[2]);
  Serial.println(myLetter2BrailleArray[0].brailleDots[3]);

  // third braille-line  
  Serial.print(myLetter2BrailleArray[0].brailleDots[4]);
  Serial.println(myLetter2BrailleArray[0].brailleDots[5]);

  Serial.println();
  Serial.println();
  Serial.println();
  Serial.println("now printing the same with shorter code using a for-loop");
  Serial.println();

  
  // this can be coded shorter by using a for-loop 
  // that counts up the index-number from 0 to 2
  Serial.print("letter=#");
  Serial.print(myLetter2BrailleArray[0].letter);
  Serial.println("#");
  // letter 'A'      has letter-index-no 0 
  //               myLetter2BrailleArray[0]
  for (byte IndexNr = 0; IndexNr < 6; IndexNr += 2) {
    Serial.println(IndexNr);
    Serial.print  (myLetter2BrailleArray[0].brailleDots[IndexNr]);
    Serial.println(myLetter2BrailleArray[0].brailleDots[IndexNr + 1]);
  }
  Serial.println();

  Serial.print("letter=#");
  Serial.print(myLetter2BrailleArray[1].letter);
  Serial.println("#");
  // letter 'B'      has letter-index-no 1 
  //               myLetter2BrailleArray[1]
  for (byte IndexNr = 0; IndexNr < 3; IndexNr += 2) {
    Serial.print  (myLetter2BrailleArray[1].brailleDots[IndexNr]);
    Serial.println(myLetter2BrailleArray[1].brailleDots[IndexNr + 1]);
  }
  Serial.println();

  Serial.print("letter=#");
  Serial.print(myLetter2BrailleArray[2].letter);
  Serial.println("#");
  // letter 'C'      has letter-index-no 2 
  //               myLetter2BrailleArray[2]
  for (byte IndexNr = 0; IndexNr < 3; IndexNr += 2) {
    Serial.print  (myLetter2BrailleArray[2].brailleDots[IndexNr]);
    Serial.println(myLetter2BrailleArray[2].brailleDots[IndexNr + 1]);
  }
  Serial.println();
  
}


void loop() {
}

and here is a version that makes use of a function called "myPrintLetterAndBrailleDots"
where aditional variables are used to shorten the code


struct Letter2Braille_type {
  char letter;
  byte brailleDots[6]; // 6 elements with index-numbers 0,1,2,3,4,5
} ;

  // brailledot-numeration
  // 1,4 equals index-No 0 3
  // 2 5 equals index-No 1 4
  // 3 6 equals index-No 2 5

Letter2Braille_type myLetter2BrailleArray[]
{ { .letter = 'A', // first element has index 0
    1, 0,
    0, 0,
    0, 0
  },

  { .letter = 'B', // second element has index 1
    1, 0,
    1, 0,
    0, 0
  },

  { .letter = 'C', // third element has index 2
    1, 1,
    0, 0,
    0, 0
  }

};

void myPrintLetterAndBrailleDots(byte p_LetterIndexNr) {
  Serial.print("letter=#");
  Serial.print(myLetter2BrailleArray[p_LetterIndexNr].letter);
  Serial.println("#");

  for (byte IndexNr = 0; IndexNr < 6; IndexNr += 2) {
    Serial.print  (myLetter2BrailleArray[p_LetterIndexNr].brailleDots[IndexNr]);
    Serial.println(myLetter2BrailleArray[p_LetterIndexNr].brailleDots[IndexNr + 1]);
  }
  Serial.println();
  
}


void setup() {
  // make sure to adjust baudrate of the
  //Arduino-IDE serial monitor to 115200 baud
  Serial.begin(115200);
  Serial.println( F("Setup-Start") );
  Serial.println();

  myPrintLetterAndBrailleDots(0); // letter 'A' has index 0
  myPrintLetterAndBrailleDots(1); // letter 'B' has index 1
  myPrintLetterAndBrailleDots(2); // letter 'C' has index 2

  Serial.println();
  
}


void loop() {
}
1 Like