Program Structure

What is the structure of the Arduino Code for a program.

I'm not having any luck using the "for" statement. Copied right lout of Reference, it doesn't not work.

e.g.
void Base(int n) {
// Size of an integer is assumed to be 48 bits
// array to store binary number
int binaryNum[48];

//counter for binary arrsy
int i = 0;
while (n > 0) {

// storing remainder in binary array
binaryNum = n % 2;

  • n = n / 2;*
  • i++;*
  • }*
  • }*
  • // printing array in reverse order.*
  • for (int j = i - 1; j >= 0; j--) {*
  • println( binaryNum[j] );*
  • } *
    }
    Error message
    DDS_Frequency_Generator:44:5: error: expected unqualified-id before 'for'
  • for (int j = i - 1; j >= 0; j--) { me ^ what does this mean*
  • ^~~*
    DDS_Frequency_Generator.ino (7.58 KB)

(deleted)

Time to learn about formatting your code in the IDE with:

CTRL T or CMD T

Spycatcher2k, your absolutely correct, I missed it. And thank you very much.

Larryd, I've never run across CRTL T or CMD T. I'm guessing that they are some form of formatting with indentation to alert one of the missing parentheses. likewise thank you very much.

I've corrected the problem and verified/compiled it/ and uploaded it, but it does not do anything. Nothing responds on the Serial Monitor.
Code follows, most.ly comments now but should still run.

#include "Arduino.h"
// Put all my vsariables in global space:
//Type & Name // AHEX SA Description
int Phase1[2]; // 0:1 0 14 Bit Phase Adjust Reg 1, Bits 15:14 X Bits 13:0 are Data. This value is the pahse angle.
int Phase2[2]; // 2:3 1 14 Bit Phase Adjust Reg 2, Bits 15:14 x Bits 13:0 are Data. This value is the pahse angle.
int FTW1[6]; // 4:9 2 48 Bit FTW Frequency Tuning Word) 1, Bits 47:0 are Data. Bits. This value using this formula.
// FTW = (Desired Freq * 2^N)/SysClk. Where N is the phase accumulator resolution of 48 bits.
int FTW2[6]; // A:F 3 48 Bit FTW=(Frequency Tuning Word) 1, Bits 47:0 are Data. Bits. This value using this formula.
// FTW = (Desired Freq* 2^N)/SysClk. Where N is the phase accumulator resolution of 48 bits.
int DFW[6]; // 10:15 4 48 Bit DFW (Delta Frequency Word), Bits 47:0 are Data. This value using this formula.
// DFW = (Desired Freq* 2^N)/SysClk. Where N is the phase accumulator resolution of 48 bits.
int UDClk[4]; // 16:19 5 32 Bit Update Clock, Bits 31:0 are Data. The data is value between update [pulses.
int RRClk[3]; // 1A:1C 6 24 Bit Ramp Rate Clock, Bits 23:20 are don't care, Bits 19:0 are Data.
int CR1; // 1D: 7 8 Bit 31:29 = X, 28 = Comp PD, 27 = Always Low, 26 = QDAC, 25 = DAC PD, 24 = DIG PD.
int CR2; // 1E: 7 8 Bit 23:16 23 = X 22 = PLL Range, 21 = Bypass PLL, 20:14 = CL:K 4:0.
int CR3; // 1F: 7 8 BIT 15:14 = ACC1:ACC2, 13 = Triangle, 12 = QDQC, 11:9 = Mode 2:0, 8 = Int'l/Ext'l UD.
int CR4; // 20: 7 8 Bit 7 = X, 6 = Bypass Invsinc, 5 = OSKEN, 4 = OSKINT, 3:2 = X, 1 = LSB First, 0 = SDO Act.
int OSKI[2]; // 21:22 8 16 Bit 12 Bits, Bit 15:12 = X, 11:0 = Output Shaped Keying I multiplier values.
int OSKQ[2]; // 23:24 9 16 Bit 12 Bits, Bit 15:12 = X, 11:0 = Output Shaped Keying Q multiplier values.
int OSKRR; // 25 A 8 Bit Bits 7:0 = Out[put Shaped Keying Ramp Rate values.
int QDACM[2]; // 26:27 B 16 Bit Bit 15:12 = X, 11:0 = QDAC Control mode values.
int Mode; // 3 Bit valuie 0:4.
int n;
int j;
void Base(int n);
// the Base(x) function)
void Base(int n)
{
// Size of an integer is assumed to be 48 bits
// array to store binary number
int binaryNum[48];

//counter for binary arrsy
int i = 0;
while (n > 0)
{

// storing remainder in binary array
binaryNum = n % 2;

  • n = n / 2;*
  • i++;*
  • }*
  • // printing array in reverse order.*
  • for (int j = i - 1; j >= 0; j--)*
  • {*
  • Serial.println ( binaryNum[j] );*
  • } *
  • }*
    void setup()
    {
  • Serial.begin(9600);*
  • delay(5000);*
  • Base(3518437); /try it hear*

}
void loop()
{

  • // put your main code here, to run repeatedly:*

  • Base(3518437);// try it hear*

  • while (Serial.available() != 0) {*

  • Mode = 0; *

  • switch (Mode)*

  • {*

  • case 0: *

  • //Single Tone*

  • break;*

  • case 1: *

  • // Un Ramped FSK*

  • case 2: *

  • //================*

  • // Ramped FSK 1*

  • break;*

  • case 3: *

  • //================*

  • // Chirp 1*

  • break;*

  • case 4: *

  • // BPSK*

  • break;*

  • default:*

  • // statements*

  • break;*

  • }*

  • }*
    }
    The thing I found out which bothered me most is that the error messages are almost not understandable to a newbie to Arduino and a C++ programmer that gave it up about 20+ years ago.
    I would also like to make a recommendation to add the messages into the Serial Monitor or a separate window so we have more room to expand the font size so we can see what we are doing. It's tuff enough on us 70+ years old folks as it is.
    Thanks,
    pamam

Size of an integer is assumed to be 48 bits. No an integer on the Arduino is 16 bits (2 bytes)


An integer on the Arduino is 2 bytes (16 bits) -32768 to 32767


int binaryNum[48]; Here you are defining an array of 48 integers (96 bytes), this array is uninitialized and contains random data.

Base(3518437);// try it hear. 3518437 an unsigned integer can only go up to +32767 :wink:

binaryNum = n % 2; No, better read about arrays:

Mode = 0;
switch (Mode). <———<<<< since Mode is made 0, only ‘case 0;’ will execute.
{
. . .
}

Note: at no place are you reading anything from the receive buffer.


Study time :wink:

Number 'type's:

  • boolean (8 bit) - simple logical true/false, Arduino does not use single bits for bool
  • byte (8 bit) - unsigned number from 0-255
  • char (8 bit) - signed number from -128 to 127. The compiler will attempt to interpret this data type as a character in some circumstances, which may yield unexpected results
  • unsigned char (8 bit) - same as 'byte'; if this is what you're after, you should use 'byte' instead, for reasons of clarity
  • word (16 bit) - unsigned number from 0-65535
  • unsigned int (16 bit)- the same as 'word'. Use 'word' instead for clarity and brevity
  • int (16 bit) - signed number from -32768 to 32767. This is most commonly what you see used for general purpose variables in Arduino example code provided with the IDE
  • unsigned long (32 bit) - unsigned number from 0-4,294,967,295. The most common usage of this is to store the result of the millis() function, which returns the number of milliseconds the current code has been running
  • long (32 bit) - signed number from -2,147,483,648 to 2,147,483,647
  • float (32 bit) - signed number from -3.4028235E38 to 3.4028235E38. Floating point on the Arduino is not native; the compiler has to jump through hoops to make it work. If you can avoid it, you should.

You should always select the 'data type' best suited for your variables.
ex:

  • your variable does not change and it defines a pin on the Arduino. const byte limitSwitchPin = 34;
  • since an analog variable can be 0 to 1023, a byte will not do, you can select 'int'. ex: int temperature;
  • if your variable needs to be within -64 to +64 a 'char' will do nicely. ex: char joystick;
  • if your variable is used for ASCII then you need type 'char', ex: char myText[] = {"Raspberry Pie Smells"};
  • if your variable enables some code then boolean can be used. ex: boolean enableFlag = false;
  • millis() returns the time in ms since rebooting, ex: unsigned long currentTime = millis();

pamam:
Larryd, I've never run across CRTL T or CMD T. I'm guessing that they are some form of formatting with indentation to alert one of the missing parentheses.

The Arduino IDE has an auto-format feature for your code. You can use it via the Tools > Auto Format menu option, or the keyboard shortcuts larryd shared. If you're using the Arduino Web Editor, the keyboard shortcut is Ctrl + B.

Even if you don't care about proper code formatting, please always do this on your code before sharing it on the forum as a courtesy to the people you're asking for help. It makes it much easier for us to quickly review code.


Please use code tags when you post code or warning/error messages. To do this, click the </> button on the forum toolbar, then paste the text you want to be in the code tags. Finally, move the cursor out of the code tags before adding any additional text you don't want to be in the code tags. If your browser doesn't show the posting toolbar, then you can manually add the code tags like this:

[code]

[color=blue]// your code is here[/color]

[/code]

The reason for doing this is that, without code tags, the forum software can interpret parts of your code as markup (the italics in your code above for example), leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier for us to read your code and to copy it to the IDE or editor.

Using code tags and other important information is explained in the "How to use this forum" post. Please read it.


pamam:
it does not do anything. Nothing responds on the Serial Monitor.

Which Arduino board are you using?

pamam:
The thing I found out which bothered me most is that the error messages are almost not understandable to a newbie to Arduino and a C++ programmer that gave it up about 20+ years ago.

The Arduino boards platforms use standard compilers (typically GCC, though the board platform author could use any compilation tool they like). The compilation error messages are from the standard compiler and is exactly what you'd see even if you weren't using the Arduino framework and Arduino development software.

pamam:
I would also like to make a recommendation to add the messages into the Serial Monitor or a separate window so we have more room to expand the font size so we can see what we are doing. It's tuff enough on us 70+ years old folks as it is.

It wouldn't make sense to display compiler output in the Serial Monitor, since this is not serial output. Perhaps it would be nice to allow the option of "undocking" the console pane of the Arduino IDE window. However, the Arduino IDE is centered around being very beginner friendly, with a gentle learning curve. Each added UI element makes the software less beginner friendly, so we need to be careful to only add UI features that are really needed. You can resize the console pane by dragging its top border upward, so you are welcome to make it as big as you like. Of course, this comes at the expense of a smaller editor pane.

larryd:
An integer on the Arduino is 2 bytes (16 bits) -32768 to 32767

...but only on the AVR boards (and likely any other 8 bit microcontrollers provided by 3rd party platforms). Yes, the AVR boards are the most popular, but 32 bit boards are certainly a big part of the Arduino world too (and growing more so all the time), so we shouldn't assume everyone is using AVR.

Thanks for all the answers to my questions.
pamam

Just ion case your are all wondering hat my project is see attached photo of a Mega 2560 and DDS Module that I'm talking to. It's a fun project building a controllable Frequency generator so I can starting testing my HF filters for my 6 band radio that I'm building from a kit. It needed lots of mod since I wanted 6 different band than the kit was built for. Anyway take a look. The black connector on the left is just laying unconnected it will get cut off eventually.