I get an error when compling to arduino and am unsure why. any help is appreciated! SOLVED!

im trying to make a sytsem to detect vibration from sensors and then light up an led and send a signal to max8 to play a sound and when i upload the code below to the arduino uno i get this errror:
Arduino: 1.8.19 (Windows Store 1.8.57.0) (Windows 10), Board: "Arduino Uno"

C:\Users\caley\Downloads\AAI_CW2_Project_Sketch\test_final\test_final.ino: In function 'void SetLedOn(int, int*)':

C:\Users\caley\Downloads\AAI_CW2_Project_Sketch\test_final\test_final.ino:59:45: warning: 'sizeof' on array function parameter 'leds' will return size of 'int*' [-Wsizeof-array-argument]

         for (int i = 1; i <= sizeof(leds); i++)

                                         ^

C:\Users\caley\Downloads\AAI_CW2_Project_Sketch\test_final\test_final.ino:57:43: note: declared here

     void SetLedOn(int ledId, int leds[])

                                       ^

C:\Users\caley\AppData\Local\Temp\cc1D5F4s.ltrans0.ltrans.o: In function `main':

C:\Users\caley\OneDrive\Documents\ArduinoData\packages\arduino\hardware\avr\1.8.5\cores\arduino/main.cpp:43: undefined reference to `setup'

collect2.exe: error: ld returned 1 exit status

exit status 1

Error compiling for board Arduino Uno.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

   //init variables
        int leds[] = { 2, 3, 4 };
        int numPads = 3;
        int threshold = 50;

        int state = 0;
        int baseTime = millis();
        int thisPress = 0;
        int lastPressed = 0;
        
        void loop()
        {
            //if in state>0 but more than 5secs elapsed since last press then reset to state0
            if( (state>0) && ( millis() > (baseTime+(5000*state))))
            {
                state = 0;
                SetLedOn(0,leds);
            }

            //test for padd press
            thisPress = GetPadPress(numPads, threshold);
            
            //if pad pressed detected and different from last pad pressed then move to next state and update leds/max
            if (thisPress != 0 && thisPress!=lastPressed)
            {
                state++;    //Increment state

                SetLedOn(thisPress,leds);  //turn the pads corresponding led on, turn off others

                Serial.println(state);  //set message to MAX
                                       
                                        //or alternatively to help in debugging 
                                        //String msgPrefix= "PadPress: ";
                                        //String msg = msgPrefix + state;
                                        //Serial.println(msg);
                                        //or alternatively you could send the pad that was pressed, or send both state and pad pressed
                
                //update the lastPressed
                lastPressed = thisPress;
            }
            
        }  //continue looping

        
        int GetPadPress(int numPads, int threshold)  //returns the number of the pad that was pressed, if press above threshold, otherwise returns 0
        {
            for (int i = 0; i < numPads; i++)  //poll each pad
            {
                if (analogRead(i) > threshold) { return i; }  //return on first detected pad press above threshold
            }
            return 0;
        }
        
        void SetLedOn(int ledId, int leds[])
        {  //Turn on specified LED, turn off all others, to turn off all leds pass ledId=0
            for (int i = 1; i <= sizeof(leds); i++)
            {
                if (i == ledId) { digitalWrite(leds[i], HIGH); }
                else { digitalWrite(leds[i], LOW); }
            }
        }

You must have a setup function, even if it does nothing.

what do you mean? im new to arduino so theres a lot im still learning

Look at any example Arduino program in the IDE. There will be at a minimum, two functions. Loop, which you have and setup, which you don't.

thanks that helped clear things up

Don't ignore this warning either.

(What made you think this was an installation issue?)

That's a pretty complex sketch for anyone just starting out.
If you started with a template of just

void setup()
{
}

void loop()
{
}

you'd have the basics.

I would code this as
#define NUMLEDS 3
int leds[NUMLEDS]={2,3,4};

and use:

for (int i = 1; i <= NUMLEDS; i++) in my code.
The sizeof(leds) is the size of an int pointer, 4 on most arduinos, but could be 8 on a 64-bit machine. sizeof(leds[0]) is the int size, which would be 2, or 4 on a 32-bit machine. It's pointer math, not the number of array elements.
My reasoning is that if you want to add, rearrange or delete leds in your project, you edit the #define and the array, and if they don't agree, the compiler will tell you you made a mistake. By basing your loop counter on the #define, if you change it, all the loops will automatically be in agreement.

I certainly would not use that in my code :wink:

That's what I get for copy paste, DOH!

for (int i = 1; i < NUMLEDS; i++)

Is better still.

You're good! :flushed:

@hap-ki-doh your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.