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).
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.
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;
StackArraystack;
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
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).
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.
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.
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.