Diode connection.

Help me please!

I do not understand how to connect the LED so that it blinks synchronously with the displayed digit in the com port.
If it is printed 1 it should blink once, 9 times nine times.

int dig1 ;
int dig2;
int dig3;
int dig4;


void setup() 
{
  Serial.begin(9600); // 
   Serial.print( " GO " );
}

void loop() 
{
  int dig1, dig2, dig3, dig4; // 
  int keys[10] = { 0,1, 2, 3, 4, 5, 
  6, 7, 8, 9  }; // 

// 

  for(dig1=1; dig1<10; dig1++-
  {
    for(dig2=1; dig2<10; dig2++-
    {
      for(dig3=1; dig3<10; dig3++-
      {
        for(dig4=1; dig4<10; dig4++=
        {
          Serial.print(keys[dig1]);
          delay(400);
          Serial.print(keys[dig2]);
          delay(400);
          Serial.print(keys[dig3]);
          delay(400);
          Serial.print(keys[dig4]);
          delay(400);
          Serial.println();
          delay(1000);
        }
      }
    }
  }
}

Your syntax is far off.
It is unclear to me what you want your code to do.

I want the diode to flash the corresponding digit

digit 1 flashes once

digit 2 flashes twice
and so on 1-9

above is the code it prints the numbers in order
1111 diode flashes four times with a pause of 1 minute
1112 the diode flashes three times with a pause of one minute and twice with a pause of five seconds
and so on
this is a kind of sorting through the digits with a diode

Your code does nothing with any LEDs. You have four “dig” variables (defined twice) but nothing ever gives them any values. Your for loops use invalid syntax. Have another try.

And the specification is very unclear. Can the digits be 0 to 9? When do you have no gap between flashes, when 5 second gap and when 1 minute? E.g. what does 3192 do? How do you tell the difference between 4000 and 1111?

Steve

Try the following:-

In your setup() function of your code add the following lines:-

pinmode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);

This will use the built-in led (connected to pin 13 on Uno and Nano boards and to other pins on other variants).

Now create a simple function called something like ledFlash:-

void ledFlash(int flashes) {
   for(int i = 1; i == flashes; i++) {
      digitalWrite(LED_BUILTIN, HIGH);
      delay(100);
      digitalWrite(LED_BUILTIN, LOW);
      delay(100);
   }

Then every time you use Serial.print to print a digit call ledFlash with the same digit e.g. :-

Serial.print(keys[dig1]);
ledFlash(keys[dig1]);

BTW your syntax for your for statements is wrong. I leave it as an exercise for the reader to interpret the compiler error messages.

Ian

How do you blink a “0”? :slight_smile: Here’s an old program called “poor man’s GUI” that might give you some ideas, type a number (1 to 999) in the top of serial monitor and press [ENTER].

// Poor man's GUI


byte led = 13;

void pmGUI(byte led, int num)
{
  int interval = 350, i;

  if(num == 0){
    num = 4; 
    interval = 50;
  }
  for(i = 0;i < num;i++){
    digitalWrite(led,HIGH); 
    delay(50); 
    digitalWrite(led,LOW);
    delay(interval);
  }
  delay(600);
}

void setup() {
  Serial.begin(9600);
  pinMode(led,OUTPUT);
  Serial.println("  Type a number from 0 to 999, then <ENTER> ");
  Serial.println("      A \"stutter\" = zero \n");
}
void loop() {
    // if there's any serial available, read it:
  while (Serial.available() > 0) {

    // look for the next valid integer in the incoming serial stream:
    int num = Serial.parseInt(); 
    // look for the newline. That's the end of your
    // sentence:
    if (Serial.read() == '\n') {
    }
    if(num > 99) pmGUI(led,num / 100);
    if(num > 9) pmGUI(led,num % 100  / 10);
    pmGUI(led,num % 10);
  }
}

outsider:
How do you blink a "0"? :slight_smile:

If you look carefully the code never actually prints a zero (possibly a bug?). All the for loops start at i=1 and terminate when i == 10. Hence dig1, dig2, dig3 and dig4 all range between 1 and 9 and therefore only index keys[1] to keys[9].

I won't even mention the fact that the keys array is completely redundant :slight_smile:

Ian

int dig1 ;
int dig2;
int dig3;
int dig4;
int ledPin = 13;


void setup() 
{
  Serial.begin(9600); 
   Serial.print( " GO " );
  pinMode(13, OUTPUT);
digitalWrite(13, LOW);
}

void loop() 
{
  int dig1, dig2, dig3, dig4; 
  int keys[10] = { 0,1, 2, 3, 4, 5, 
  6, 7, 8, 9  }; 

 
  for(dig1=2; dig1<10; dig1++)
  {
    for(dig2=2; dig2<10; dig2++)
    {
      for(dig3=2; dig3<10; dig3++)
      {
        for(dig4=2; dig4<10; dig4++)
        {
        
          
          Serial.print(keys[dig1]);
          ledFlash(keys[dig1]);
          delay(800);
          Serial.print(keys[dig2]);
          ledFlash(keys[dig2]);
          delay(800);
          Serial.print(keys[dig3]);
          ledFlash(keys[dig3]);
          delay(800);
          Serial.print(keys[dig4]);
          delay(800);
          ledFlash(keys[dig4]);
          Serial.println();
          delay(5000);
         
        
        
       
          
          
        }
      }
    }
  }
}

void ledFlash(int flashes) {
 for(int i = 1; i ==flashes ; i++) 
  {
    
        
      digitalWrite(13, HIGH);
      delay(10);
      digitalWrite(13, LOW);
      delay(10);
     
     
    
   }
 
 }

unfortunately the code does not work
1 blinks times
on other numbers does not react
I’m not a programmer on this, I do not understand much
:cold_sweat:

Maksrus:

int dig1 ;

int dig2;
int dig3;
int dig4;
int ledPin = 13;

void setup()
{
  Serial.begin(9600);
  Serial.print( " GO " );
  pinMode(13, OUTPUT);
digitalWrite(13, LOW);
}

void loop()
{
  int dig1, dig2, dig3, dig4;
  int keys[10] = { 0,1, 2, 3, 4, 5,
  6, 7, 8, 9  };

for(dig1=2; dig1<10; dig1++)
  {
    for(dig2=2; dig2<10; dig2++)
    {
      for(dig3=2; dig3<10; dig3++)
      {
        for(dig4=2; dig4<10; dig4++)
        {
       
         
          Serial.print(keys[dig1]);
          ledFlash(keys[dig1]);
          delay(800);
          Serial.print(keys[dig2]);
          ledFlash(keys[dig2]);
          delay(800);
          Serial.print(keys[dig3]);
          ledFlash(keys[dig3]);
          delay(800);
          Serial.print(keys[dig4]);
          delay(800);
          ledFlash(keys[dig4]);
          Serial.println();
          delay(5000);
       
       
       
     
         
         
        }
      }
    }
  }
}

void ledFlash(int flashes) {
for(int i = 1; i ==flashes ; i++)
  {
   
       
      digitalWrite(13, HIGH);
      delay(10);
      digitalWrite(13, LOW);
      delay(10);
   
   
   
  }

}




unfortunately the code does not work
1 blinks times
on other numbers does not react
I'm not a programmer on this, I do not understand much
:cold_sweat:

Sorry, my bad, there was a typo in my code the line

for(int i = 1; i ==flashes ; i++)

should read

for(int i = 1; i <=flashes ; i++)

By the way you have now limited your for loops to the range 2 to 9. Was this deliberate?

Note setting the delays in ledFlash to 10ms means you may not be able to see the individual flashes as they are coming at 50Hz which is usually beyond what persistence of vision will allow the human eye to discriminate.

Ian

No, this is not a typo, I checked that it really does not blink twice for 2.

outsider:
How do you blink a “0”? :slight_smile: Here’s an old program called “poor man’s GUI” that might give you some ideas, type a number (1 to 999) in the top of serial monitor and press [ENTER].

// Poor man's GUI

byte led = 13;

void pmGUI(byte led, int num)
{
 int interval = 350, i;

if(num == 0){
   num = 4;
   interval = 50;
 }
 for(i = 0;i < num;i++){
   digitalWrite(led,HIGH);
   delay(50);
   digitalWrite(led,LOW);
   delay(interval);
 }
 delay(600);
}

void setup() {
 Serial.begin(9600);
 pinMode(led,OUTPUT);
 Serial.println("  Type a number from 0 to 999, then “);
 Serial.println(”      A “stutter” = zero \n");
}
void loop() {
   // if there’s any serial available, read it:
 while (Serial.available() > 0) {

// look for the next valid integer in the incoming serial stream:
   int num = Serial.parseInt();
   // look for the newline. That’s the end of your
   // sentence:
   if (Serial.read() == ‘\n’) {
   }
   if(num > 99) pmGUI(led,num / 100);
   if(num > 9) pmGUI(led,num % 100  / 10);
   pmGUI(led,num % 10);
 }
}

this code works correctly, but the numbers are entered from the keyboard I need automatically, without my participation.

Maksrus:
No, this is not a typo, I checked that it really does not blink twice for 2.

Yes it was a typo. For loops continue until the second parameter is false. If you use == instead of <= then the condition is never true and the for loop never executes.

Change the condition to less than or equal to ( <= ) and increase the delays to 100ms and the sketch will function in the way you want. If you keep the delays at 10ms the flashes will merge into one flash of varying length. This is due to the way the human eye works.

Ian

IanCrowe:
Yes it was a typo. For loops continue until the second parameter is false. If you use == instead of <= then the condition is never true and the for loop never executes.

Change the condition to less than or equal to ( <= ) and increase the delays to 100ms and the sketch will function in the way you want. If you keep the delays at 10ms the flashes will merge into one flash of varying length. This is due to the way the human eye works.

Ian

But it does work.
Thank you so much.