Trouble passing parameters to/from subroutines

I got my application below to compile, though it wasn’t passing the variables back to its caller to determine which screen graphic to display. I figured the data wasn’t actually global, so I changed the button press info to a boolean array…now the thing won;t compile despite hours trying to figure out what i am doing wrong. Can anybody lend assistance? Thanks in advance.

/*

  6 button ButtonBitmap.pde
  
 

  
  
 


*/


#include "U8glib.h"


//define the  pins for the touch screen
#define xLow  A0//14
#define xHigh A1//15
#define yLow  A2//16
#define yHigh A3//17




#define workbench 0;
#define center 1;
#define leftwall  2;
#define rightwall  3;
#define alloff  4;
#define allon  5;

boolean button[6];


//Declare functions
int  get_xy(void);
void whichbuttonpressed(int,int);
int draw(void);
int x,y;

// setup u8g object, please remove comment from one of the following constructor calls


U8GLIB_LC7981_160X80 u8g(22,23,24,25,26,27,28,29,30,31,32,33,34);//change these pin assignments...
                                                                     //d0,d1,d2,d3,d4,d5,d6,d7,en,cs1,di=rs,rw,res. cs2 is unneeded,reset is recommended to connect

const uint8_t button_bitmap[] PROGMEM  = {
		0x00,0x00,0x00,0x00,
		0x01,0xFF,0xFF,0x80,
		//truncated
		0x00,0x00,0x00,0x00};

 const uint8_t button_on_bitmap[] PROGMEM  = {
		0x00,0x00,0x00,0x00,
		0x01,0xFF,0xFF,0x80,
		//truncated
		0x00,0x00,0x00,0x00};

void setup(void) {
 
}

 
// *******************************************************************************************************
int draw(void) { //draw 4 columns of 2 rows of buttons - text below
  // graphic commands to redraw the complete screen should be placed here 
 //Set this up for hangar lights. 5 buttons for light banks. Rotate screen to portrait view.

u8g.setRot270(); //ok now we are rotated -90 degrees, so change the button positions.

int get_xy();

 whichbuttonpressed(x,y);
//workbench=1;  //test works from here

 if  (button[allon]) 
 button[workbench]=1; button[center] =1;  button[leftwall] =1; button[rightwall] = 1; 
 button[alloff] = 0;

if (button[alloff])  //preset all to off
 button[workbench]=0; button[center]=0; button[leftwall]=0; button[rightwall]=0; button[allon]=0; 
  //alloff button need not show "on"
 
if (button[workbench])
  u8g.drawBitmapP( 02,  0, 4,32, button_on_bitmap); //check for workbench button press-display on/off
else
   u8g.drawBitmapP( 02,  0, 4,32, button_bitmap);
  
if (button[center])
  u8g.drawBitmapP( 46, 0, 4,32, button_on_bitmap);
  
else 
  u8g.drawBitmapP( 46, 0, 4,32, button_bitmap);
  
if (button[leftwall])
  u8g.drawBitmapP( 02, 55, 4,32, button_on_bitmap);
else
  u8g.drawBitmapP( 02, 55, 4,32, button_bitmap);
  
if (button[rightwall])  
  u8g.drawBitmapP( 46, 55, 4,32, button_on_bitmap);
else
  u8g.drawBitmapP( 46, 55, 4,32, button_bitmap);
  
if (button[allon]) 
  u8g.drawBitmapP( 46,109, 4,32, button_on_bitmap);
 else
  u8g.drawBitmapP( 46,109, 4,32, button_bitmap);
  
 u8g.drawBitmapP( 02, 109, 4,32, button_bitmap); //just draw "all off" button regardless of keypress

  //draw button descriptors
  //u8g.setFont(u8g_font_unifont);
  u8g.setFont(u8g_font_5x7);

  u8g.drawStr( 03, 39, "W-bench");
  u8g.drawStr( 03, 46, "& Door");
  
  u8g.drawStr( 45, 39, " Center");
   //u8g.drawStr( 46, 46, "Center");
   
  u8g.drawStr( 03, 95, " Left");
  u8g.drawStr( 03, 103, " Wall");
  
  u8g.drawStr( 45, 95, " Right");
  u8g.drawStr( 47, 103, " Wall");
  
  u8g.drawStr( 03, 148, " All Off");
  // u8g.drawStr( 04, 155, " Off");
  
  u8g.drawStr(47, 151, " All On");
  
  
 
}  //end of draw function
// *******************************************************************************************************


// *******************************************************************************************************
void loop(void) {
  // picture loop
  u8g.firstPage();  
  do {
    draw();
      } while( u8g.nextPage() );
  
  // rebuild the picture after some delay
 // delay(3000);
   
   
}  
// *******************************************************************************************************


// *******************************************************************************************************
 // Calculate which button if any has been pressed  
   void whichbuttonpressed(int x,int y) 
   {
    
   
    if (x < 85) 
     return;
     //top of portrait screen limit-just return
   if (x > 740) 
      return; ;//bottom bottom button limit
   if (y < 140) 
     return; //left of buttons
   if (y > 750) 
      return; //right of ...
   
  if ( (x>85 && x<250) && ( y>140 && y<365 ))  //top left button press? 
    button[workbench] = !button[workbench];
       return;
    //return button[workbench],button[center],button[leftwall],button[rightwall],button[alloff],button[allon];
  } 
   //workbench=1;   //this declaration changes the button to on
    if ( (x>85 && x<250 ) && ( y>500 && y<750 ))  //top right button?
    button[center] =!button[center];   
      return;
      
    if ( (x>342 & x<495) & (y>140 & y<365 ))  //middle left button?
    button[leftwall} =!button[leftwall];   
      return;
   
   if ( (x>342 & x<495) & (y>500 & y<750) )  //middle right button?
    button[rightwall] =!button[rightwall];   
      return;
      
    if ( (x>590 & x<740) & ( y>140 & y<365 ))  //all off button?
    alloff =1; 
     button[workbench]=button[center]=button[leftwall]=button[rightwall]=0;   
      return;
      
    if ( (x>590 & x<740) & (y>500 & y<750 ))  //all on button?
    allon =1;   
      return;
  
  return;
  
}
// *******************************************************************************************************

//COntents of touchsendserial inserted below. Revised function names
//Basically using the analog pins 0,1,2,3 on an Arduino board, those pins can be both analogRead pins and digitalWrite pins(in this case:pin numbers are 14,15,16,17) depending on //setting.
//First, to read an x-coordinate value, set the pin14 as 0V(LOW) and the pin15 as 5V(HIGH), then the rest of the pins(either of 16 and 17) can be analogRead pins. 
//Next, to read a     y-coordinate value, set the pin16 as 0V(LOW) and the pin16 as 5V(HIGH), then read the value from either of the pin14 and the pin15.
// Reading the both value one after another then send them through a serial communication to Processing.
//70 :a minimum value when touching the left edge of the touch panel
//???? :a maxmum value when touching the right edge of the touch panel
//???? :a minimum value when touching the upper edge of the touch panel
//???? :a maxmum value when touching the lower edge of the touch panel
//
//In this example, I used a 12.1-inch(width:height=4:3) touch panel.
//When touching each edge of the touch panel, the values are like the above.
//When not touching the touch panel, you can read zero value from the analog pins because of the pull-down resistors. 

//the digital output pins  - already defined up top
//#define xLow  A0//14
//#define xHigh A1//15
//#define yLow  A2//16
//#define yHigh A3//17

int get_xy(){   //read touch screen position
  
    //start serial communication
  Serial.begin(9600);
  //
  //X?LOW HIGH
  //set the both x-coordinate pins as digital output:one is Low the other is HIGH  
  pinMode(xLow,OUTPUT);
  pinMode(xHigh,OUTPUT);
  digitalWrite(xLow,LOW);
  digitalWrite(xHigh,HIGH);

  //  Y LOW:the both y-coordinate pins are set to be LOW
  digitalWrite(yLow,LOW);
  digitalWrite(yHigh,LOW);

  //Ychange the y-coordinate pins as digital input
  pinMode(yLow,INPUT);
  pinMode(yHigh,INPUT);
  delay(10);

  //?yLow?//read analog pin2(yLow pin) to get an x-coordinate value
  int x=analogRead(A2);
  
   //set the both y-coordinate pins as digital output:one is Low the other is HIGH 
  pinMode(yLow,OUTPUT);
  pinMode(yHigh,OUTPUT);
  digitalWrite(yLow,LOW);
  digitalWrite(yHigh,HIGH);

  //X LOW:the both x-coordinate pins are set to be LOW
  digitalWrite(xLow,LOW);
  digitalWrite(xHigh,LOW);

  //X:change the x-coordinate pins as digital input
  pinMode(xLow,INPUT);
  pinMode(xHigh,INPUT);
  delay(10);

  //?xLow
  //read analog pin0(xLow pin) to get an y-coordinate value
  int y=analogRead(A0);

  //if(Serial.available()>0){
    //:send the values as a DEC format with a delimiter
    Serial.print(x,DEC);   //X:x-coordinate
    Serial.print(",");     //:delimiter
    Serial.println(y,DEC); //Y:y-coordinate
    
    Serial.print("/Button booleans:");
    Serial.println(workbench);
   //delay (1000);
       //read a handshake signal from Processing and clear the buffer
    Serial.read();
 return x,y;
}
// *******************************************************************************************************

Moderator edit: CODE TAGS

Edit your post, select the code, and hit the "#" button above the input box to put it into code tags.

if  (button[allon]) 
 button[workbench]=1; button[center] =1;  button[leftwall] =1; button[rightwall] = 1; 
 button[alloff] = 0;

What do you think happens if “button(allon)” returns “true”?

A couple of obvious things:

 if  (button[allon]) 
 button[workbench]=1; button=1;  button[leftwall] =1; button[rightwall] = 1; 
 button[alloff] = 0;

As hinted-at by AWOL, this isn’t going to do what you expect. If you want all of those assignments to be done only if button[allon] is true, they have to be in braces i.e.:

if  (button[allon])
{
 button[workbench]=1; button=1;  button[leftwall] =1; button[rightwall] = 1; 
 button[alloff] = 0;
}

Also, you can’t return two values from get_xy() with ‘return x,y’ (not in C/C++, anyway). I would change the function to:

void get_xy(int& x, int& y)
{
   x = <whatever your code does now>
   y = <whatever your code does now>
}

This is passing x & y by reference so that their values can be changed within the function. You could pass them as pointers, but this is 2012 :wink:

Thanks for the replies, this will get me going hopefully. The Arduino IDE leaves a lot to be desired in helping a newbie debug. Those semicolon problems get thrown in by accident while I was trying out different things to get something to work. I clearly lack understanding of pointers, lemme go study and try again. Thanks

The Arduino IDE leaves a lot to be desired in helping a newbie debug.

You mean that it needs a "make my code do what I'm thinking, instead of the rubbish I'm typing" icon?

The board is full of smart-alecs today, eh? I meant the little box below the code during compiling, is not only hard to read, but its descriptions of what is actually wrong with the code bears little resemblance to the actual coding problem.

It indicates line numbers which have none in the code above, and runs on into infinity with subsequent errors a page or two long, for something a simple as one "{" missing. It makes getting up to speed with coding much more difficult. I mean, the intent of Arduino is to make things easier to get up to speed, not?

Likely there are "fine tunings" that you all have implemented to make things easier, but out of the box, this IDE sucks for a newbie.

I meant the little box below the code during compiling, is not only hard to read

This I'll agree with.

but its descriptions of what is actually wrong with the code bears little resemblance to the actual coding problem.

This I won't. That the messages mean little to you, today, does not mean that they are wrong. With experience, you'll begin to recognize mistakes when you make them again.

It indicates line numbers which have none in the code above

The code actually being compiled does not look like what you see, in most cases. The compiler output is referencing line numbers in the input, which might be quite different from what you see. There is definitely room for improvement.

and runs on into infinity with subsequent errors a page or two long

Generally, the errors need to be corrected in the order listed. Don't worry about the 2nd and subsequent messages. Fix the first one. Then, compile again to get a new first one.

Likely there are "fine tunings" that you all have implemented to make things easier

If you call experience fine-tuning, then yes.

So pointers are my current misunderstanding...maybe you can shed some light on what I don't have clear... I THOUGHT I could just declare my shared x and y variables across functions because I declared them at the top, as "global" variables. I read that if they are global, they are maintained and changeable anywhere in the program? But the app didn't cross the function boundary and return the value of x and y to be processed further...what am I missing? (I KNOW, a LOT) but be kind...

I THOUGHT I could just declare my shared x and y variables across functions because I declared them at the top, as "global" variables.

You can.

I read that if they are global, they are maintained and changeable anywhere in the program?

That is right.

But the app didn't cross the function boundary and return the value of x and y to be processed further...what am I missing?

I'm not sure what you are trying to say here. Global variables do not need to be passed to, or returned by, a function. That was why you made them global.

Right, but in the previous more simple version of this program, I had no passing of variables...was just changing the global variables. But the variables weren't getting changed upon return from the functions, leading me into this fray of passing parameters, which I apparently only half understand.

What I mean is, I simply declared x,y as integers up top (as global), then called the get_xy subroutine, which obtained the current x,y values (which I assume updates the global x,y variables throughout?), sent them to the serial port for display/troubleshooting (which works fine), and returns to the caller, however the x,y values aren't usable by the caller upon return, the function wasn't making a decision based on updated x,y global values. If I inserted manual x,y values in the caller just before the needed routines, it worked fine. So for some reason the x,y global values don't get brought back to the caller? Am I missing something? Do I need a pointer? Just trying to keep as simple as possible, so early successes lead to further more complex projects...thanks MUCH for insight.

It sounds as though you were operating on local variables in your setxy function. Did you declare x and x there?

they are global unless you declare some local with the same name!

You mean when I pasted the get_xy subroutine into my program the x,y variables, if declared there, simply changes them to “local”? LEmme go check if that’s the case…

int x=analogRead(A2);

the get_xy function has the above line (x is not redefined in the get_xy() routine)...x is used...it is declared as global at the top of the program...then gets set here in get_xy(), and this works locally, as it is sent to the serial monitor correctly...this x value should be usable on return to the caller right? It wasn't happening that way, which is how I got off this side path into passing parameters.

You are declaring a local variable called x with this:

int x=analogRead(A2);

This is what you want I think:

x=analogRead(A2);

the get_xy function has the above line (x is not redefined in the get_xy() routine)...x is used

x IS redefined. Get rid of the int declaration, if it is the global x you mean to modify.

The x defined inside a function as

int x=analogRead(A2);

will not affect the value of any x variable defined outside that function. That's why it's called a local variable.

Update: I eliminated the array and simplified things, returning to simple boolean descriptors..cleaned a little syntax and at least now it compiles again. But it STILL does not update the x,y global variables.

Am missing something basic. Does the line " int x=analogRead(A2); " redeclare the x as local...because it isn't returning to the caller with its new value, although within the subroutine it sends the properly read value to the serial monitor... should I add a new local variable and then reassign the analogread local variable to the global variable and return to the caller?

AHA!!! thanks so much, something simple.