0

This program should prompt the user for an encryption key to be input through the serial monitor. The encryption key contains 16 characters. However, the message that will be encrypted can be up to 128 characters. As such, when storing the encryption key in the EEPROM, you should repeat it so that it fills locations 0-127.

in the code, I'm trying to repeat the 16 character key 8 times

volatile bool prese_button = false;
String Password;
int counter = 8;
boolean right_pass;
void setup() {
  DDRD &= 0b11111011;
  PORTD |=0b00000100;
  EICRA |= 0b00000010;
  EIMSK |= 0b00000001;
  Serial.begin(9600);
}

void loop() {
  Serial.println("Please enter a 127 character password:"); 
  while (Serial.available() == 0) {}  //wait for serial terminal to be ready 
  Password = Serial.readString(); 
  Serial.print("your password is: ");  //just to verify it was read correctly 
  Serial.println(Password);
  for ( int j = 0 ;j < 128; j++) //   28 char 
  {
    for (int i = 0; i<16 ; i++) {     //16 char
      EEPROM_write ( (j), Password[i] );
    }
  }
}
2
  • And what exactly is the problem? – chrisl Apr 14 '20 at 19:23
  • I don't think your code does what you've described. But what exactly are you asking? – StarCat Apr 14 '20 at 19:43
1
   for ( int j = 0 ;j < 128; j++) //   28 char 
  {
    for (int i = 0; i<16 ; i++) {     //16 char
      EEPROM_write ( (j), Password[i] );
    }
  }

In these two for loops you are writing 128 times 16 characters. That seems more than you described in your post. If you have 16 things and need to repeat to fill 128 slots then you need 128 / 16 = 8 repeats of the 16 character sequence. It's just basic math. So that outer loop should count to 8, not 128.

for ( int j = 0 ;j < 8; j++) //  8 repeats of the 16 char 
      {
        for (int i = 0; i<16 ; i++) {     //16 char
          EEPROM_write ((i + (16*j)), Password[i] );
        }
      }
1
  • Yes, and you'd have to calculate the address. I'll add an example to the answer. – Delta_G Apr 14 '20 at 20:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.