I need help with my code

So here’s the deal, i have like 6 buttons (reed swiches) on track and i have imagined it til now as the only way for the board to work the way i want it would be the code i stored the serial.writeln() to the serial monitor everytime the input is HIGH
here’s the code

int premiereed=7;
int secondereed=8;
int positionus [] = {1,2,3,4,5,6};

void setup(){
  pinMode(premiereed, INPUT);
  pinMode(secondereed, INPUT);
  Serial.begin(9600);
}
void loop(){
 
  if (digitalRead(premiereed)==HIGH){
    Serial.println(positionus [0]);
  }
  else {}
  if (digitalRead(secondereed)==HIGH){
    Serial.println(positionus [3]);
  }
  else {}
//this was for testing only, if the read thingy will work then it will be changed to the real values 
  if ( positionus [6] - positionus [1] <= 2 ) {
    Serial.println("this will be changed with the code to set the red light on for the 2nd train");
  }
  else {}
    delay(50);
}

so to be more specific, what i need is a way to find the last value added to the serial monitor and one before the last one (in array would be perfect)

For the record:
I have 2 trains equipped with 2 magnets under (1 for each), so the need for the penultimate is required for train emergencies, if they approach the red led will shine and make the train stay there for a moment until the other train moves at least 2 (kinda) neighborhoods away.
This is why i need to get the penultimate value written to the serial monitor

You never need to do this:

 else {}

C/C++ array indexes begin at 0. This points outside of the array:

positionus [6]

The values in your array are never indexed by anything except constants.

It's hard to guess what you are actually trying to do.

aarg:
You never need to do this:

  else {}

I only added the condition in case i needed it to do it something else if no response was given :stuck_out_tongue:

B4rdh:
I only added the condition in case i needed it to do it something else if no response was given :stuck_out_tongue:

It's best not to clutter a program with unused code. Especially when it doesn't work as expected.

aarg:
It's hard to guess what you are actually trying to do.

here's a quick demonstration of what i mean :stuck_out_tongue:
...
Serial.println("foo");
}
...
Serial.println("bar");
}
...

...
/and the code i need would be here/
Serial.println("the penultimate value:");
Serial.print(penultimate);
Serial.println("the last value:");
Serial.print(lastvalue);

...
//and the serial monitor:
foo
bar
the penultimate value:foo
the last value:bar

Are the last and previous values numbers or character strings like "foo" and "bar". If they are character strings, are they the same length? What is the max length?

You will need two variables lastValue and penultimateValue. You save the last statement into lastValue, do what you need to do with the two items, and then assign the lastValue to penultimateValue.

It's somewhat like a millis() timer where you have the current value, and the previous value and the current value becomes the previous value.

I'm not sure why you want to work with the serial output, instead of the underlying reed switch id or value associated with it?

cattledog:
Are the last and previous values numbers or character strings like “foo” and “bar”. If they are character strings, are they the same length? What is the max length?

hi, the values are the positionus contents (i.e positionus[1] = 2)
which is what it does

if ( positionus [5] - positionus [1] <= 2 ) I.E if ( 6 - 2 <= 2 )

cattledog:
I’m not sure why you want to work with the serial output, instead of the underlying reed switch id or value associated with it?

I’m a noob in C & C++ :stuck_out_tongue:
(just got bored with PHP, so coding is not news to me :P)
so by the brief look i gave to arduino i could not really find another successful way to save something momentarily without the need of an SD card

Use a two element array.

int lastvalues[2] = { -1, -1};

if (condition == true)
{
  lastvalues[1] = lastvalues[0];
  lastvalues[0] = positionus[somevalue];
  Serial.println(lastvalues[0]);
}

lastvalues[0] contains the last value, lastvalues[1] contains the one before that.

Array is initialised with minus ones to indicate invalid values. You can check for that if needed.

You have to fill in the conditions :wink:

sterretje:
Use a two element array.

int lastvalues[2] = { -1, -1};

if (condition == true)
{
  lastvalues[1] = lastvalues[0];
  lastvalues[0] = positionus[somevalue];
  Serial.println(lastvalues[0]);
}



lastvalues[0] contains the last value, lastvalues[1] contains the one before that.

Array is initialised with minus ones to indicate invalid values. You can check for that if needed.

You have to fill in the conditions ;)

cheers for that, i am sure that i'll come in use in the future :slight_smile:


but what i really (and kinda urgently too) is for something that i've missed, i need for some piece of code that reads the last added value to the serial monitor and the one before that, so i could later divide them if condition is met

i really hope someone will help me out with this, it was demanded of me and if i will not deliver it many things will be at stake on my end :frowning:

B4rdh:
cheers for that, i am sure that i'll come in use in the future :slight_smile:


but what i really (and kinda urgently too) is for something that i've missed, i need for some piece of code that reads the last added value to the serial monitor and the one before that, so i could later divide them if condition is met

You can't get information back from the serial monitor. But you don't have to. You printed it to the serial monitor in your code, so you can just as easily save whatever you told it to print into a variable. Then you have the last thing you told the serial monitor to print.

It's like if you were talking to me on the phone and said you needed me to tell you the last two things that you said to me. I'd say, "you said it, don't you remember what you said?" You don't need to get information back from the serial monitor, you just need to remember what you sent to it.

B4rdh:
i really hope someone will help me out with this, it was demanded of me and if i will not deliver it many things will be at stake on my end :frowning:

Sounds like a tough situation. But what you ask is truly impossible.

Delta_G:
You can't get information back from the serial monitor. But you don't have to. You printed it to the serial monitor in your code, so you can just as easily save whatever you told it to print into a variable. Then you have the last thing you told the serial monitor to print.

It's like if you were talking to me on the phone and said you needed me to tell you the last two things that you said to me. I'd say, "you said it, don't you remember what you said?" You don't need to get information back from the serial monitor, you just need to remember what you sent to it.

Sounds like a tough situation. But what you ask is truly impossible.

so could you send me a link of an example or tutorial so i could try it on my own code
(the save value into variable)

B4rdh:
so could you send me a link of an example or tutorial so i could try it on my own code
(the save value into variable)

What do you mean? A tutorial on how to save a value into a variable? What value? It's just an assignment, like:

int i = 4;

puts the value 4 into the variable i.

Or perhaps:

Serial.print(someIntVariable);
int theLastThingIPrinted = someIntVariable;

I'm not sure I understand what part is confusing you.

int lastValue = 0;
int previousLastValue = 0;
int difference = 0;

void setup() {
  Serial.begin(115200);
  randomSeed(analogRead(A0));
}
void loop() {
  //obtain latest value
  //use random number for demonstration
  lastValue = random(1000);//get latest value 0 -999
  Serial.print ("lastValue = ");
  Serial.println(lastValue);
  Serial.print("previousLastValue = ");
  Serial.println(previousLastValue);
  difference = lastValue - previousLastValue;
  Serial.print("The difference between the last an next to last value = ");
  Serial.println(difference);
  Serial.println();

  previousLastValue = lastValue;//transfer last to previous
  delay(1000);

}

cattledog:

...

...

I do not really see how your code differs from mine :wink: I might have missed something in OP's request.

@ sterretje

The OP is having some sort of problem understanding the assignment of the last value to the previous value and taking a new value. He obviously didn't get the point of your code or he wouldn't have come back. More examples from a slightly different perspective might help.

Or then again, maybe I don't understand the OP's request as well.

as long as i remember people had trouble understanding what i meant so i would have been surprised if anyone would have understood me pmsl

i had a lot of time to create an alternative of the function that i wanted as i barely slept lol
(its literally been 23 hours since the last time i slept lol, and i have been dodging sleep for the last week so i could finish this asap)

Here's what i have achieved for the last 3 days

if (digitalRead(capt1)==HIGH){
  Serial.println("3");

if(Serial.available()>=0) {
int dernier=Serial.parseInt();
if(dernier >0) {
Serial.println("foo"); // added these two lines (foo & bar) so i could check if the value would be recognized
Serial.println(dernier);
Serial.println("bar");
}
if (dernier - 1 == 2 || dernier + 1 == 4 ){
led1change(); } //beta testing this

}}

it does the job, but it doesn't do what i meant for it to do

but the one issue that i've encountered is that it doesn't read the "3" added on line 2 but it only reads the values that the user'll have to manually send on the serial monitor and then press the button (in this case the "888")

So am i missing something or have i added a typo to the code?

P.S. i really appreciate and wanted to thank all of the members who took their time to help me and post here :smiley:

It was explained before. You can not read what you send (the terminal does not echo it back to the Arduino). And as explained before

if (digitalRead(capt1)==HIGH){
  int whatever = 3;
  Serial.println(whatever);
  ...
  ...

  // do something with whatever
  whatever +=4;
  Serial.println(whatever);

  ...
  ...

I too am having difficulty understanding what you are after.
So,
You have
last_value LV
and
next_to_last_value NLV

Do you want NLV to be what LV was before it changed ?
What happend if they are the same ? No change ?

sterretje:
It was explained before. You can not read what you send (the terminal does not echo it back to the Arduino). And as explained before

if (digitalRead(capt1)==HIGH){

int whatever = 3;
  Serial.println(whatever);
  ...
  ...
  // do something with whatever
  whatever +=4;
  Serial.println(whatever);
  ...
  ...

mate are you sure that i can't echo the sent value by the arduino itself?
what i need is only for a way to actually create the last added value into a variable but i cannot find a way for it to read the serial data sent by the arduino on a press of a button, it only echoes the values added by the computer :confused:

Yes, I’m sure. Hence you first save the value that you want to be echoed (in your terminology) in a variable and next you send the variable.

Please post your latest code (complete) when you have an update so we have an idea how it all fits together.