Array, For Loops, and AnalogWrite

Hi, Im rather new to Arduinos and I was trying out a simple sketch.

The file compiles fun but the buzzer makes no noise. I checked with simpler sketches and the buzzer is working fine. I am not sure where the problem is here.

#include <Wire.h>
#include <Servo.h>
#include <seeed-kit.h>
#include <math.h>

float  music[] = {240, 270, 300, 320, 360, 400};
void playNote(float f[], int port);

int main()
{
      playNote(music,5);
}

void playNote(float f[], int port)

{
  for ( int i = 0; i < 6; i ++)
  {
         analogWrite(port, f[i]);
         delay(1000);
  }
 
}

@OP

Try the following sketch – a slightly modified version of you. If it works, then ask questions relating to medications of the codes.

#include <Wire.h>
#include <Servo.h>
//#include <seeed-kit.h>
//#include <math.h>

//float  music[] = {240, 270, 300, 320, 360, 400};
int music[] = {240, 270, 300, 320, 360, 400};
//void playNote(int f[], int port)//(float f[], int port);

void setup()
{
  
}

void loop()//int main()
{
  playNote(music, 5);
}

void playNote(int f[], int port)//(float f[], int port)
{
  for ( int i = 0; i < 6; i ++)
  {
    analogWrite(port, map(f[i], 240, 400, 0, 255));//(port, f[i]);
    delay(1000);
  }
}

It works like that, but I am not clear about what is happening or how I can control it

MSA23dsn: It works like that, but I am not clear about what is happening or how I can control it

That means the modified program of Post#1 does not work. But, it is working fine in my system of NANO and small buzzer -- it is nicely generating 5 pitches.

This is what the default main() looks like when compiling for Arduino AVR Boards: https://github.com/arduino/ArduinoCore-avr/blob/1.6.23/cores/arduino/main.cpp#L33-L51

int main(void)
{
    init();

    initVariant();

#if defined(USBCON)
    USBDevice.attach();
#endif

    setup();
    
    for (;;) {
        loop();
        if (serialEventRun) serialEventRun();
    }
        
    return 0;
}

In this case, the important thing is that call to init(). That does some hardware initialization. In your sketch, you have defined your own main(), which doesn't call init(). Your main() overrides the default main(). If you want to define your own main(), that's fine, but you need to take the time to understand what you're doing. You can't expect everything (e.g. analogWrite()) to work magically just as it does when you use the default main() if your main() doesn't call init(). If you don't want to take the time to dive into these complex details, that's fine, just use setup() and loop() instead of defining your own main(), like 99.999% of the rest of the Arduino users. The choice is yours.

@OP

The Post#4 of @pert has the explanation why the modified program of mine works and not your original program.

pert: This is what the default main() looks like when compiling for Arduino AVR Boards: https://github.com/arduino/ArduinoCore-avr/blob/1.6.23/cores/arduino/main.cpp#L33-L51

int main(void)
{
    init();
initVariant();

if defined(USBCON)

USBDevice.attach();

endif

setup();

    for (;;) { loop(); if (serialEventRun) serialEventRun(); }         return 0; }



In this case, the important thing is that call to init(). That does some hardware initialization. In your sketch, you have defined your own main(), which doesn't call init(). Your main() overrides the default main(). If you want to define your own main(), that's fine, but you need to take the time to understand what you're doing. You can't expect everything (e.g. analogWrite()) to work magically just as it does when you use the default main() if your main() doesn't call init(). If you don't want to take the time to dive into these complex details, that's fine, just use setup() and loop() instead of defining your own main(), like 99.999% of the rest of the Arduino users. The choice is yours.

....Its embarrasing but yes, I noticed I was missing init. My professor pointed that out too. I apologize for that, I had just started learning and was trying out some simple tasks. And at our school we are being taught to define our own main function instead of using setup and loop, therefore I have to use this. Thank you.

Very cool! I really like how Arduino provides a gentle learning curve with setup(), loop(), function prototype generation, etc. but if you want to write your code as completely standard C++ you can do that also. When I first started working with Arduino, I was afraid that I would eventually "grow out" of it, and have to start over with something more "professional". However, it manages a good balance between being beginnner friendly while still allowing you to do very advanced things.

I wish I had the opportunity to play with something like this when I was in school.