Multiplication Trainer

Multiplication Trainer

I designed a simple code so that my son could memorize his tables. This code generates random numbers between 2 and 12 and asks for the product of the 2 numbers. You need to enter the answer and then press the # key. If you answer correctly it will display a message with the correct answer for 3 seconds and then move on to asking a new question. If you answer incorrectly, it will display an appropriate message with the correct answer for 5 seconds and move on to asking the next question.

It also displays the date, time, temperature and humidity when you press the * key for 10 seconds. 

Below are some images - 



Code - 

#include <dht.h>
#include <Wire.h>
#include <LCD.h>
#include "RTClib.h"
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
LiquidCrystal_I2C lcd(0x20, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Addr, En, Rw, Rs, d4, d5, d6, d7, backlighpin, polarity
RTC_DS1307 RTC;

#define DHTPIN A2     // what pin we're connected DHT11
#define DHTTYPE DHT   // DHT 11 
#define dht_dpin A2
dht DHT;


int randNumber1;
int randNumber2;
int answer;
String answerreceived;
int counter;

const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 9, 8, 7, 6 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 12, 11, 10 }; 

// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );


void setup(){
  //kpd.addEventListener(keypadEvent);
  //Serial.begin(9600);
  DHT.read11(dht_dpin);
  lcd.begin(16,2);
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print(" Welcome Varun");  
  delay(2000);
  lcd.clear();
  
 
  Wire.begin();
  
// part code from http://tronixstuff.wordpress.com/
Wire.beginTransmission(0x68);
Wire.write(0x07); // move pointer to SQW address
Wire.write(0x10); // sends 0x10 (hex) 00010000 (binary) to control register - turns on square wave
Wire.endTransmission();
// end part code from http://tronixstuff.wordpress.com/

    RTC.begin();
// RTC.adjust(DateTime(__DATE__, __TIME__));
// if you need set clock... just remove // from line above this
//  if (! RTC.isrunning()) {
//    Serial.println("RTC is NOT running!");
//    // following line sets the RTC to the date & time this sketch was compiled
//    RTC.adjust(DateTime(__DATE__, __TIME__));
//  }
  randomSeed(analogRead(0));
  randNumber1 = random(2,13);
  randNumber2 = random(2,10);
  

  
  //answerreceived = Serial.read();
}

void loop() {
  
  
  
  
  
  
            char key = kpd.getKey();
            //lcd.blink();
            //lcd.clear();
            lcd.setCursor(0, 0);
            lcd.print("Answer this?");
            
            lcd.setCursor(0, 1); 
            lcd.print(randNumber1);
            lcd.setCursor(4, 1); 
            lcd.print("x");
            lcd.setCursor(7, 1); 
            lcd.print(randNumber2);
            
           // Serial.print(answerreceived);
           // answer = randNumber1 * randNumber2;
           // Serial.print(answer);
            
            //answerreceived = "";
            
            //char key = kpd.getKey();
            
             if (key){
                    
                answerreceived +=  key;
                //Serial.print(answerreceived);
              }
            
                    if (key == '#') {
                            //int answerreceived = int(key);
                            int answer = randNumber1 * randNumber2;
                            //Serial.println(answerreceived.toInt());
                            
                            if (answer == answerreceived.toInt()) {
                                //Serial.print("checking");
                                lcd.setCursor(0, 0); 
                                lcd.clear();
                                lcd.print("    Correct!");
                                lcd.setCursor(0, 1);
                                lcd.print("Right Ans = ");
                             
                                lcd.setCursor(12, 1);
                                
                                lcd.print(answer,DEC);
                                
                                delay(2000);
                                lcd.clear();
                                
                                counter = 0;
                                
                                
                                
                                randomSeed(analogRead(0));
                                randNumber1 = random(2,13);
                                randNumber2 = random(2,10);
                                
                                answerreceived = "";
                            }
                             else
                             {
                               lcd.clear();
                               lcd.print("  Wrong Answer!");
                               lcd.setCursor(0, 1);
                               lcd.print("Answer is = ");
                               lcd.setCursor(13, 1);
                               lcd.print(answer);
                               
                               delay(5000);
                               lcd.clear();
                               
                               randomSeed(analogRead(0));
                               randNumber1 = random(2,13);
                               randNumber2 = random(2,10);
                               
                               answerreceived = "";
  
                            }
                          
                      }
                      
                if (key == '*') {
                    counter = 0;
                    while (counter < 11)
                    {                    
                      //Serial.println(key);
                      DateTime now = RTC.now();
                      int h = DHT.humidity;
                      int t = DHT.temperature;
                       lcd.clear();
                       lcd.setCursor(1, 0);
                       if ( now.hour() < 10)
                       {
                         lcd.print(" "); 
                         lcd.print(now.hour(), DEC);
                       }
                       else
                       {
                       lcd.print(now.hour(), DEC);
                       }
                       lcd.print(":");
                       if ( now.minute() < 10)
                       {
                         lcd.print("0"); 
                         lcd.print(now.minute(), DEC);
                       }
                       else
                       {
                       lcd.print(now.minute(), DEC);
                       }
                       lcd.print(":");
                       if ( now.second() < 10)
                       {
                         lcd.print("0"); 
                         lcd.print(now.second(), DEC);
                       }
                       else
                       {
                       lcd.print(now.second(), DEC);
                       }
                       lcd.print(" "); 
                       
                       lcd.setCursor(12, 0);
                       lcd.print(t);
                       lcd.write(0b11011111);
                       lcd.print("C");
                        
                       lcd.setCursor(0, 1);
                       if ( now.day() < 10)
                       {
                         lcd.print("0"); 
                         lcd.print(now.day(), DEC);
                       }
                       else
                       {
                       lcd.print(now.day(), DEC);
                       }
                       lcd.print("/");
                       if ( now.month() < 10)
                       {
                         lcd.print("0"); 
                         lcd.print(now.month(), DEC);
                       }
                       else
                       {
                       lcd.print(now.month(), DEC);
                       }
                       lcd.print("/");
                       lcd.print(now.year(), DEC);
                       lcd.print(""); 
                       
                       lcd.setCursor(12, 1);
                       lcd.print(h);
                       lcd.print("%u");
                      
                       delay(1000);
                       lcd.clear();
                     
                    counter = counter + 1;
                    
                    }    
                       
                       char key;
                       answerreceived = "";
                 
           }
              
}





Comments

Popular posts from this blog

Temperature and Humidity sensor using Arduino display on I2C 16X2 LCD

Simple LED memory game using Arduino