Arduino IR LCD Würfel

Das mache ich auch , möchte ja auch wissen ob alles so richtig ist.

In meinem ersten Sketsch #1
Functioniert ja ein Würfel so wie ich es möchte ist nur die frage
wie ich jetzt 2 oder 3 Würfel bekomme.
Oder alles von grund auf ?

Na gut.

alles erstmal auf Serial.print.
Playtaste gibt zufalszahl aus.
Die anderen Tasten 1-3 zeigen Taste 1 usw.

Nachtrag:

#include <LiquidCrystal_I2C.h>
#include <IRremote.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

int Zufallszahl1; 
int Zufallszahl2; 
int Zufallszahl3; 
int receiver = 2; 

int x = 4;
IRrecv irrecv(receiver);
decode_results results;

void setup()
{
  Serial.begin(9600);
  lcd.begin(20,4); 
  irrecv.enableIRIn();

    lcd.setCursor (0, 1);
    lcd.print("1, 2, oder 3 Wuerfel");
}

void loop()
{
  if (irrecv.decode(&results))
  {
    translateIR(); 
    irrecv.resume();
  }  
}
  
void translateIR()
{

   Zufallszahl1 = random(6)+1;
   Zufallszahl2 = random(6)+1;
   Zufallszahl3 = random(6)+1;
      
  switch(results.value)
  {

  case 0x9716BE3F: 
   Serial.println("Taste1"); // Taste1  
    
      break;
  case 0x3D9AE3F7: 
   Serial.println("Taste2"); // Taste2 
        
      break;
  case 0x6182021B: 
   Serial.println("Taste3"); // Taste3     

      break;
  case 0xD7E84B1B: 
   Serial.println("#Taste Play"); // Taste Play      
   Serial.println(Zufallszahl1 ); 
   Serial.println(Zufallszahl2 ); 
   Serial.println(Zufallszahl3 );   
      break;
  }
}

so, was du nun überlegen musst,
wie schaffst du es dass die Tastendrücke 1-3 einen Einfluss auf die Ausgabe mit Play haben.

Du siehst es immer noch nicht?

Ich würde dann so weitermachen:

Dazu könntest z.B. eine globale Variablen definieren

uint8_t mode = 3; // which mode (how many dices to throw)

dann diesen mode in den drei Tastaturabfragen entsprechend setzen

Serial.println("Taste1"); // Taste1
mode = 1;

usw.

und im Play dann nur so viele Zufallszahlen ausgeben wie du im mode festgelegt hast

Serial.println("#Taste Play"); // Taste Play
Serial.println(Zufallszahl1 );
if (mode== 3) {Serial.println(Zufallszahl2 );}
if (mode== 3) {Serial.println(Zufallszahl3 );}

Ausprobieren.
Entspricht das jetzt deinem gewünschten Ablauf?

Vom Ablauf ist mir alles klar.
Mein problem ist der big Font nebeneinander.
Habe ja versucht die Zahlen in eine Function aufzurufen Void eins () usw.
Aufgerufen habe ich die Zahlen dan mit

eins ();
x=x+4;

lcd.setCursor(x, 1);

Frage:
Wie bekomme ich die Zahlen Animiert wie bei einer Uhr nur schneller
um das Würfeln zu simulieren.
1 Würfel und
2 oder 3 nebeneinander?

MFG

in dem du dich schrittweise der Sache näherst.

Also mach erst mal einen funktionierenden Sketch der sich bedienen lässt, der dir 1, 2 ODER 3 Zufallszahlen ausgibt.

DANACH
drehen wir die Ausgabe um aufs LCD

und

DANACH

kümmern wir uns ums schön werden sprich eine Animation

In aller Deutlichkeit:
Ich kenne nur diesen einen Weg. Entweder gehst du den mit mir oder du gibst bescheid, dann klinke ich mich aus dem Thread raus.

Alles Super habe Ich ausprobiert mit einer globale Variable

so sieht es aus

   if (mode== 1) {Serial.println(Zufallszahl1);}
   
   if (mode== 2) {Serial.println(Zufallszahl1);
                  Serial.println(Zufallszahl2 );}

   if (mode== 3) {Serial.println(Zufallszahl3 );
                  Serial.println(Zufallszahl1 );
                  Serial.println(Zufallszahl2 );}

so soll es sein.

Ist nur noch die frage wie das mit der Animation geht
um das Würfeln zu Simulieren.

( big Font )

Habe ja versucht die Zahlen in eine Function aufzurufen Void eins () usw.
Aufgerufen habe ich die Zahlen dan mit

eins ();
x=x+4;

lcd.setCursor(x, 1);

zunächst generalisiere deinen LCD Output,

mach eine Funktion zur Darstellung EINES Würfels und übergib ihr zwei Parameter
a) welcher Würfel soll angezeigt werden
und
b) welche Kopfzahl soll angezeigt werden.

z.B.:

void showDice(byte dice, byte count)
// dice: the dice number to show
// count: what to display on the dice

in der Funktion legst du dann auf Grund des Würfels (dice) die Startposition fest, wo du etwas hinschreiben willst (also den ersten Parameter von lcd.setCursor)

bei mir sieht's so aus:

void showDice(byte dice, byte count)
{
  byte startposition[4] = {0, 6, 0, 12};   // define the start positions for the 3 dices - ignore index 0 ;-).
  // I decided to change position of dice 1 with dice 2. Therefore it looks like dice 1 is now in the middle of the 16 digit screen in Mode 1
  if (mode == 2)                           // rearange the positions for the dices in mode 2
  { startposition[1] = 4;
    startposition[2] = 10;
  }

  switch (count)                           // Print the pattern for the specific dice at its startposition
  {
    case 1:
      lcd.setCursor(startposition[dice], row);
      lcd.print(count);                         // this is just a very simple demo to print a specific value to a position
      lcd.setCursor(startposition[dice] + 2, row);
      lcd.write((byte)1);                       // and some custom characters
      break;
    case 2:
      lcd.setCursor(startposition[dice], row);
      lcd.print(count);
      lcd.setCursor(startposition[dice] + 2, row);
      lcd.write((byte)2);
      break;
    case 3:
      lcd.setCursor(startposition[dice], row);
      lcd.print(count);
      lcd.setCursor(startposition[dice] + 2, row);
      lcd.write((byte)3);
      break;
    case 4:
      lcd.setCursor(startposition[dice], row);
      lcd.print(count);
      lcd.setCursor(startposition[dice] + 2, row);
      lcd.write((byte)4);
      break;
    case 5:
      lcd.setCursor(startposition[dice], row);
      lcd.print(count);
      lcd.setCursor(startposition[dice] + 2, row);
      lcd.write((byte)5);
      break;
    default:
      lcd.setCursor(startposition[dice], row);
      lcd.print(count);
      lcd.setCursor(startposition[dice] + 2, row);
      lcd.write((byte)6);
  }
}

ich hab aber nur ein 16x2 und daher zeige ich je Ziffer nur relativ wenig an. Wenn du 3 Zeilen brauchst, dann setze halt mehr.

row kommt bei mir aus einer global damit ich die Startreihe festlegen kann ... ist bei mir 1 (zweite Zeile)

Bau mal so ein Konstrukt und zeige deine Zufallszahlen damit an,

DANACH gibts einen Schnellsiederkurs in State machine und dann die Animation

Bin jetzt erstmal offline
Bis heute Arbend.
Lg

Hier bin ich wider

Hat Super funktioniert !!
Start Augenzahl und Position der Würfel.

Deinen Code kann ich auch gut nachvollziehen.
Da habe ich doch etwas Gelehrt für vieleicht spätere Projekte.

vielen Danke dafür.

Jetzt nur noch die Animation.

LG

Ich werde es jetzt Versuchen
und später meinen Sketsh Posten.

Die Animation habe ich jetzt für einen Würfel so

for(int i = 1; i <= 4 ; i++){
for(int i = 1; i <= 6; i++){
showDice (1,i);
delay (50);
}
}

Soll ich das in jedem mode 1-3 so machen
oder geht es einfacher ?

auf Grund des modes weist du ja wie viele Würfel du brauchst, also könntest das in eine for schleife packen

for(uint8_t i = 1; i <= mode ; i++){

... und pass mit den gleichlautenden i's auf da kommst äugisch durcheinder ... es gibt auch i, j, k ... :wink:

Vorschlag wäre, die Animation auch bereits zu randomisieren:

    // show an animation for rolling
    // currently we just randomize the value in fast intervall
    for (uint8_t i = 1; i <= mode; i++)
    {
      showDice(i, (random(6) + 1));
    }
    delay(250);

habe es jetz so gemacht Funtzt Super

for(int i = 1; i <= 4 ; i++){

for(int i = 1; i <= 6; i++){
  if (mode == 1)
  { showDice (1,i);}
  if (mode == 2)
  { showDice (1,i); 
    showDice (2,i);}
  if (mode == 3)
  { showDice (1,i);
    showDice (2,i);
    showDice (3,i);}
delay (50);
}
}

und hier für die nach Weld mein kompletter Sketch

#include <LiquidCrystal_I2C.h>
#include <IRremote.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

int Zufallszahl1; 
int Zufallszahl2; 
int Zufallszahl3; 
int receiver = 2; 
uint8_t mode = 4; 
int x = 4;
int row =1;
IRrecv irrecv(receiver);
decode_results results;

byte SpecialChar0[8]={B00011,B00111,B01111,B11111,B00000,B00000,B00000,B00000};
byte SpecialChar1[8]={B11000,B11100,B11110,B11111,B00000,B00000,B00000,B00000};
byte SpecialChar2[8]={B11111,B11111,B11111,B11111,B00000,B00000,B00000,B00000};
byte SpecialChar3[8]={B11111,B11110,B11100,B11000,B00000,B00000,B00000,B00000};
byte SpecialChar4[8]={B11111,B01111,B00111,B00011,B00000,B00000,B00000,B00000};
byte SpecialChar5[8]={B00011,B00111,B01111,B11111,B11111,B11111,B11111,B11111};
byte SpecialChar6[8]={B11000,B11100,B11110,B11111,B11111,B11111,B11111,B11111}; 
byte SpecialChar7[8]={B11111,B11111,B11111,B11111,B11111,B11111,B11111,B11111};

void setup()
{
  Serial.begin(9600);
  lcd.begin(20,4); 
  irrecv.enableIRIn();

    lcd.createChar(0, SpecialChar0);
    lcd.createChar(1, SpecialChar1);
    lcd.createChar(2, SpecialChar2);
    lcd.createChar(3, SpecialChar3);
    lcd.createChar(4, SpecialChar4);
    lcd.createChar(5, SpecialChar5);
    lcd.createChar(6, SpecialChar6);
    lcd.createChar(7, SpecialChar7);
    
    lcd.setCursor (0, 1);
    lcd.print("1, 2, oder 3 Wuerfel");
}

void loop()
{
  if (irrecv.decode(&results))
  {
    translateIR(); 
    irrecv.resume();
  }  
}

void showDice(byte dice, byte count)
{
  byte startposition[4] = {0, 3, 0, 11};   // define the start positions for the 3 dices - ignore index 0 ;-).
  // I decided to change position of dice 1 with dice 2. Therefore it looks like dice 1 is now in the middle of the 16 digit screen in Mode 1
  if (mode == 1)                           // rearange the positions for the dices in mode 2
  { startposition[1] = 6;
  }
  
  if (mode == 2)                           // rearange the positions for the dices in mode 2
  { startposition[1] = 4;
    startposition[2] = 9;
  }
  if (mode == 3)                           // rearange the positions for the dices in mode 2
  { startposition[1] = 1;
    startposition[2] = 2;
    startposition[2] = 6;
  }
  
  switch (count)                           // Print the pattern for the specific dice at its startposition
  {
    case 1:
      lcd.setCursor(startposition[dice], row);
                         // this is just a very simple demo to print a specific value to a position
      lcd.setCursor(startposition[dice] + 2, row);
  lcd.write((byte)0);
  lcd.write((byte)7);
  lcd.print (" ");                       // and some custom characters
      lcd.setCursor(startposition[dice] + 2, row+1);
  lcd.print (" ");
  lcd.write((byte)7);
  lcd.print (" ");
      lcd.setCursor(startposition[dice] + 2, row+2);
  lcd.write((byte)2);
  lcd.write((byte)2);
  lcd.write((byte)2);          
      break;

    case 2:
      lcd.setCursor(startposition[dice], row);

      lcd.setCursor(startposition[dice] + 2, row);
  lcd.write((byte)0);
  lcd.write((byte)2);
  lcd.write((byte)6);
      lcd.setCursor(startposition[dice] + 2, row+1);
  lcd.write((byte)5);
  lcd.write((byte)2);
  lcd.write((byte)3);
      lcd.setCursor(startposition[dice] + 2, row+2);
  lcd.write((byte)4);  
  lcd.write((byte)2);
  lcd.write((byte)2);  
      break;
 
    case 3:
      lcd.setCursor(startposition[dice], row);

      lcd.setCursor(startposition[dice] + 2, row);
  lcd.write((byte)0);
  lcd.write((byte)2);
  lcd.write((byte)6);
      lcd.setCursor(startposition[dice] + 2, row+1);
  lcd.print (" ");
  lcd.write((byte)2);
  lcd.write((byte)7);  
      lcd.setCursor(startposition[dice] + 2, row+2);
  lcd.write((byte)4);  
  lcd.write((byte)2);
  lcd.write((byte)3);     
      break;
 
    case 4:
      lcd.setCursor(startposition[dice], row);

      lcd.setCursor(startposition[dice] + 2, row);
  lcd.write((byte)5);
  lcd.print (" "); 
  lcd.write((byte)7);
      lcd.setCursor(startposition[dice] + 2, row+1); 
  lcd.write((byte)2);
  lcd.write((byte)2);
  lcd.write((byte)7);      
      lcd.setCursor(startposition[dice] + 2, row+2);       
  lcd.print (" "); 
  lcd.print (" "); 
  lcd.write((byte)2);           
      break;

    case 5:
      lcd.setCursor(startposition[dice], row);

      lcd.setCursor(startposition[dice] + 2, row);
  lcd.write((byte)7);
  lcd.write((byte)2);
  lcd.write((byte)2);
      lcd.setCursor(startposition[dice] + 2, row+1); 
  lcd.write((byte)2);
  lcd.write((byte)2);
  lcd.write((byte)7);      
      lcd.setCursor(startposition[dice] + 2, row+2);      
  lcd.write((byte)4);
  lcd.write((byte)2);
  lcd.write((byte)3);      
      break;

    default:
      lcd.setCursor(startposition[dice], row);

      lcd.setCursor(startposition[dice] + 2, row);
  lcd.write((byte)5);
  lcd.write((byte)2);
  lcd.write((byte)1);
      lcd.setCursor(startposition[dice] + 2, row+1);
  lcd.write((byte)7);
  lcd.write((byte)2);
  lcd.write((byte)6);
      lcd.setCursor(startposition[dice] + 2, row+2);
  lcd.write((byte)4);
  lcd.write((byte)2);
  lcd.write((byte)3);  
  }
}


  
void translateIR()
{

   Zufallszahl1 = random(6)+1;
   Zufallszahl2 = random(6)+1;
   Zufallszahl3 = random(6)+1;
      
  switch(results.value)
  {

  case 0x9716BE3F: 
   Serial.println("Taste1"); // Taste1  

  mode = 1;    
      break;
  case 0x3D9AE3F7: 
   Serial.println("Taste2"); // Taste2 
 mode = 2;         
      break;
  case 0x6182021B: 
   Serial.println("Taste3"); // Taste3     
 mode = 3; 
      break;
  case 0xD7E84B1B: 
   Serial.println("#Taste Play"); // Taste Play      

         lcd.clear ();
for(int i = 1; i <= 4 ; i++){

for(int i = 1; i <= 6; i++){
  if (mode == 1)
  { showDice (1,i);}
  if (mode == 2)
  { showDice (1,i); 
    showDice (2,i);}
  if (mode == 3)
  { showDice (1,i);
    showDice (2,i);
    showDice (3,i);}
delay (50);
}
}
   if (mode== 1) {
         showDice (1,Zufallszahl1);}   
   if (mode== 2) {
         lcd.clear ();
         showDice (1,Zufallszahl1); showDice (2,Zufallszahl2);}
   if (mode== 3) {
         lcd.clear ();
         showDice (1,Zufallszahl1); showDice (2,Zufallszahl2); showDice (3,Zufallszahl3);} 
      
      break;
  }
  }

Alle hier im Thema Danke ich sehr.
und Danke das ihr es mit mir ausgehalten habt.

Das großte Danke schön gehört noiasca
er hatte viel gedult mit mir und schritt für schritt bin ich am Ziehl.
Das Beste ist aber Ich habe wirglich was dazu Gelehrt.

1000 mal Danke !!!

LG tsw68

Hi

Das kompiliert so??
Innerhalb der äußeren Schleife sollte i definiert sein, somit müsste die Definition in der 2.ten Schleife einen Error hervorrufen.

Weiter: Du kannst Deine eigenen Posts auch editieren.
Klar, wenn Du über die 9000 Zeichen kommst, ist der Post voll.

MfG

PS: Glückwunsch, daß Du Deinen Sketch so weit zusammen bekommen hast!
(und Danke für's Zeigen)

@TSW68
drück mal STRG-T in der IDE, dann werden die Formatierungen/Einrückungen gerade gerückt.

Wenn du noch verbessern willst, du könntest einige Themenrelevante IF's mit else if verschachteln.

und die Idee vom
byte startposition[4] = {0, 3, 0, 11}; // define the start positions for the 3 dices - ignore index 0 ;-).

war eigentlich, dass hier bereits die Standard-Positionen für die Würfel stehen
Würfel 0 gibt es nicht
Würfel 1 kommt auf 3
Würfel 2 kommt auf 0 also links vom ersten
Würfel 3 kommt auf 11 also rechts davon

daher bräuchte es eigentlich keinen

if (mode == 1) // rearange the positions for the dices in mode 2

wenn du den Startwert für dich anpasst. Außerdem ist das Kommentar noch falsch

Ergo das

if (mode == 3) // rearange the positions for the dices in mode 2
{ startposition[1] = 1;
startposition[2] = 2;
startposition[2] = 6;
}

ist imho auch zu überarbeiten, startpositon[2] wird immer 6 sein.
und die startpostion[3]=11 kommt daher immer aus dem Array am Anfang.

Wenn du also das Array sinnvoll setzt so dass es für Mode 1 und 3 passt, brauchst wirklich nur ein If für den Mode 2.

die ersten lcd.setCursor in den Ausgaben sind offenbar auch noch übriggeblieben ... die können auch raus.

@Postmaster: ja sein sketch kompiliert prinzipiell (hab offenbar eine andere I2C_LCD lib ...)

Hallöchen,

Es läuft alles Super.
Werde meinen Sketch noch einmal überarbeiten und werde deinen Rat befolgen.
Mein Würfel ist mit eurer Hilfe echt gut geworden und hoffe
es gibt hier im Forum noch viele die ihn gebrauchen können.

Mit freundlichen Grüßen