Peer-graded Assignment: Peer Assignment: EEPROM Sketch Answer

Peer-graded Assignment Peer Assignment EEPROM Sketch Answer


Peer-graded Assignment: Peer Assignment: EEPROM Sketch Answer 


In this article i am gone to share Coursera Course: Interfacing with the Arduino Week 3 Peer-graded Assignment: Peer Assignment: EEPROM Sketch Answer with you.


Peer-graded Assignment: Peer Assignment: EEPROM Sketch

Instructions

Write a sketch that allows a user to access data in EEPROM using the serial monitor.


Review criteria

Write a sketch that allows a user to access data in EEPROM using the serial monitor. In the serial monitor the user should be able to type one of two commands: “read” and “write. "Read" takes one argument, an EEPROM address. "Write" takes two arguments, an EEPROM address and a value. 

For example, if the user types “read 3” then the contents of EEPROM address 3 should be printed to the serial monitor. If the user types “write 3 10” then the value 10 should be written into address 3 of the EEPROM. 

In order to grade this assignment, you will perform a simulation on the website www.tinkercad.com. You will need to create an account for free. There are instructional videos on that website that will teach you how to use the simulator.

See the details on the Course Overview page regarding purchasing the hardware and/or using a web-based simulator. 


My submission

Screenshot
Peer-graded Assignment Peer Assignment EEPROM Sketch Answer

____________________________________________________________________________

Source Code..

#include <EEPROM.h>
void setup() 
{
  Serial.begin(9600);
}
int address;
int dataToWrite;

void loop() 
{
  String buffer = "";
  buffer = Serial.readString();
  if(buffer.startsWith("read")) {
    address = buffer.substring(buffer.indexOf(' ') +1).toInt();
    Serial.print("Data from the Address : ");
    Serial.print(address);
    Serial.println();
    Serial.println(EEPROM.read(address)); 
  } else if(buffer.startsWith("write")) {
    address = buffer.substring(6,7).toInt();
    dataToWrite = buffer.substring(8).toInt();
    Serial.print("Written ");
    Serial.print(dataToWrite);
    Serial.println();
    Serial.print("to the Address: ");
    Serial.print(address);
    Serial.println();
    EEPROM.write(address,dataToWrite); 
  }
}
____________________________________________________________________________

Simulation

Write a sketch that allows a user to access data in EEPROM using the serial monitor. In the serial monitor the user should be able to type one of two commands: “read” and “write. "Read" takes one argument, an EEPROM address. "Write" takes two arguments, an EEPROM address and a value.

For example, if the user types “read 3” then the contents of EEPROM address 3 should be printed to the serial monitor. If the user types “write 3 10” then the value 10 should be written into address 3 of the EEPROM.


# Author: Niam Moltta
#include <EEPROM.h>

String command;

void setup() {
  
  Serial.begin(9600);
  
}

void loop() {

  if (Serial.available() > 0) {
    
    command = Serial.readStringUntil('\n'); 
    String commandRead = command;
    String commandWrite = command;
    String sRead = "read";
    String sWrite = "write";
    commandRead.remove(4);
    commandWrite.remove(5);

    if (commandRead.equals(sRead)) {
      
      String sreadArg1 = command.substring(5);
      
      int readArg1 = sreadArg1.toInt();
      
      if (readArg1 > 1023 || readArg1 < 0) {
        
        Serial.println("Invalid, please enter a value from 1 to 1023");
      
      }

      else { 
        int valueEEP = EEPROM.read(readArg1); 
        Serial.print("The value is ");
        Serial.println(valueEEP, DEC);
      }
    }

    if (commandWrite.equals(sWrite)) { 
      
      String swriteArgs = command;
      
      swriteArgs.remove(0, 6);
      
      int swriteSpace = swriteArgs.indexOf(' ');
      
      String swriteArg1 = swriteArgs.substring(0, swriteSpace);
      
      int writeArg1 = swriteArg1.toInt(); 
      
      String swriteArg2 = swriteArgs.substring(swriteSpace + 1); 
      
      int writeArg2 = swriteArg2.toInt(); 

      if (writeArg1 < 0 || writeArg1 > 1023 || writeArg2 < 0 || writeArg2 > 255) {
        
        Serial.println("Invalid, enter a first number from 0 to 1023 and the second number from 0 to 255");
        
      }

      else { 
        byte byte1Arg2 = writeArg2 & 0XFF;
        
        byte byte2Arg2 = (writeArg2 >> 8) & 0XFF;
        
        EEPROM.write(writeArg1, byte1Arg2);
        
        EEPROM.write(writeArg1 + 1, byte2Arg2);
        
        Serial.println("Success!");
        
   }
  }
 }
}


____________________________________________________________________________

Interfacing with the Arduino Module 1 Quiz Answer











Post a Comment

0 Comments