c - Arduino StackArray not global -


i'm coding project read byte of serial data, convert binary , print out serial. currently, i'm using stack store binary. however, when push binary numbers stack, appear pushed in method call push() in, cannot pop ints array outside of method because there no elements pop. why, , there ways resolve problem. thought stacks reference variables.

void converttobinary(byte number, stackarray<int> binarylist){ while(number > 0){     int rem = number % 2;     binarylist.push(rem);     number /= 2;      } } 

it pushes seemingly fine. when try pop in method in pass stack popped, no elements there popped.

void printtoled(stackarray<int> list){   //digitalwrite(ledpin, high);   while(!list.isempty()){     int ledstate = list.pop();     //serial.println(ledstate);     if(ledstate == 1){       digitalwrite(ledpin, high);         }     else{       digitalwrite(ledpin, low);       }     delay(1000);     transitionflash();   } } 

since in setup specified serial stack printer, gives me message when try pop element stack.

stack: can't pop item stack: stack empty. 

edit(full example):

#include <stackarray.h> stackarray<int> binary; int ledpin = 13; byte data = 0; void setup() {   serial.begin(9600);   binary.setprinter(serial);   pinmode(ledpin, output); }  void transitionflash(){   for(int = 0; < 20; i++){   digitalwrite(ledpin, low);   delay(50);   digitalwrite(ledpin, high);   delay(50);   } }  void converttobinary(byte number, stackarray<int> binarylist){   while(number > 0){     int rem = number % 2;     binarylist.push(rem);     number /= 2;    } }  void printtoled(stackarray<int> list){   while(!list.isempty()){     int ledstate = list.pop();     if(ledstate == 1){       digitalwrite(ledpin, high);         }     else{       digitalwrite(ledpin, low);       }     delay(1000);     transitionflash();   } }  void blink(int times, int duration){   for(int = 0; < times; i++){     digitalwrite(ledpin, high);     delay(duration);     digitalwrite(ledpin, low);     delay(duration);   } }  void loop() {   while(serial.available()){     data = serial.read();     converttobinary(data, binary);     printtoled(binary);     blink(10, 500);   } } 

i've resolved this. in order pass reference variable, need add ampersand front, notify c in fact reference variable.


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -