Go Down

Topic: Help with coding... (Read 2616 times) previous topic - next topic

R10_JR

Jul 31, 2016, 09:53 am Last Edit: Aug 18, 2016, 01:39 am by R10_JR Reason: UPDATE!
Hi everyone I'm stuck on keypad, because I want 3 products like 111 beats per minute. So if I press 1 once then it saves it as a variable of 1, if i press it again it saves it in a another variable as 1, etc... Then calculates. The code supplied is what I have so far and kind of explains what is happening, thanks for the help to those who have contributed.

Code V0.0:

"
Code: [Select]

#include "Arduino.h"
#include "Keypad.h"

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
              {'1','2','3'},
              {'4','5','6'},
              {'7','8','9'},
              {'*','0','#'}
            };
byte rowPins[ROWS] = {12, 11, 10, 9}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad

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

int VARone = 0;
int VARtwo = 0 ;
int VARthree = 0;
int One1 = 0;
int Two2 = 0;
int Three3 = 0;
int convert = 0;
int total = 0;
int RUN = 0;
int x = 0;
int input = 0;
int varin = 0;
int count = 0;
int vardp = 0;
int varcalc = 1;

void setup()
{
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(2,INPUT);
  pinMode(5,OUTPUT);
  pinMode(4,OUTPUT);
}

void loop()
{
  // put your main code here, to run repeatedly:
  char key = keypad.getKey();
  
  if (digitalRead(2))
    {
    if( ( RUN ) == ( 0 ) )
    {
      varcalc = 0;
      VARone = 0;
      VARtwo = 0;
      VARthree = 0;
      One1 = 0;
      Two2 = 0;
      Three3 = 0;
      convert = 0;
      total = 0;
      RUN = 1;
    }
    else
    {
      if( ((VARone) == (0)) && ((VARtwo)==(0)) && ((VARthree)==(0)) )
      {
        if(key)
        {
          switch (key)
          {
            case '0':
            One1 = 0;
            varcalc = 0;
            VARone = 1;
            break;
            case '1':
            One1 = 1;
            varcalc = 0;
            VARone = 1;
            break;
            case '2':
            One1 = 2;
            varcalc = 0;
            VARone = 1;
            break;
            case '3':
            One1 = 3;
            varcalc = 0;
            VARone = 1;
            break;
            case '4':
            One1 = 4;
            varcalc = 0;
            VARone = 1;
            break;
            case '5':
            One1 = 5;
            varcalc = 0;
            VARone = 1;
            break;
            case '6':
            One1 = 6;
            varcalc = 0;
            VARone = 1;
            break;
            case '7':
            One1 = 7;
            varcalc = 0;
            VARone = 1;
            break;
            case '8':
            One1 = 8;
            varcalc = 0;
            VARone = 1;
            break;
            case '9':
            One1 = 9;
            varcalc = 0;
            VARone = 1;
            break;
          }
        }
      }
        else
        {
          if( ((VARone) == (1)) && ((VARtwo)==(0)) && ((VARthree)==(0)) )
          {
            if(key)
            {
            switch (key)
            {
              case '0':
              Two2 = 0;
              varcalc = 0;
              VARtwo = 1;
              break;
              case '1':
              Two2 = 1;
              varcalc = 0;
              VARtwo = 1;
              break;
              case '2':
              Two2 = 2;
              varcalc = 0;
              VARtwo = 1;
              break;
              case '3':
              Two2 = 3;
              varcalc = 0;
              VARtwo = 1;
              break;
              case '4':
              Two2 = 4;
              varcalc = 0;
              VARtwo = 1;
              break;
              case '5':
              Two2 = 5;
              varcalc = 0;
              VARtwo = 1;
              break;
              case '6':
              Two2 = 6;
              varcalc = 0;
              VARtwo = 1;
              break;
              case '7':
              Two2 = 7;
              varcalc = 0;
              VARtwo = 1;
              break;
              delay(11);
              case '8':
              Two2 = 8;
              varcalc = 0;
              VARtwo = 1;
              break;
              case '9':
              Two2 = 9;
              varcalc = 0;
              VARtwo = 1;
              break;
            }
          }
        }
        else
        {
          if( ((VARone) == (1)) && ((VARtwo)==(1)) && ((VARthree)==(0)) )
          {
            if(key)
            {
              switch (key)
              {
                case '0':
                Three3 = 0;
                varcalc = 0;
                VARthree = 1;
                break;
                case '1':
                Three3 = 1;
                varcalc = 0;
                VARthree = 1;
                break;
                case '2':
                Three3 = 2;
                varcalc = 0;
                VARthree = 1;
                break;
                case '3':
                Three3 = 3;
                varcalc = 0;
                VARthree = 1;
                break;
                delay(11);
                case '4':
                Three3 = 4;
                varcalc = 0;
                VARthree = 1;
                break;
                case '5':
                Three3 = 5;
                varcalc = 0;
                VARthree = 1;
                break;
                case '6':
                Three3 = 6;
                varcalc = 0;
                VARthree = 1;
                break;
                delay(11);
                case '7':
                Three3 = 7;
                varcalc = 0;
                VARthree = 1;
                break;
                case '8':
                Three3 = 8;
                varcalc = 0;
                VARthree = 1;
                break;
                case '9':
                Three3 = 9;
                varcalc = 0;
                VARthree = 1;
                break;
                delay(11);
              }
            }
          }
        }
      }
    }
  }
    else
    {
    if( ( varcalc ) == ( 0 ) )
    {
      if( ((VARone) == (1)) && ((VARtwo)==(0)) && ((VARthree)==(0)) )
      {
            convert = ((60000)/(One1));
            varcalc = 1;
      }
      else
      {
        if( ((VARone) == (1)) && ((VARtwo)==(1)) && ((VARthree)==(0)) )
        {
          total =  ( ( (One1) * (10) ) + ( (Two2)  ) ) ;
          convert = ( ( 60000 )/ ( total ) );
          varcalc = 1;
        }
        else
        {
          if( ((VARone) == (1)) && ((VARtwo)==(1)) && ((VARthree)==(1)) )
          {
            total = ( ( ( (One1) * (1000) ) + ( (Two2) * (100) ) ) +  ( Three3 ) );
            convert = ( ( 60000 )/ ( total ) );
            varcalc = 1;
          }
          else
          {
            varcalc = 1;
          }
        }
      }
        }
    else
        {
      RUN = 0 ;
      digitalWrite(4,HIGH);
      analogWrite(5,255);
      delay( 11 );
      analogWrite(5,0);
      digitalWrite(4,LOW);
      delay( convert );
        }
    }
}

"

AWOL

#1
Jul 31, 2016, 10:14 am Last Edit: Aug 01, 2016, 09:15 am by AWOL
It's hard to tell what's going on and what's supposed to happen, but I can tell you that all the delays between a "break" and the next "case" will never get executed, so they may as well not be there.
Something that isn't there but should be, is a pair of code tags.

Code: [Select]
if( ((VARone) == (0)) && ((VARtwo)==(0)) && ((VARthree)==(0)) )
(Why) do (you) have (so) many (unnecessary) parentheses?

R10_JR

Hi everyone I updated the post to an extent that if you choose a number i.e. 111 and follow the code it would make sense and special thanks to AWOL for the help and look forward this project working.

AWOL

#3
Aug 01, 2016, 09:12 am Last Edit: Aug 01, 2016, 10:15 am by AWOL
...and now you've edited the original post, so my earlier comments don't make any sense, and you've still not got any code tags.

Good luck.

Code-shortening hint
Code: [Select]
if(key >= '0' && key <= '9')
{
  One1 = key - '0';
  varcalc = 0;
  VARone = 1;
}
etc.

R10_JR


AWOL

#5
Aug 03, 2016, 09:55 pm Last Edit: Aug 03, 2016, 09:57 pm by AWOL
New Update evryone
That's the second time you've posted that, but I see no update.

Edit: {light dawns} You've edited the original post (still no code tags), so nothing makes sense.

Do you want a forum timeout?

TomGeorge

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :)
Everything runs on smoke, let the smoke out, it stops running....

R10_JR

Update: Form and Code. 06/08/2016

TomGeorge

Hi,
PLEASE DO NOT KEEP GOING BACK AND UPDATING/EDITING YOUR FIRST POST.
You are not helping others to help you by changing the initial code you had problems with.

Each time you update YOUR CODE please post a NEW message with your updated code, NOT a message that says 
Quote
New Update evryone
This forum is to help all users, so anyone who had a similar problem to you, SHOULD be able to follow how you solved your problem, by changing the initial post you are removing any hope of this thread being used to help other users.

SO PLEASE NEW MESSAGES EACH TIME, INCLUDING CODE UPDATES.

Thanks..  Tom... :)
Everything runs on smoke, let the smoke out, it stops running....

TomGeorge

Hi,
How does the code know if you are putting in a 1 or 2 or 3 digit number?
Would it be better to store each key press and look for a # press to be the equivalent of ENTER/CALCULATE?

You wouldn't need all these IF statements to check for number of digits and do the calculation?
And you would be able to input any number of digits.

Tom... :)
Everything runs on smoke, let the smoke out, it stops running....

R10_JR

Hi it saves like this for all three variables;

 One1 = key - '0';//First variable
 
 Two2 = key - '0';//Second variable
 
 Three3 = key - '0';//Third variable

R10_JR

#11
Aug 15, 2016, 01:00 am Last Edit: Aug 18, 2016, 01:40 am by R10_JR
This is a new code version of V0.0
Code V0.1:
Code: [Select]

#include "Keypad.h"

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
              {'1','2','3'},
              {'4','5','6'},
              {'7','8','9'},
              {'*','0','#'}
            };
byte rowPins[ROWS] = {12, 11, 10, 9}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad

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

int VARone = 0;
int VARtwo = 0 ;
int VARthree = 0;
int One1 = 0;
int Two2 = 0;
int Three3 = 0;
int total = 0;
int RUN = 0;
int varcalc = 1;

void setup()
{
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(2,INPUT);
  pinMode(5,OUTPUT);
  pinMode(4,OUTPUT);
}

void loop()
  {
    // put your main code here, to run repeatedly:
    char key = keypad.getKey();
    Serial.print(total);
    if (digitalRead(2))//Mode Loop
    {
        varcalc = 0;
        VARone = 0;
        VARtwo = 0;
        VARthree = 0;
        One1 = 0;
        Two2 = 0;
        Three3 = 0;
        total = 0;
}
    else
    {
        //Keypad closed loop
        Serial.print(key);
        if((VARone)==(0))//Test if variable one is equal to 0
        {
          //Get key from 0 - 9 and set it to variable one
          delay(11);
          char key = keypad.getKey();
          if(key != NO_KEY)
          {
            One1 = (key);
            varcalc = 0;
            VARone = 1;
          }
        }
        else
        {
          if(((VARone)==(1))&&((VARtwo)==(0)))//Test variable one is equal to 1, whilst variable two is equal to 0
          {
            //Get key from 0 - 9 and set it to variable two
            delay(11);
            char key = keypad.getKey();
            if(key != NO_KEY)
            {
Two2 = (key);
varcalc = 0;
VARtwo = 1;
}
}
        else
        {
            if(((VARone)==(1))&&((VARtwo)==(1))&&((VARthree)==(0)))//Test variable one and variable two is equal to 1, whilst variable three is equal to 0
            {
//Get key from 0 - 9 and set it to variable three
delay(11);
char key = keypad.getKey();
if(key != NO_KEY)
{
Three3 = (key);
varcalc = 0;
VARthree = 1;
}
            }
        }
if((varcalc)==(0))//Calculation Loop
        {
            if((VARone)==(1))//Runs calculation when there is only one variable
            {
                total = (One1);//Takes one inputted number like; '5'
                varcalc = 1;
            }
            else
            {
                if(((VARone) == (1)) && ((VARtwo)==(1)))//Runs calculation when there is only two variables
                {
                    total =  (((One1)*(10))+((Two2)));//Takes two inputted numbers and calculates like; '60'
                    varcalc = 1;
                }
                else
                {
                    if(((VARone)==(1))&&((VARtwo)==(1))&&((VARthree)==(1)))//Runs calculation when there is only three variables
                    {
                      total = ((((One1)*(100) )+((Two2)*(10)))+(Three3));//Takes three inputted numbers and calculates like; '111'
                      varcalc = 1;
                    }
                    else
                    {
                      varcalc = 1;
                    }
                  }
                }
              }
              else//loop
              {
                  digitalWrite(4,HIGH);
                  analogWrite(5,255);
                  delay(11);
                  analogWrite(5,0);
                  digitalWrite(4,LOW);
                  delay((60000)/(total));//makes a delay, e.g. 60000/111 = 540.540540541ms
              }
         }
    }
}


AWOL

((Why) (have you) (got so (many)) ((unnecessary)parentheses))?

R10_JR

#13
Aug 17, 2016, 12:18 am Last Edit: Aug 18, 2016, 01:42 am by R10_JR
This is a new code version of V0.1
Code V0.2:
Code: [Select]

#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'#','0','*'}
};
byte rowPins[ROWS] = {12, 11, 10, 9}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad

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

int VARone = 0;
int VARtwo = 0 ;
int VARthree = 0;
int One1 = 0;
int Two2 = 0;
int Three3 = 0;
int total = 0;
int RUN = 0;
int varcalc = 1;

void setup()
{
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(2,INPUT);
  pinMode(5,OUTPUT);
  pinMode(4,OUTPUT);
}

void loop()
{
// put your main code here, to run repeatedly:
char key = keypad.getKey();
Serial.println(total);
if (digitalRead(2))//Reset Loop
{
varcalc = 1;
VARone = 0;
VARtwo = 0;
VARthree = 0;
One1 = 0;
Two2 = 0;
Three3 = 0;
total = 0;
}
else
{
if((varcalc)==(1))//Keypad Loop
{
if((VARone)==(0))//Test if variable one is equal to 0
{
//Get key from 0 - 9 and set it to variable one
char key = keypad.getKey();
if(key != NO_KEY)
{
One1 = key;
varcalc = 0;
VARone = 1;
}
}
else
{
if(((VARone)==(1))&&((VARtwo)==(0)))//Test variable one is equal to 1, whilst variable two is equal to 0
{
//Get key from 0 - 9 and set it to variable two
char key = keypad.getKey();
if(key != NO_KEY)
{
Two2 = key;
varcalc = 0;
VARtwo = 1;
}
}
else
{
if(((VARone)==(1))&&((VARtwo)==(1))&&((VARthree)==(0)))//Test variable one and variable two is equal to 1, whilst variable three is equal to 0
{
//Get key from 0 - 9 and set it to variable three
char key = keypad.getKey();
if(key != NO_KEY)
{
Three3 = key;
varcalc = 0;
VARthree = 1;
}
}
}
}
}
            else
{
if((varcalc)==(0))//Calculation Loop
{
if((VARone)==(1))//Runs calculation when there is only one variable
{
total = (One1);//Takes one inputted number like; '5'
varcalc = 1;
}
else
{
if(((VARone) == (1)) && ((VARtwo)==(1)))//Runs calculation when there is only two variables
{
total =  (((One1)*(10))+((Two2)));//Takes two inputted numbers and calculates like; '60'
varcalc = 1;
}
else
{
if(((VARone)==(1))&&((VARtwo)==(1))&&((VARthree)==(1)))//Runs calculation when there is only three variables
{
total = ((((One1)*(100) )+((Two2)*(10)))+(Three3));//Takes three inputted numbers and calculates like; '111'
varcalc = 1;
}
else
{
varcalc = 1;
}
}
}
}
else//loop
{
digitalWrite(4,HIGH);
analogWrite(5,255);
delay(11);
analogWrite(5,0);
digitalWrite(4,LOW);
delay((60000)/(total));//makes a delay, e.g. 60000/111 = 540.540540541ms
}
}
        }
}

TomGeorge

Hi,


Code: [Select]
if((varcalc)==(1))//Keypad Loop
can be
Code: [Select]
if(varcalc==1)//Keypad Loop


Code: [Select]
if(((VARone)==(1))&&((VARtwo)==(1))&&((VARthree)==(1)))

can be

Code: [Select]
if((VARone==1)&&(VARtwo==1)&&(VARthree==1))


Tom..... :)
Everything runs on smoke, let the smoke out, it stops running....

Go Up