A question about functions

I have been writing some code to draw a linegraph on a LCD screen and trigger an alarm if the analog value is greater than an alarm point.

Everything works as I want.

Graph is drawn on the screen.
The INC and DEC buttons change the alarm level
The bottom RH corner displays the weigh in Kg
The relay turns from on to off when the alarm is exceeded.

I have a function coded to change the Alarm level when the INC and DEC buttons are pressed.

My question is if I leave out the (int Weight) parameter in the following line

int AlarmLevel(int Weight) {

I get a too many parameters error.

I thought I would be able to put

void AlarmLevel () {

as I was not parsing any parameter TO the function, just returning Alarm to the main loop.

So what have I misunderstood about writing a Function?

Thanks
sutto55

// Touch screen library with X Y and Z (pressure) readings as well
// Analog sensor data graph
// Code by Damon Borgnino

#include "TouchScreen.h"
#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_TFTLCD.h> // Hardware-specific library

#define DEBUG

#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0

#define LCD_RESET A4

// These are the pins for the shield!
#define YP A3  // must be an analog pin, use "An" notation!
#define XM A2  // must be an analog pin, use "An" notation!
#define YM 9   // can be a digital pin
#define XP 8   // can be a digital pin

#define MINPRESSURE 100
#define MAXPRESSURE 1000

// calibration mins and max for raw data when touching edges of screen
// YOU CAN USE THIS SKETCH TO DETERMINE THE RAW X AND Y OF THE EDGES TO GET YOUR HIGHS AND LOWS FOR X AND Y
#define TS_MINX 210
#define TS_MINY 210
#define TS_MAXX 915
#define TS_MAXY 910

#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF
#define GREY      0xCE79
#define LIGHTGREY 0xDEDB


#define relayPin 40 //will turn on to power sensor(s)
#define sensor1Pin A15 // sensor one
#define sensor2Pin A14 // sensor two
//#define relayPin 40 // Output to turn on relay
Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 364);



int sensor1Val = 0; // used to hold sensor data
int sensor1ValOld = 0; // holds previous value for line graph
int Alarm = 110;
int AlarmPoint;
int oldAlarmPoint;
int oldAlarm;
int Xposn = 51, Yposn;
int Weight;
int Kg;
int oldKg = 0;
int plotVal ;
int oldPlotVal;
bool toggle = true;
//int relayPin = 40;

//int Xposn, Yposn;
//TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
//TSPoint p = ts.getPoint();
#define  BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF

#define TS_LEFT 200 //920
#define TS_TOP  120 //940
#define TS_RT   920 //200
#define TS_BOT  940 //120


/////////////////////////////////////////////////////////////////////////////////////////////

void setup() {

#ifdef DEBUG
  Serial.begin(9600);
#endif // DEBUG

  pinMode(relayPin, OUTPUT);
  //digitalWrite(powerPin, HIGH); // turn sensor power on

  tft.drawRect(50, 0, 319, 200, BLUE);

  int i = 0;

  // Draw crosshairs {

  tft.reset();
  delay(500);

  uint16_t identifier = tft.readID();

  tft.begin(identifier);

  delay(500);

  tft.fillRect(0, 0, 240, 320, BLACK);
  tft.setRotation(3);
  tft.drawRect(50, 1, 270, 200, BLUE);

  for (int i = 0; i < 200; i += 10) {
    tft.drawLine(45, i, 50, i, BLUE);

  }
  tft.fillRoundRect(10, 205, 50, 30, 8, GREEN);
  tft.fillRoundRect(80, 205, 50, 30, 8, GREEN);
  tft.setCursor(17, 215);
  tft.setTextColor(BLACK);
  tft.setTextSize(2);
  tft.print("INC");
  tft.setCursor(87, 215);
  tft.setTextColor(BLACK);
  tft.setTextSize(2);
  tft.print("DEC");
  //tft.setCursor(1, 1);
  //tft.setTextColor(BLUE);
  //tft.setTextSize(2);
  //tft.print("KGs");
  tft.setTextColor(YELLOW);
  tft.setTextSize(3);
  tft.setCursor(280, 210);
  tft.print("Kg");
  //tft.drawLine(51, 112, 318, 112, RED);


}





//////////////////////////////////////////////////////////////////////////////////


void loop () {

  int AlarmLine;
  //Read Value from Strain Gauge and send
  //it to Tension Function then return calculated weight.
  sensor1Val = analogRead(sensor1Pin);
  Kg = sensor1Val / 4.096;
  plotVal =  197 - (Kg / 1.28);
  tft.drawLine(Xposn, oldPlotVal, Xposn + 1, plotVal, YELLOW);

  if (plotVal != oldPlotVal) {
    oldPlotVal = plotVal;
  }

  if ( Kg != oldKg ) {
    oldKg = Kg;

    tft.fillRect(220, 210, 60, 40, BLACK);
    tft.setTextColor(YELLOW);
    tft.setTextSize(3);
    tft.setCursor(220, 210);
    tft.print(Kg);

  }

  //pinMode(YP, OUTPUT);     //.kbv these pins are shared with TFT
  //pinMode(XM, OUTPUT);
  delay(100);
  Xposn = Xposn + 1;
  if (Xposn > 317) {
    Xposn = 52;
    tft.fillRect(51, 2, 268, 197, BLACK);


  }
  AlarmLine = AlarmLevel (Alarm);

  AlarmPoint = (-0.8 * Alarm) + 200 ;
  tft.drawLine (52, AlarmPoint, 318, AlarmPoint, RED);

  tft.setTextColor(RED);
  tft.setTextSize(2);
  tft.setCursor(0, (AlarmPoint - 10));

  if (Alarm != oldAlarm) {
    tft.fillRect(0, (AlarmPoint - 20), 40, 35, BLACK);
    tft.print(Alarm);
    oldAlarm = Alarm;
  }


  if (Kg < Alarm) {
    tft.fillCircle(160, 220, 15, GREEN);
    digitalWrite(relayPin, HIGH);
  }

  else {
    tft.fillCircle(160, 220, 15, RED);
    digitalWrite (relayPin, LOW);
  }

}




int AlarmLevel(int Weight) {
  TSPoint p = ts.getPoint();
  pinMode(YP, OUTPUT);     //.kbv these pins are shared with TFT
  pinMode(XM, OUTPUT);
  if (p.z > ts.pressureThreshhold) {
    if (p.x > 820 && p.x < 910 && p.y > 150 && p.y < 250) {
      tft.drawLine (52, ((-0.8 * Alarm) + 200), 318, ((-0.8 * Alarm) + 200), BLACK);
      Alarm = Alarm + 10;
      if ( Alarm > 240) {
        Alarm = 240;

      }
    }


    if (p.x > 820 && p.x < 920 && p.y > 330 && p.y < 430) {
      tft.drawLine (52, ((-0.8 * Alarm) + 200), 318, ((-0.8 * Alarm) + 200), BLACK);
      Alarm = Alarm - 10 ;
      if (Alarm < 10) {
        Alarm = 10;
      }

    }

    return Alarm;
  }
}

Hello,

If you remove the parameter from the function, then you cannot call that function with a parameter as you did here:AlarmLine = AlarmLevel (Alarm);

Instead you have to do:AlarmLine = AlarmLevel ();

Great, Thanks guix

that makes it clearer - and it works

sutto55

Every function you define in your code has a function signature[/b] in the form:
Function Type Function Paramter list
* Specifier Name*
* | | |*
* | | |*
* V V V*
* int AlarmLevel (int Weight)*
The function type specifier tells the data type this function returns. If the function does not return a value, it should be labeled void. The function name follows the same naming rules as applied to variables. The parameter list contains a comma-separated list of the parameters that are being passed into the function. The parameter list can be empty. The parameter list must match the parameters that are being passed during the call. In your code, this is:
AlarmLine = AlarmLevel (Alarm);
Think of yourself as a person walking through your code. When you reach this line, the program takes the backpack you happen to be wearing, opens it up and places a copy of the value held in Alarm into the backback as a 2-byte integer. Assume Alarm equals 15, so that's the number that's in your backpack as two bytes of data. It must do this because you defined the function type specifier as an int. The code then sends you to a black box named AlarmLevel.
You walk into that black box and the code opens your backpack, takes out the two bytes and see that it is the number 15. It then uses that value in whatever the black box is supposed to do. When it's done with its work, it opens up your backpack and puts int value that it calculated into your backpack. (Your function type specifier dictates that the number must be an int.) The program then sends you back to the same statement position in the code, opens your backpack, takes out the integer value stored in your backpack, and assigns it into AlarmLine.
Now, think of the problems you create if you called the function as:
AlarmLine = AlarmLevel (); // NOTE: nothing put into the backpack
When you get inside the black box and the code looks into your backpack, it expects to see an integer value in there. Since you didn't put a two-byte int in there, it goes crazy because the function signature doesn't match the way you used it in the statement that calls the function. You either need to supply the missing parameter, or change the function signature.

Thanks Econjack,

the backpack analogy is very useful.

One of the other things I need to add to the code is that when the alarm goes above the alarm line it turns off a relay and doesnt reset it if it drops below the alarm level.

If I use a function for this, in my previous example, I returned an integer but in this case I just need true or false.
So in this case I would use the function type specifier as bool?

sutto55

Yes.

.