obtaining value of integer from button.

Hi, just asking on how will i grab the value of the integer when i pushed the button.
i've declared test as char as a container for the value that i needed.
now, when i pressed button, the value of the button should be "button=123"
and for the second button, "button1=456" when i pressed. but i can't get it. :blush:

int button=2;
int button1=3;
int buttonstate=0;
int buttonstate1=0;
char test;
char password={test};
void setup()
{
pinMode(button,INPUT);
}
void loop()
{
buttonstate=digitalRead(button);
buttonstate1=digitalRead(button1);
if(buttonstate==HIGH)
{
test="123";   //This should be the value of the "char password"
}
if(buttonstate1==HIGH)
{
test="456"; //This should be the value of the "char password"
}
}
test="123";   //This should be the value of the "char password"

That won't even compile. You can not store a string (a NULL terminated array of chars) in a char.

Make test an array.

i've change it to

char test[4]="123"

then error occurred invalid conversion from chat* to char

char Password = {test};

then error occurred invalid conversion from chat* to char

Why does that surprise you? You can't store a string in a char!

Sir, what would be the best way to put the value of the "char test[4]="123" >>to>> char password" sorry for my poor knowledge

like this:

int button=2;
int button1=3;
int buttonstate=0;
int buttonstate1=0;
char *test[4];
//char password={test};
void setup()
{
  pinMode(button,INPUT);
}
void loop()
{
  buttonstate=digitalRead(button);
  buttonstate1=digitalRead(button1);
  if(buttonstate==HIGH)
  {
    *test = "123";   //This should be the value of the "char password"
  }
  if(buttonstate1==HIGH)
  {
    *test="456"; //This should be the value of the "char password"
  }
}

Sir, what would be the best way to put the value of the "char test[4]="123" >>to>> char password" sorry for my poor knowledge

You can NOT store a string in a char. Get over it.

In the same way that you had to make test an array, you HAVE to make password an array.

aspirines:
Sir, what would be the best way to put the value of the "char test[4]="123" >>to>> char password" sorry for my poor knowledge

If it had to go in an array the first time...

BulldogLowell:
like this:

int button=2;

int button1=3;
int buttonstate=0;
int buttonstate1=0;
char *test[4];
//char password={test};
void setup()
{
  pinMode(button,INPUT);
}
void loop()
{
  buttonstate=digitalRead(button);
  buttonstate1=digitalRead(button1);
  if(buttonstate==HIGH)
  {
    *test = "123";   //This should be the value of the "char password"
  }
  if(buttonstate1==HIGH)
  {
    *test="456"; //This should be the value of the "char password"
  }
}



sir what about the "char password=*test" :)
is there no other way?

well, you can use strncmp()

example you can try:

char *test[4];
char *test2[4];
//char password={test};
void setup()
{
  Serial.begin(115200);
  *test = "123";
  *test2 = "123";
  Serial.print("comparing "); Serial.print(*test); Serial.print(" to "); Serial.println(*test2);
  Serial.println(strncmp(*test,*test2,3)?"Different":"Same");
  *test = "123";
  *test2 = "113";
  Serial.print("comparing "); Serial.print(*test); Serial.print(" to "); Serial.println(*test2);
  Serial.println(strncmp(*test,*test2,3)?"Different":"Same");
}
void loop()
{
 
}

outputs:

comparing 123 to 123
Same
comparing 123 to 113
Different

so try to put that together...

sir what would be the best way to get the value of the string and put it to the "password" i declared?

when i pressed button 1, the val should be button1="123"
when i pressed button2, the value should be button2="456" and place the value to password="button1 or button2"

BulldogLowell:
well, you can use strncmp()

example you can try:

char *test[4];

char *test2[4];
//char password={test};
void setup()
{
 Serial.begin(115200);
 *test = "123";
 *test2 = "123";
 Serial.print("comparing "); Serial.print(*test); Serial.print(" to "); Serial.println(*test2);
 Serial.println(strncmp(*test,*test2,3)?"Different":"Same");
 *test = "123";
 *test2 = "113";
 Serial.print("comparing "); Serial.print(*test); Serial.print(" to "); Serial.println(*test2);
 Serial.println(strncmp(*test,*test2,3)?"Different":"Same");
}
void loop()
{

}




outputs:



> comparing 123 to 123
> Same
> comparing 123 to 113
> Different



so try to put that together...

sir thank you so much for your effort. i really appreciate it but i still can't reach what i need. right now im using two buttons and each button hold different value, and those value should be store to, e.g. char password="//button1 or button2 value" and im using keypad to access it.

OK.... use your mad skills with this, for example and modify your original sketch... see what transpires!

char *test[4];
char *test2[4];
char *password[4];
void setup()
{
  Serial.begin(115200);
  *test = "123";
  *test2 = "123";
  Serial.print("comparing "); Serial.print(*test); Serial.print(" to "); Serial.println(*test2);
  Serial.println(strncmp(*test,*test2,3)?"Different":"Same");
  *test = "123";
  *test2 = "113";
  Serial.print("comparing "); Serial.print(*test); Serial.print(" to "); Serial.println(*test2);
  Serial.println(strncmp(*test,*test2,3)?"Different":"Same");
  *password = *test;
  Serial.print("Password = "); Serial.println(*password);
}
void loop()
{
 
}