I tried using the above code you mention replacing variables to fit my situation. I'm still unable to store it.
Do have a look at my current code.
Uploading is fine.
char numbuf[9]; //set array to having 8 + 1 digit
void password(char buf[])
{
uint8_t i, a;
randomSeed(analogRead(0));
int randNumber;
Serial.println("Generating Password");
for( i=0; i < 8; i++){
while(i != 8)
{
randNumber = random(10); // Print random numbers with no seed value
buf[i] = (char) randNumber; // this is known as casting an int to a char
i++;
mySerial.print(randNumber);
Serial.println(randNumber);
}
Serial.println(numbuf);
}
}
char numbuf[9]; //set array to having 8 + 1 digit
void password(char buf[])
{
uint8_t i, a; // uint8_t takes longer to think than 'byte' which is the same thing.
randomSeed(analogRead(0));
int randNumber;
Serial.println("Generating Password");
for( i=0; i < 8; i++){
while(i != 8) // i == 8 never happens in this loop, the compiler likely optimizes it out
{
randNumber = random(10); // Print random numbers with no seed value
// don't you mean numbuf, not buf? otherwise declare buf[]
buf[i] = (char) randNumber; // this is known as casting an int to a char
i++;
mySerial.print(randNumber);
Serial.println(randNumber);
}
Serial.println(numbuf); // and here's your extra print
}
}
for( i=0; i < 8; i++){
while(i != 8) // i == 8 never happens in this loop, the compiler likely optimizes it out
The compiler doesn't optimize it out. The for loop index is from i=0 to i<8 which means that 'i' will take on the successive values 0, 1, 2 .. 6, 7.
Why is there a while loop inside the for loop??
while(i != 8) // i == 8 never happens in this loop, the compiler likely optimizes it out
The compiler doesn't optimize it out. The for loop index is from i=0 to i<8 which means that 'i' will take on the successive values 0, 1, 2 .. 6, 7.
Why is there a while loop inside the for loop??
Pete
Also note that i never changes in the while loop. It's an infinite loop.
char numbuf[9]; //set array to having 8 + 1 digit
void password(char buf[])
{
{
byte i,a;
Serial.println("Generating Password");
randomSeed(analogRead(0));
int randNumber;
for(i=0; i<=7; i++){ //allows only 8 Digit for Password
randNumber = random(10);
a = randNumber;
mySerial.print(randNumber);
Serial.println(randNumber);
numbuf[i] = (char) randNumber; // this is known as casting an int to a char
}
Serial.println(numbuf);
buf[i]=0; // reset my password
}
}
Whats' shown in my serial monitor:
Generating Password
8
6
3
4
5
5
6
7
(Serial.Print numbuf) to appear here but it doesnt.
Sending Password
It appears that it is not storing in as when I did my Serial.println(numbuf);
Nothing is displayed here and my code moves along.
GoForSmoke, I have changed buf to numbuf*. Same thing happens as well.*
The ASCII code for 1 as a char is 49. So, if the random number generated is a 1 we need to add 48 to it to get its char value.
ASCII 2 is 50, 3 is 51 etc. See http://www.asciitable.com/ So, adding 48 to an integer between 0 and 9 gives its value as a char.
Adding '\0' to the end of the array ensures that the char array is seen as a string and can be used as such. It does not 'reset' the array, but why do you need to because next time you put 8 chars in it they will overwrite the ones already there.
printableNumber is now 49, ASCII '1'
We can code ASCII values by putting the character in single quotes, and it's an 8 bit signed integer that prints that character in the quotes and can be operated on with math. 'A' + 3 == 'D' just as '0' + 1 == '1'. And the difference between uppercase and lowercase alphas is the lowercase have bit 5 set, 'a' - 'A' == 32.