[SOLVED] Store a number from a variable to to another, then take a new number from the same variable, then put it to another one

The title sound confusing, I know.
So, basically I want to take a number from a variable (let's call it First), then put the number to another varialbe (let's call this one FirstNumber) and do the same thing but get a different number from First and put it into another one (let's call the another one SecondNumber).
I hope you'll understand this.

nope.. :slight_smile:

what will change the first variable?
to store multiple incoming data you could use arrays and increase the index every time you store a new entry. Make sure to not overflow the array

The First variable is taking the number from an IR remote keypad.
So, if you press 1, the First variable will change to one, etc.

OK then use an array

int myDataCollection[10];
size_t dataIndex = 0;
int myVariable;

void setup() {
  // get the first variable
  myVariable = random(100);
  // store it into the array
  myDataCollection[dataIndex++] = myVariable;

  // get the second variable
  myVariable = random(100);
  // store it into the array at the next position
  myDataCollection[dataIndex++] = myVariable;
}

void loop() {}

As mentioned Make sure to not overflow the array

I would suggest to study Serial Input Basics to see how asynchronous data can be collected as it comes and stored into an array.

Thanks for the reply, I'll look into it and I'll let you know if it worked.

Hi, I have a problem I don't know where have I got it wrong.
Heres the error:

error: 'myDataCollection' was not declared in this scope
   KeypadN[dataIndex++] = myDataCollection;
                          ^~~~~~~~~~~~~~~~
exit status 1
'dataIndex' was not declared in this scope

I put this in setup:

int myDataCollection[10];
size_t dataIndex = 0;

And this in loop:

KeypadN[dataIndex++] = myDataCollection;
KeypadN[dataIndex++] = myDataCollection;

KeypadN is the "First" variable and from what I understood, myDataCollection is where the first and the second number is coming to.

Don't post snippets (Snippets R Us!)

myDataCollection is the array, so it's not the value... look at the code I had written

myDataCollection[dataIndex++] = myVariable;

I see, but where do I put the input and where is it sending the numbers to? In myVariable or dataIndex?

post your code

#include <IRremote.h>

int IR = 2;

int KeypadN;
int Number1;
int Number2;

IRrecv irRecriver(IR);
decode_results answer;

void setup() {
  int myDataCollection[10];
  size_t dataIndex = 0;

  irRecriver.enableIRIn();
}

void loop() {
  if(irRecriver.decode(&answer)){
    Serial.println(answer.value, HEX);
    irRecriver.resume();
    switch(answer.value){
      case 0xFF6897: //0
       KeypadN = 0;
       break;
      case 0xFF30CF: //1
       KeypadN = 1;
       break;
      case 0xFF18E7: //2
       KeypadN = 2;
       break;
      case 0xFF7A85: //3
       KeypadN = 3;
       break;
      case 0xFF10EF: //4
       KeypadN = 4;
       break;
      case 0xFF38C7: //5
       KeypadN = 5;
       break;
      case 0xFF5AA5: //6
       KeypadN = 6;
       break;
      case 0xFF42BD: //7
       KeypadN = 7;
       break;
      case 0xFF4AB5: //8
       KeypadN = 8;
       break;
      case 0xFF52AD: //9
       KeypadN = 9;
       break;
  KeypadN[dataIndex++] = myDataCollection;
  KeypadN[dataIndex++] = myDataCollection;
  Serial.println(Number1);
  Serial.print(Number2);
  delay(100);
}

the IR library has changed, you should move to the new API (resume() should be called at the end after you are done dealing with the results) and the data collection should be outside the switch/case

try this (typed here - so may be some bugs)

#include <IRremote.h>
const byte IR_RECEIVE_PIN = 2;

const size_t maxKeys = 5;
size_t currentKeyIndex = 0;
char keys[maxKeys + 1]; // global variables are initialized with 0 and we allocated 1 extra byte to mark the end of the cString

void setup() {
  Serial.begin(115200);
  IrReceiver.begin(IR_RECEIVE_PIN, DISABLE_LED_FEEDBACK);
  Serial.println(F("Ready"));
}

void loop() {
  if (IrReceiver.decode()) {  // results are in IrReceiver.decodedIRData
    if (((IrReceiver.decodedIRData.flags & IRDATA_FLAGS_IS_REPEAT) == 0) && (IrReceiver.decodedIRData.command != 0)) {
      Serial.print(F("Command: 0x"));
      Serial.println(IrReceiver.decodedIRData.command, HEX);
      char matchingKey = '\0';
      switch (IrReceiver.decodedIRData.command) {
        case 0xFF6897: matchingKey = '0'; break;
        case 0xFF30CF: matchingKey = '1'; break;
        case 0xFF18E7: matchingKey = '2'; break;
        case 0xFF7A85: matchingKey = '3'; break;
        case 0xFF10EF: matchingKey = '4'; break;
        case 0xFF38C7: matchingKey = '5'; break;
        case 0xFF5AA5: matchingKey = '6'; break;
        case 0xFF42BD: matchingKey = '7'; break;
        case 0xFF4AB5: matchingKey = '8'; break;
        case 0xFF52AD: matchingKey = '9'; break;
        default: matchingKey = '?';
      }
      if (currentKeyIndex < maxKeys) keys[currentKeyIndex++] = matchingKey;
    }
    IrReceiver.resume();
  }

  if (currentKeyIndex >= maxKeys) {
    // got all of them
    Serial.println(F("GOT: "));
    Serial.println(keys); // will print the cString. as we have allocated 1 extra byte we know it's null terminated
    Serial.println(F("Demo ended"));
    while (true) yield(); // die here
  }
}

you should double check the commands received as they probably no longer match what you had in your cases so you'll likely see ????? printed at the end

Hi, so I've replaced the old HEX values with new ones, and it's now working really well (I also changed the Serial.begin from 115200 baud to 9600 baud).
Thanks for you help :slight_smile:

Also, I have another question.
I want to loop the code and not make it a 1 cycle
When I get the numbers, the code "dies" (as you said "//die here").

Replace this line:

while (true) yield(); // die here

with this:

currentKeyIndex = 0;

Hi, thanks for your reply.
This work perfectly.

that's not a great idea unless you have a Bluetooth interface requiring 9600... Why would you go slow?

I prefer prefer 9600 baud
I use it in every single project I make.

Any scientific rationale?

I tend to use 115200 for examples I post and 500.000 or 1.000.000 or sometimes even 2.000.000 in my own codes (which I still find reliable).

At 9600 bauds emitting or receiving bytes slows your arduino down 12 times compared to 115200 bauds. This can have unintended consequences like saturating quickly your output buffer which will make calls to print() blocking for example and then your debug statements start creating other timing bugs in your code...

9600 is something of the past, when the first Arduino could not reliably talk faster with the host PC... but it's really the past, so I would advise against it.

Hi, I didn't know it would change that much stuff.
I'll be using 115200 baud from now on.
Thanks for the advice

You will have to read up on scope. The variables declared in setup() are only known inside setup() and nowhere else.