I am new to Arduino and need a little help

Hello everyone!

I have this arduino program that writes to the eeprom. This is kind of a static initialization file.
What I need to do is make a C/C++ app that will ask the user for input then, similar to below, write it to the arduino.

I have seen that I can use the serial.h to connect, but that is as far as I have been.

Can anyone maybe show me an example of how to connect to an arduino via usb/serial and then write to EEPROM?

Thank you!

//EEPROM initial setup
//LED will stay on while writing, when program exits, LED will go off

#include <EEPROM.h>
#include "EEPROMAnything.h"

int addr   = 0;

const byte id          = 'a';
const int  potPin      = A3;
const int  dirPin      = 8;
const int  stepPin     = 9;
const int  ledPin      = 13;
const int  minFreq     = 0;
const long maxFreq     = 3500;
const int  minPos      = 0;
const long maxPos      = 1024;


void setup()
{
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  delay(1000);
  
  Serial.print("Writing values to memory...\n");
  digitalWrite(ledPin, HIGH);
  
  //Clear the eeprom
  // write a 0 to all 512 bytes of the EEPROM
  for (int i = 0; i < 512; i++)
    EEPROM.write(i, 0);
  
  //Write device id
  Serial.print("Address is: ");
  Serial.print(addr);
  addr += EEPROM_writeAnything(addr, id);
  Serial.print("   Value is: ");
  Serial.print(id);
  Serial.print('\n'); 
  
  //Write potentiometer pin
  Serial.print("Address is: ");
  Serial.print(addr);
  addr += EEPROM_writeAnything(addr, potPin);
  Serial.print("   Value is: ");
  Serial.print(potPin);
  Serial.print('\n');

  //Write direction pin
  Serial.print("Address is: ");
  Serial.print(addr);
  addr += EEPROM_writeAnything(addr, dirPin);
  Serial.print("   Value is: ");
  Serial.print(dirPin);
  Serial.print('\n');
  
  //Write stepper motor pin
  Serial.print("Address is: ");
  Serial.print(addr);
  addr += EEPROM_writeAnything(addr, stepPin);
  Serial.print("   Value is: ");
  Serial.print(stepPin);
  Serial.print('\n');
  
  //Write led pin
  Serial.print("Address is: ");
  Serial.print(addr);
  addr += EEPROM_writeAnything(addr, ledPin);
  Serial.print("   Value is: ");
  Serial.print(ledPin);
  Serial.print('\n');
  
  //Write minFrequency
  Serial.print("Address is: ");
  Serial.print(addr);
  addr += EEPROM_writeAnything(addr, minFreq);
  Serial.print("   Value is: ");
  Serial.print(minFreq);
  Serial.print('\n');
  
  //Write maxFrequency
  Serial.print("Address is: ");
  Serial.print(addr);
  addr += EEPROM_writeAnything(addr, maxFreq);
  Serial.print("   Value is: ");
  Serial.print(maxFreq);
  Serial.print('\n');
  
  //Write minPosition
  Serial.print("Address is: ");
  Serial.print(addr);
  addr += EEPROM_writeAnything(addr, minPos);
  Serial.print("   Value is: ");
  Serial.print(minPos);
  Serial.print('\n');
  
  //Write maxPosition
  Serial.print("Address is: ");
  Serial.print(addr);
  addr += EEPROM_writeAnything(addr, maxPos);
  Serial.print("   Value is: ");
  Serial.print(maxPos);
  Serial.print('\n');

  //Return address to 0
  addr = 0;
  
  Serial.print("Done writing values to memory");
  delay(200);
  digitalWrite(ledPin, LOW);
}

void loop()
{  
}

Can anyone maybe show me an example of how to connect to an arduino via usb/serial and then write to EEPROM?

Connect to it from what? The Serial Monitor application is able to. You should be able to develop an application that is also able to.

//Write potentiometer pin
  Serial.print("Address is: ");
  Serial.print(addr);
  addr += EEPROM_writeAnything(addr, potPin);
  Serial.print("   Value is: ");
  Serial.print(potPin);
  Serial.print('\n');

Your potPin should be a constant with in the program and thus there is no need to store it in eeprom.

The for loop setting very thing (that you then write values to) to 0 is not only point less (why write 0 to a location and then something else?). Why do 512 loc's when you need 3 or 4?

You should check the chips spec there are only a limited number of writes to a location in eeprom and then IT IS DEAD and you need a new chip.

Mark

Thank you, and I understand about the limited number of writes. But… back to my question,

Connect to it via a command line C/C++ app.

Example:

#include <ARDUINOSTUFFS.H>

int main(int argc, char** argv) {
 //Connect to Arduino and make sure it is available *

 //Get user input

 //Write that input to the EEPROM*

return 0;
}
  • What goes here?

If you could point me to an example or the documentation I will be very thankful. I am not asking you to write it for me, I have just not been able to find the resources.

I think I figured it out. The arduino will typically be communicating via i2c, so when it gets plugged into serial, it will check the serial for say "config" and if it gets that, it will then go into config mode and communicate with my C app.

michaela_elise:
I think I figured it out. The arduino will typically be communicating via i2c, so when it gets plugged into serial, it will check the serial for say “config” and if it gets that, it will then go into config mode and communicate with my C app.

It’s good if you have got it to work but what you have written makes no sense to me in the context of normal Arduino/PC communication. The simplest thing is two lines in setup() like this

Serial.begin(9600); // baud rate
Serial.println("Hello World");

And you can check if there is data available from the PC with

if (Serial.available) .....

You do need to be aware (if you are using your own PC program to talk to the Arduino) that when the Arduino is reset it spends a few seconds in the bootloader code before your sketch starts working.

…R

I2C has nothing to do with serial communication. Are you using the IDE or not?

Mark