Show Posts
Pages: [1] 2
1  Using Arduino / Programming Questions / Re: Several getKey() on: March 29, 2011, 09:00:46 pm
and one more thing..How to assign answer so that I can make if (key == answer)?
example,
Code:
int answer = 0;
int i, j;
i=random(4);
j=random(5);
lcd.print(i);
lcd.print("+");
lcd.print(j);
answer=i+j;
key = keypad.getKey();
                if (key != NO_KEY)
                {
                  if (key == answer) // how to do here?
                    //print something..
2  Using Arduino / Programming Questions / Re: Several getKey() on: March 28, 2011, 03:42:31 am
this is work..
Code:
void loop()
{

  switch (keypad.getKey())    
  {
  case '*':
// nom nom nom

but for the next switch, when I press 4 or other key, nothing happen..so I assume that this getKey() not working in switch..
Code:
key = keypad.getKey();
      if (key != NO_KEY)
      {
        switch (key)
        {
          case '1':
              i = random(4);
              j = random(5);
              if ((i<=1 && j<=5) || (i<=4 && j<=1))
              {
                lcd.setCursor(0,1);
                lcd.print("You Choose Add");
                delay(2000);
                lcd.clear();
                lcd.setCursor(1,0);
                lcd.print("What is 1+3=");
                 // when I press 4 or other key, nothing happen..
                 // so I assume that this getKey() not working in switch..
                key = keypad.getKey();
                if (key != NO_KEY)
                {
                  if (key == '4')
                  {
                    lcd.setCursor(1,0);
                    lcd.print(key);
                    delay(2000);
                    lcd.clear();
                    lcd.setCursor(0,1);
                    lcd.print("Great Job!!!");
                    correct++;
                  }
                  else
                    lcd.print("Sorry.. try again");



3  Using Arduino / Programming Questions / Re: Several getKey() on: March 28, 2011, 12:03:36 am
I have another question.. Can I have keypad.getKey() in if statement or switch?
4  Using Arduino / Programming Questions / Re: Several getKey() on: March 23, 2011, 11:15:04 pm
So, I cant have answer with two digits..
But, in Addition(), how can I capture the key press by user, is it correct to use if (key == answer)?
5  Using Arduino / Programming Questions / Several getKey() on: March 23, 2011, 07:11:04 pm
Hi..
I have problem for the keypad.getKey(). Here are the program.
Code:
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

char answer;
int correct;
int wrong;
long i;
long j;

void setup()
{
  //lcd.begin(16,2);
  //lcd.clear();
  Serial.begin(9600);
  //Serial.print(254, BYTE);
  //delay(100);
  Serial.println("Welcome to MATH GAME!!!");  // Initial dispaly
  Serial.println("Press * to start");         // Message on lcd
  //delay(2000);
 
 
}

void loop()
{
  char key = keypad.getKey();
 
  if (key != NO_KEY)
  {
    switch (key)     
    {
      case '*':
            Serial.println("[1] Addition");
            Serial.println("[2] Subtraction");
            Serial.println("[3] Exit");
            Serial.println("Choose");
            delay(1000);
            break; 
       
      default:
          Serial.println("Please Press *");
    }
  }
  key = keypad.getKey();
            if (key != NO_KEY)
            {
              switch (key)
              {
                  case '1':
                    //Serial.println("ONE");
                    Addition();
                    break;
                   
                  case '2':
                    Serial.println("Subtraction");
                    break;
                   
                  default:
                    Serial.println("Nothing");
              }
            }   
}

void Addition()
{
  i = random(10);
  j = random(10);

  if ((i<9) && (j<9))
  {
      Serial.print(i);
      Serial.print("+");
      Serial.print(j);
      Serial.print("=");
      answer=i+j;
     
      char key = keypad.getKey();
      if (key != NO_KEY)
      {
        if (key == answer)
          Serial.println("CORRECT");
        else
          Serial.println("WRONG");
      }
  }
  //Serial.println(answer);
 
}
The problem is for the "Press * to start", when I press *, the output is
[1] Addition
[2] Subtraction
[3] Exit
Choose

Then I choose 1, the output is Please Press *. I press 1 for three times..only the third one, the output is correct (0+2)
Please Press *
Please Press *
0+2
 
Then when I entered the answer, 2, the output is Subtraction. What I want is the output should be "CORRECT".
Actually I do not know how this program works and why this happen?..

6  Using Arduino / Displays / Re: How to display many lines on lcd 16x2? on: March 15, 2011, 05:51:16 pm
ops sorry..actually,
Code:
lcd.print(16,2)
is
Code:
lcd.begin(16,2)
7  Using Arduino / Displays / Re: How to display many lines on lcd 16x2? on: March 14, 2011, 05:38:02 pm
here some of the whole code..
Code:
#include <Keypad.h>
#include <LiquidCrystal.h>


LiquidCrystal lcd(5, 4, 3, 2, 1, 0);

const byte ROWS = 4;  // four rows
const byte COLS = 4;  // four columns

char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

byte rowPins[ROWS] = {6, 7, 8, 9}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {10, 11, 12, 13}; //connect to the column pinouts of the keypad


int answer;
int correct = 0;
int wrong = 0;

long i;
long j;

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
//NewSoftSerial mySerial = NewSoftSerial(14,15);  // Soft Serial naming of analog Pin1

void setup()
{
  lcd.print(16, 2);
  lcd.print("Welcome");  // Initial dispaly
  lcd.print("Press * to start");       
}

void loop()
{
  lcd.setCursor(0, 1);
 
  char key = keypad.getKey();
  if (key != NO_KEY)
  {
    lcd.print(key);
    if (key == '*')
    {
      // When it picks up the "*" then it starts gathering the digits
      //delay(100);
      lcd.print("  **KIDS MATH");
      lcd.print(" QUIZ GAME!**");
      lcd.print("******MENU******");
      lcd.print("[1] Addition");
      lcd.print("[2] Subtraction");
      lcd.print("[3] Exit");
      lcd.print("Enter Your Choice: ");
     
      delay(2000);
      if (key == '1')
      {
        Addition();
      }
      else if (key == '2')
      {
        Subtraction();
      }
      else if (key == '3')
      {
        Exit();
      }
      else
      {
        lcd.print("Please Choose 1 or 2 or 3.");
      }
    }
     
    else
    {
      lcd.print("please enter * to start");
    }
  }
}

void Addition()
{
  lcd.print(254, BYTE);
  delay(100);
  lcd.print(1,BYTE);
  delay(100);
 
  i = random(10);
  j = random(10);
 
  if ((i<=1 && j<=9) || (i<=9 && j<=1))
  {
    lcd.print("What is ");
    lcd.print(i);
    lcd.print(" + ");
    lcd.print(j);
    lcd.print(" = ");
    answer = i + j;
   
    char key = keypad.getKey();
    if (key != NO_KEY)
    {
      if (key == answer)
      {
        lcd.print("Great Job!!!");
        correct++;
      }
      else
      {
        lcd.print("Sorry.. ");
        lcd.print(i);
        lcd.print(" + ");
        lcd.print(j);
        lcd.print(" = ");
        lcd.print(answer);
        wrong++;
      }
      lcd.print("You got ");
      lcd.print(correct);
      lcd.print(" right and ");
      lcd.print(wrong);
    }
  }
 
8  Using Arduino / Displays / Re: 16x2 lcd not working on: March 14, 2011, 03:21:36 am
the lcd already working..after checked for several times, there are error in connecting the pins (there are some pins of arduino that connect to wrong lcd pins).
9  Using Arduino / Displays / How to display many lines on lcd 16x2? on: March 14, 2011, 03:14:29 am
Hello..
I working to finishing my Arduino project name mathematics game and I have some trouble to display many lines on LCD display 16x2.
What should I do?
I want to display this.
Code:
lcd.setCursor(0, 1);
 
  char key = keypad.getKey();
  if (key != NO_KEY)
  {
    lcd.print(key);
    if (key == '*')
    {
      // When it picks up the "*" then it starts gathering the digits
      //delay(100);
      lcd.print("  **KIDS MATH");
      lcd.print(" QUIZ GAME!**");
      lcd.print("******MENU******");
      lcd.print("[1] Addition");
      lcd.print("[2] Subtraction");
      lcd.print("[3] Exit");
      lcd.print("Enter Your Choice: ");
     
      delay(2000);
      if (key == '1')
      {
        Addition();
      }
      else if (key == '2')
      {
        Subtraction();
      }
      else if (key == '3')
      {
        Exit();
      }
      else
      {
        lcd.print("Please Choose 1 or 2 or 3.");
      }
    }
10  Using Arduino / Displays / Re: 16x2 lcd not working on: March 06, 2011, 11:51:00 pm
should I change the library or any suggestion for the trouble?
11  Using Arduino / Displays / 16x2 lcd not working on: March 06, 2011, 11:49:24 pm
Hello..Im new to Arduino and now working for Arduino project.
I have problem on my lcd where when I running example of HelloWorld, only lights turn on but nothing appear on the screen.
I have no idea whether Im using the wrong library or anything wrong with my connection. For information, Im using LiquidCrystal library and I follow tutorial from http://www.ladyada.net/learn/lcd/charlcd.html.

Thanks for any help
12  Using Arduino / Programming Questions / Re: Convert C++ to Arduino Programming on: February 06, 2011, 10:25:24 pm
instead of using keypad, we still need to have mySoftSerial?
okay..I will try to include it. But, is it same mySerial.available and keypad.getkey()?Sorry for ask this question. I really don't know.
Code:
if (mySerial.available()) {
      mySerial.print((char)mySerial.read());

Code:
char key = keypad.getKey();
 
  if (key != NO_KEY)
  {
    Serial.println(key);
  }
13  Using Arduino / Programming Questions / Convert C++ to Arduino Programming on: February 06, 2011, 07:19:58 am
Hi everyone. I am new to Arduino. I have two problem. First, I have a program in c++ language and I have some problem to convert it to Arduino programming (in C++ we use "cin" to ask user to enter answer but how about in Arduino?). Should anybody give me some guide and suggestion..I am using Arduino Uno and keypad 16 button.

second, Any idea to make a complete circuit that connect Keypad 16 button and Arduino. I already google about it but still have no solution.

Here some of C++ program and Arduino that I already try..
Thanks a lot for any help.

Code:
int main()
{
        int sum;
        int answer;
        int correct = 0;
        int wrong = 0;
        int menu;
        int i;
        int j;

         srand(time(0));


        do {
                cout << setw( 50 ) << "Kids Math Quiz Game!!!" << endl;
                cout << setw( 49 ) << "**********Menu**********" << endl;
                cout << setw( 44 ) << "[1] Addition " << endl;
                cout << setw( 47 ) << "[2] Subtraction " << endl;
                cout << setw( 40 ) << "[3] Exit " << endl << endl;
                cout <<"Please enter your choice: ";
                cin >> menu;

                switch ( menu )
                {
                 case 1:
                        i = rand()%10;
                        j = rand()%10;

                        if ((i<=1 && j<=9) || (i<=9 && j<=1))
                        {
cout << "What is " << i << " + " << j << " = ";
                         cin >> answer;
                         sum = i + j;

                         if( answer == sum )
                           {
                         cout <<"Great Job!!!!!" << endl << endl;
                           correct++;
                         }
                         else
                           {
                         cout << "Sorry " << i <<" + " << j << " = " << sum <<endl << endl;
                           wrong++;
                         }

                         cout << "You got " << correct <<" right and " << wrong << " wrong"<< endl<< endl;
                         cin.get();
                        }
                        

Code:
#include <Keypad.h>
 
int sum;
int answer;
int correct = 0;
int wrong = 0;
int menu;
//int i;
//int j;
//long randNumber;
long i;
long j;

const byte ROWS = 4;  // four rows
const byte COLS = 4;  // four columns
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'#','0','*','D'}
};
byte rowPins[ROWS] = {8,1,2,4};  // Connect to the row pinouts of the keypad
byte colPins[COLS] = {3,5,6,7};  // Connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
byte ledPin = 13;

boolean blink = false;
 
void setup()
{
  Serial.begin(9600);    // Set up Serial library at 9600 bps
  pinMode(ledPin, OUTPUT);      // Sets the digital pin as output
  digitalWrite(ledPin, HIGH);   // Sets the LED on
  keypad.addEventListener(keypadEvent);  // Add an event listener for the keypad
}

void loop()
{
  char key = keypad.getKey();
  
  if (key != NO_KEY)
  {
    Serial.println(key);
  }
  
  if (blink)
  {
    digitalWrite(ledPin,!digitalRead(ledPin));
    delay(100);
  }
}

void keypadEvent(KeypadEvent key)
{
  //Serial.println(randomNumber);
    
  Serial.println("  **KIDS MATH");
  Serial.println(" QUIZ GAME!**");
  Serial.println("******MENU******");
  Serial.println("[1] Addition");
  Serial.println("[2] Subtraction");
  Serial.println("[3] Exit");
  Serial.println("Enter Your Choice: ");
  
  switch (key)
  {
    case '1':
            i = random(10);
            j = random(10);
  
            if ((i<=1 && j<=9) || (i<=9 && j<=1))
            {
              Serial.println("What is ");
              Serial.print(i);
              Serial.print(" + ");
              Serial.print(j);
              Serial.print(" = ");
              Serial.println(i + j);
            }
            

Thanks
14  Forum 2005-2010 (read only) / Interfacing / How to write data into SD card? on: June 03, 2010, 11:07:35 pm
Hello! im new in Arduino.
im using Arduino duemilanove and MicroSD module and i want to make a program that can read and write data to the SD card..

the program to read the data from SD card already successful but i do not know how to modify the coding so that it also can write data..
here is the coding to read data;
Code:
#include <WProgram.h>
#include "mmc.h"
#include "microfat.h"

#define NOT_ENABLED 0
#define READ 1
#define WRITE 2

#define EOL '.'  // used to end the package to write to the board
#define EOF 0  // used to mark end of file

byte buffer[512];
int inByte = -1;
int mode = NOT_ENABLED;

int powerSD = 8;

static unsigned long sector, length;

void setup(void)
{
  Serial.begin(115200);
  pinMode(powerSD, OUTPUT);
  digitalWrite(powerSD, HIGH);
}


void loop(void)
{
  if (Serial.available() > 0) {
    inByte = Serial.read();
    switch (inByte) {
    case 'R':
      mode = READ;
      break;
    case 'W':
      mode = WRITE;
      break;
    default:
      mode = NOT_ENABLED;
      break;
    }
  }

  if (mode == READ)
  {
    mmc::initialize();

    if (microfat::initialize(buffer))
    {

      Serial.println("locate");

      if (microfat::locateFileStart("hola.txt", sector, length))
      {
        Serial.println("read");
        if (RES_OK == mmc::readSectors(buffer, sector, 1))
        {
          if (length > BYTESPERSECTOR)
            length = BYTESPERSECTOR;

          for(int i = 0; i < length; ++i)
          {
            Serial.print(buffer[i], BYTE);
          }

        }
      }
    }
    Serial.println();
    Serial.println("Done Reading");
    mode = NOT_ENABLED;
  }
15  Forum 2005-2010 (read only) / Troubleshooting / Re: Tutorial for keypad on: January 23, 2011, 08:07:37 am
Here the link for the picture of the keypad and LCD screen.





Thanks a lot for any help.
Pages: [1] 2