Using Stack in Arduino IDE

Hello guys,
I am using stack extensively in my arduino programme, but I am not sure of how to display the contents of stack, I went through arduino playground it says that the syntax to be used in (int count) for number of items and I dono how to implement even that, Can anyone out here help me in displaying the contents of the stack so that i can read it in serial monitor and as well counting number of items in the stack, what are all the syntax to be used (Note: I am implementing stack using arrays).

Thanks in advance

You have to show your sketch so we can see how you implemented your stack.

You have a stack pointer.
The stack contents between either the top of the stack and the stack pointer or the bottom of the stack and the stack pointer are your "valid" stack contents.
You know where all these items are, so writing a simple loop to print them out is almost trivial.

This is the "programming" section - it's usually expected that you post some code.

As John says, seeing your code is important in providing help.

I for one am intrigued that you are using this functionality extensively but don't understand how to use one of the methods provided.

Being a Novoice in Arduino IDE, and by having a very little knowledge about stack, please tolerate my innocence and help me through this, I am posting program code below

// program using stack

#include <StackArray.h>

int a[5];
int b = 1;
int c = 2;
int d = 1;
int e = 2;
int w = 4;
int x = 5;
int y = 6;
int z = 7;
int l293d1 = 2;
int l293d2 = 3;

void setup(){

pinMode(w, OUTPUT);
pinMode(x, OUTPUT);
pinMode(y, OUTPUT);
pinMode(z, OUTPUT);
pinMode(l293d1, OUTPUT);
pinMode(l293d2, OUTPUT);


stack.setPrinter (Serial);

int count();

Serial.print (a[5]);


void loop(){

if(b < c){


stack.push (a[0]);



stack.push (a[1]);


stack.push (a[2]);


stack.push (a[5]);


stack.push (a[4]);



// this is a simple model of my program, actually i want to built a maze following robot which saves the path it went.

A few points:
that code doesn't compile.

Serial.print (a[5]);

"a" is declared as a five element array - it doesn't have a subscript 5, so you're trying to print something off the end of the array.

Please use code tags when posting code.

fine, I was just testing to know whats happening inside the stack, I want to read all elements present in the stack

thank you

I want to read all elements present in the stack

You pushed them there - they're the same as the stuff you pushed.
You could pop them and print them, but I don't really see the point.

Is it because of this?

stack.push (a[5]);

As I said, you don't own a[5], so what you pushed and what is there now may not be the same.

I am designing maze solving robot, where robot has to navigate around the cells, and all the visited cells must be pushed to stack, so that it doesn't go to the cell which has already explored, while the robot is in action, We are not sure about which and how many cells it has gone through, so I will have to program in such a way that, by seeing the list of elements in stack, the robot should go to the remaining cells which remains unexplored.

So, I am pushing visited cells ( i.e a[0], a[1] etc) to stack, now while in the program i would have 1000 array elements and I just want to view the elements present in the stack, now by seeing the program given before I could say that ( a[0] to a[4] ) are inside the stack, If it dynamic how do I view the elements present in the stack

So, I am pushing visited cells ( i.e a[0], a[1] etc) to stack,

a[5] is not and cannot be a visited cell, so it is pointless pushing it to the stack.
Fix the obvious stuff first before trying to move on.

now while in the program i would have 1000 array elements

You're running this on a Mega, I hope/assume?

Thank you for your continuous support sir,

After pushing some elements like ( a[0] to a[4]), I want to view the elements inside the stack, I tried ( Serial.print( stackArray)) but of no avail, so for testing I used Serial.print(a[5]), I got the answer in serial monitor as 404 after execution of program, I dono what does it mean.

My simple question is how to view stack elements stored?

And yes I am using arduino due ( 32 bit microcontroller).

so for testing I used Serial.print(a[5])

I'm not going to post this again - there is NO a[5].
Getting an error or not what you expect is to be expected.

If you want to know what is on the the stack, either print it as you push it, or pop it all off and print it.

Thank you

Based on a quick glance of the available methods on playground page, a simple for loop should suffice:

int numberOfItems = stack.count();
for (int i = 0; i < numberOfItems; i++){
  int item = stack.peek(i);
  // now what do you want to do with the item?
  // it remains on the stack, but you can have a look at this copy.
  Serial.print(item); //perhaps?

Edit, I’ve just looked at the code for StackArray itself and that’s not how peek works. Man that documentation is awful.
I’ve added an additional ‘peek(int index)’ function to the library which should help you along.

StackArray.h (6.77 KB)

Not too difficult to add a peek(i) method to the library though.

Not too difficult to add a peek(i) method to the library though.

I thought that too. See my edited previous post.

Is there a reason why you are using a stack rather than simply using an array, particularly as what you are putting on the stack are elements of an array ? I note that the stack is defined to hold ints. How are you coding the cell coordinates using an int ? It is possible using a combination of integer division and modulus but I wondered if that was how you were doing it.

As to not visiting cells that have already been explored, this is surely going to be impossible when the robot has to back out of a dead end.

If you only want to hold a single path representing where you are (and how you got there) then an array ought to be sufficient. However, the size of the array is potentially the number of cells in your maze. How many cells will you have?

I suspect you will also need a separate store to record which cells have ever been visited. The most space-efficient way I can see to hold it is with one bit per cell, which would be easy enough to implement. You have to deal with sizing issues here too, but this is likely to be much smaller than your 'stack' so is probably not the thing that will break your solution.

Thank you Tom,

I have got a totally different perspective which could help me move forward, but I have small obstacle

After pushing the stack items, I pasted your code and I received some error which I had never heard before

Error goes like

no matching function for call to ‘Stack Array::peek(int&)’

@ UKHeliBob and @ PeterH

Idea behind using stacks is to save the last visited cell dynamically, In maze solving competition I would be given just the size of the maze [16 x 16] ( i.e 256 cells) I would not be the given the paths. Hence I am pushing the visited cell inside stack, so that the robot after visiting one cell checks its stack element and moves on with cell which remains unexplored and again after visiting another cell checks its stack elements again and move on.

As a result my robot wouldn't go to the visited cell again and its obvious it would save my competition time.

And myself being a Novoice in programming, I would appreciate any other way also, I want to learn as much as I can.

With this criteria if you could develop program using array, and with pleasure I would love to learn it from you, and even If you could guide me in this stack programming, I would love even that.

Thanks a lot