Pin Settings

Hello, I’m currently solving the pin settings using the for loop, with loading of pin information from EEPROM.

It can be solved this way on a stand-alone basis, and we probably all agree

for(int i = 0; i <= 5; i++) {
    pinMode(i, OUTPUT);
}

But it would not be me if I did not want to do anything unattractive. Thus:

int vccPins[] = {2, 4, 6};
int digitalPins[] = {3, 5, 7};

for(int i = 0; i <= 2; i++) {
    pinMode(vccPins[i], OUTPUT);
    pinMode(digitalPins[i], OUTPUT);
}

so I should be seized to set even pins as power and odd as data. But I can find the problem here. Attiny 85 will be the ATmega328P with this code. Scanned on the example of a blink, extended by several LEDs.

However, my intention is, for example, to use a web interface. Specify the pins of the device that will be stored in the EEPROM and then work with them like.

int vccPins[10];
int digitalPins[10];

for(int a=0; i <= sizeof(vccPins); i+2) {
    for(int b=1; <= sizeof(vccPins); i++) {
        vccPins[b] = EEPROM.read[a];
    }
}

for(int c=0; i <= sizeof(digitalPins); i+2) {
    for(int d=1; <= sizeof(digitalPins); i++) {
        digitalPins[b] = EEPROM.read[a];
    }
}

for(int i = 0; i <= 2; i++) {
    pinMode(vccPins[i], OUTPUT);
    digitalPins(vccPins[i], OUTPUT);
}

However, I’m not sure if I’m reading data correctly using cycles.

Thank you in advance for your reply

i <= sizeof(vccPins); i+2

Oops

int b=1; <= sizeof(vccPins); i++And again.

how to do it differently?

Otherwise, when loading from EEPROM into the field will this work?

Do yourself a couple of favours:

  1. Print out the value of sizeof vccPins
  2. Read up on the syntax of the for loop

I’m hesitant to post this after reading what AWOL replied while I was writing because I do think it might be best to think through the problem on your own so you’ll be sure to learn this important lesson. However, I don’t think the best practices code to determine the size of an array is immediately obvious and I already have this written out so I’m going to go ahead and post it. Please do still go ahead and write a test sketch that prints the sizeof values and think about why the code I provided works.

This is not correct:

tribalcz:

for(int a=0; i <= sizeof(vccPins); i+2) {

for(int b=1; <= sizeof(vccPins); i++) {
       vccPins[b] = EEPROM.read[a];
   }
}

The type of vccPins is int. On the ATmega328P or ATtiny85, an int is two bytes. That means that sizeof(vccPins) == 2 bytes/int * 10 elements in the array. This means you’re writing outside the bounds of the vccPins array and corrupting some random memory, which could cause all sorts of problems.

Here’s the correct way to determine the number of elements in an array of any type:

sizeof(vccPins) / sizeof(vccPins[0])

I recommend using that code even when you have an array that uses a one byte type. The reason is that you might later change the type of the array, or use the code on a different architecture where the type is not one byte. If you use the code above, it will always work no matter what happens. The calculation will be done at compile time so there is no overhead for this code.

You have a number of errors that indicate a lack of basic understanding:

‘sizeof’ gives a size in bytes, not in elements. Your arrays have 10 elements but 20 bytes. Are there always the same number of VCC and ‘digital’ pins? If so, you might want to put that first:

const byte PinCount = 10;
int vccPins[PinCount];
int digitalPins[PinCount];

Then you can use the constant ‘PinCount’ in your loops.

  for (int a = 0; i <= sizeof(vccPins); i + 2)
  {
    for (int b = 1; <= sizeof(vccPins); i++)

In your ‘for’ loops it looks like you intend to use ‘a’ and ‘b’ as your loop variables but then you test and increment ‘i’ instead?!? Also, the ‘increment’ “i + 2” doesn’t do anything! If you are counting by 2’s you would use “i += 2” which is a shortcut way of saying “i = i + 2”.

EEPROM.read is a FUNCTION, not an ARRAY. Use () around the argument, not square brackets.

Arrays start at index 0 so the highest index is size-1. In general you would use a loop like:

for (i=0; i < size; i++)

instead of

for (i=0; i <= size-1; i++)

It is an error to use:

for (i=0; i <= size; i++)

because that would go off the end of the array.

It looks like what you wanted to do was load 20 pin numbers from EEPROM and set them all as outputs. This can be done in a single loop:

#include <EEPROM.h>

const byte PinCount = 10;
int vccPins[PinCount];
int digitalPins[PinCount];

void setup()
{
  for (int i = 0; i < PinCount; i++)
  {
    vccPins[i] = EEPROM.read(i * 2); // Even entries
    pinMode(vccPins[i], OUTPUT);

    digitalPins[i] = EEPROM.read(i * 2 + 1); // Odd entries
    pinMode(digitalPins[i], OUTPUT);
  }
}

void loop() {}

johnwasser:
You have a number of errors that indicate a lack of basic understanding:

Then you can use the constant ‘PinCount’ in your loops.

  for (int a = 0; i <= sizeof(vccPins); i + 2)

Still, oops2

Yes, you are right in the knowledge gap. Originally I'm a php programmer, not C. I'm still learning and I'm more or less sliding on the surface, but I'm putting up the theory that practice is the best teacher.

What is the size of the function, I tried to search the internet, and asked how to get the number of elements in EEPROM memory. The result was that using sizeof would be a solution.

What is the size of the function, I tried to search the internet, and asked how to get the number of elements in EEPROM memory. The result was that using sizeof would be a solution.

See reply #4 for the correct way to use sizeof to determine the number of elements in an array

UKHeliBob:
See reply #4 for the correct way to use sizeof to determine the number of elements in an array

I've been looking at it, and when I'm home, I'm so smart. However, if I still can. Whether this is the right way to load all EEPROM data into the array.

byte count = 10;
int address = 0;
int data[count];

EEPROM.get(address, data);