assisting some code i am helping school with

hi guys, i have had quite a bit of experience with arduinos, and subsequent to this revelation at school, i have offered to help them with their work.

now, a peer of mine is attempting to create a safe, and he needs someone to write the code for it to lock and unlock with a certain code. the code he had didnt work, and i just attempted to add to it to try and fix it.

so basically, the buttons can be registered and do come up on serial monitor, to get a distinct code to unlock the safe, i let a variable called BODMASS determine it. i had a variable, called variable to give it a value. when the 1 number is pressed, variable becomes 4, when 4 is pressed, variable becomes 4X3, when 2 is pressed, variable becomes 12/2=6, and when 5 is pressed, variable becomes 6-6 = 0. when number six is pressed, i created an if statement to change the variable “var” from 0 to 1, changing the switch statement. my problem is that the arduino doesnt even acknowledge the for statement inside the switch statement where button 6 is pressed. any ideas why? after i get that working, the rest of the code should be easy to complete.

for those who are confused, this is my problem:

case 5:

Serial.println(5);
variable = variable - 6;
BODMASS = variable;
Serial.println(BODMASS);
if (BODMASS = 0){
var = 1;
}

break;

in this set of code, everything works, the button is pressed and the button number is recognized, after pressing the code 2415, BODMASS becomes zero as seen in serial monitor. but when i use an if statement for when it is equal to zero, nothing is recognized, even if i do something like:
case 5:

Serial.println(5);
variable = variable - 6;
BODMASS = variable;
Serial.println(BODMASS);
if (BODMASS = 0){
Serial.println(“1234567”);
}

break;

please help me figure out why it is not recognized. thanks.

rest of code:

int BODMASS;
int vara = 5;
int var = 0;
int variable;
byte h=0,v=0;    //variables used in for loops

const unsigned long period=50;  //little period used to prevent error

unsigned long kdelay=0;        // variable used in non-blocking delay

const byte rows=4;             //number of rows of keypad

const byte columns=4;            //number of columnss of keypad

const byte Output[rows]={2,3,4,5}; //array of pins used as output for rows of keypad

const byte Input[columns]={6,7,8,9}; //array of pins used as input for columnss of keypad

byte keypad() // function used to detect which button is used

{

static bool no_press_flag=0;  //static flag used to ensure no button is pressed

 for(byte x=0;x<columns;x++)   // for loop used to read all inputs of keypad to ensure no button is pressed

 {

    if (digitalRead(Input[x])==HIGH);   //read evry input if high continue else break;

    else

    break;

    if(x==(columns-1))   //if no button is pressed

    {

     no_press_flag=1;

     h=0;

     v=0;

    }

 }

 if(no_press_flag==1) //if no button is pressed

 {

   for(byte r=0;r<rows;r++) //for loop used to make all output as low

   digitalWrite(Output[r],LOW);

   for(h=0;h<columns;h++)  // for loop to check if one of inputs is low

   {

     if(digitalRead(Input[h])==HIGH) //if specific input is remain high (no press on it) continue

     continue;

     else    //if one of inputs is low

     {

         for (v=0;v<rows;v++)   //for loop used to specify the number of row

         {

         digitalWrite(Output[v],HIGH);   //make specified output as HIGH

         if(digitalRead(Input[h])==HIGH)  //if the input that selected from first sor loop is change to high

         {

           no_press_flag=0;                //reset the no press flag;

           for(byte w=0;w<rows;w++) // make all outputs as low

           digitalWrite(Output[w],LOW);

           return v*4+h;  //return number of button

         }

         }

     }

   }

 }

return 50;

}

void setup()

{

 for(byte i=0;i<rows;i++)  //for loop used to make pin mode of outputs as output

 {

 pinMode(Output[i],OUTPUT);

 }

 for(byte s=0;s<columns;s++)  //for loop used to makk pin mode of inputs as inputpullup

 {

   pinMode(Input[s],INPUT_PULLUP);

 }

 Serial.begin(9600); //to use serial monitor we set the buad rate

}

void loop()
{
 switch (var) {
   case 0:

 if(millis()-kdelay>period) //used to make non-blocking delay

 {

   kdelay=millis();  //capture time from millis function

switch (keypad())  //switch used to specify which button

  {
           case 14:

           Serial.println(1); //2
           variable = 4;
           BODMASS = variable;
          
           Serial.println(BODMASS);  
      break;
           case 5:

          
           Serial.println(5);
           variable = variable - 6;
BODMASS = variable;
          Serial.println(BODMASS);
         if (BODMASS = 0){
var = 1;
}
          
  
          
      break;

           case 6:

          
           Serial.println(4); //6
           variable = variable * 3;
BODMASS = variable;
           Serial.println(BODMASS);
      break;

          
           case 13:

           Serial.println(2); //0

           variable = variable / 2;
           BODMASS = variable;
           Serial.println(BODMASS);
          
         
      break;
          


}
break;
}

break;

case 1:


break;


 }
}

Ok you might be king at school but you need to read the how to use the forum sticky post to learn how to post code and to ask questions.

The words make no sense and why is the code on every other line, and what is the crossing out in the code showing?

Hi,

Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Tom… :slight_smile:

  if (BODMASS = 0){

Oops.

(Why we use code tags - great example.)

Hi, thanks for the fast replies.

my apologies, i am in a rush to get this working, beleive me, if i took my time with this code, it would look much better, but ive got around 3 peoples codes to get working, plus i have exams to prepare for, so im sure you could understand the stress i am under.

here is the code, i am unsure why the code was posted with lines through it… hopefully how i have posted it now will get rid of that.

oh i am no king of programming, i just have a basic idea of how it works, probably terrible compared to everone here haha!

anyways, i just wanna know what i have done wrong, i want to know why the if statement wont work.

thanks

*this keypad tutorial number one by omar tarek 29-06-2014 */
 
int BODMASS;
int vara = 5;
int var = 0;
int variable;
byte h=0,v=0;    //variables used in for loops
 
const unsigned long period=50;  //little period used to prevent error
 
unsigned long kdelay=0;        // variable used in non-blocking delay
 
const byte rows=4;             //number of rows of keypad
 
const byte columns=4;            //number of columnss of keypad
 
const byte Output[rows]={2,3,4,5}; //array of pins used as output for rows of keypad
 
const byte Input[columns]={6,7,8,9}; //array of pins used as input for columnss of keypad
 
byte keypad() // function used to detect which button is used
 
{
 
static bool no_press_flag=0;  //static flag used to ensure no button is pressed
 
  for(byte x=0;x<columns;x++)   // for loop used to read all inputs of keypad to ensure no button is pressed
 
  {
 
     if (digitalRead(Input[x])==HIGH);   //read evry input if high continue else break;
 
     else
 
     break;
 
     if(x==(columns-1))   //if no button is pressed
 
     {
 
      no_press_flag=1;
 
      h=0;
 
      v=0;
 
     }
 
  }
 
  if(no_press_flag==1) //if no button is pressed
 
  {
 
    for(byte r=0;r<rows;r++) //for loop used to make all output as low
 
    digitalWrite(Output[r],LOW);
 
    for(h=0;h<columns;h++)  // for loop to check if one of inputs is low
 
    {
 
      if(digitalRead(Input[h])==HIGH) //if specific input is remain high (no press on it) continue
 
      continue;
 
      else    //if one of inputs is low
 
      {
 
          for (v=0;v<rows;v++)   //for loop used to specify the number of row
 
          {
 
          digitalWrite(Output[v],HIGH);   //make specified output as HIGH
 
          if(digitalRead(Input[h])==HIGH)  //if the input that selected from first sor loop is change to high
 
          {
 
            no_press_flag=0;                //reset the no press flag;
 
            for(byte w=0;w<rows;w++) // make all outputs as low
 
            digitalWrite(Output[w],LOW);
 
            return v*4+h;  //return number of button
 
          }
 
          }
 
      }
 
    }
 
  }
 
return 50;
 
}
 
void setup()
 
{
 
  for(byte i=0;i<rows;i++)  //for loop used to make pin mode of outputs as output
 
  {
 
  pinMode(Output[i],OUTPUT);
 
  }
 
  for(byte s=0;s<columns;s++)  //for loop used to makk pin mode of inputs as inputpullup
 
  {
 
    pinMode(Input[s],INPUT_PULLUP);
 
  }
 
  Serial.begin(9600); //to use serial monitor we set the buad rate
 
}
 
void loop()
{
  switch (var) {
    case 0:
 
  if(millis()-kdelay>period) //used to make non-blocking delay
 
  {
 
    kdelay=millis();  //capture time from millis function
 
switch (keypad())  //switch used to specify which button
 
   {
            case 14:
 
            Serial.println(1); //2
            variable = 4;
            BODMASS = variable;
           
            Serial.println(BODMASS);  
       break;
            case 5:
 
           
            Serial.println(5);
            variable = variable - 6;
BODMASS = variable;
           Serial.println(BODMASS);
          
           
   
           
       break;
 
            case 6:
 
           
            Serial.println(4); //6
            variable = variable * 3;
BODMASS = variable;
            Serial.println(BODMASS);
       break;
 
           
            case 13:
 
            Serial.println(2); //0
 
            variable = variable / 2;
            BODMASS = variable;
            Serial.println(BODMASS);
           
          
       break;
           
 
 
}
break;
}
 
break;
 
case 1:
 
 
break;
 
 
  }
}

i want to know why the if statement wont work.

What if statement? You have removed the one for case 5 which you originally asked about and AWOL told you the error.

beleive me, if i took my time with this code, it would look much better,

So how much time does it take you to select "Auto Format" from the tools menu?

hi, i have no idea, i am unsure what the original writer of code meant, i am just attempting to add to it.

Grumpy_Mike,

i apologise, it is supposed to be there.

i failed to see the correction he made. can you please explain why is wasnt working?

thanks

So adding to code that doesn't work is not going to be a very fruitful experience.

You have to understand what you are trying to do and how the originator is trying to do it.

I assume you are doing these other kids homework for them. While it might get you popularity points it is cheating and you are complicit in it.

i failed to see the correction he made.

He pointed out you only had one equals sign in that if statement.

it is supposed to be there

Check it is gone now.

Next time you post format it correctly and remove all the unintentional blank lines.

AAAAAAHHHHHHHH oh my god i cant beleive i did that. im so embarrased, i cant beleive i forgot that :o :o :o :o :o :o

thank you guys, that should be all haha! im so thankfull, thank you for helping, and embarrasing me HAHA!

have a lovely evening, knowing that you lifted the spirits of both myself and the person i am helping.

thank you!

i am unsure why the code was posted with lines through it..

Because you forgot to post the code using [code][/code] tags.

(It would've been more fun if you'd used "u", "b", "sup" or "sub" as your array indices)

Yep ok thanks, i made the correction.

have a lovely evening