Setup Function Question >>> SOVLED <<<<

I am using an Itsy Bitsy 32u4 3V board.
I am trying to use the following code>>

void setup(){

// Initialize and start UBS for print debug points
  Serial.begin(115200);

 // Setup and Initialize the GPIO pins
  pinMode(Smack_Intensity_Pin_B4, INPUT);
  pinMode(Smack_Intensity_Pin_B3, INPUT);
  pinMode(Smack_Intensity_Pin_B2, INPUT);
  pinMode(Smack_Intensity_Pin_B1, INPUT);
  pinMode(Run_Pin,           INPUT);
  pinMode(Abort_Pin,         INPUT);
  pinMode(DIR_IN_Pin,        INPUT);
  pinMode(CW_Home_Pin,       INPUT_PULLUP);
  pinMode(CCW_Home_Pin,      INPUT_PULLUP);

..........................................................................................
..........................................................................................
..........................................................................................

  pinMode(Mode_Pin_0,        OUTPUT);
  pinMode(Mode_Pin_1,        OUTPUT);
  pinMode(Mode_Pin_2,        OUTPUT);
 

//Set output pins to defult values
  digitalWrite(Step_Pin ,   false);
  digitalWrite(DIR_Out_Pin, false);
  digitalWrite(Busy_Pin,    false);

  // Set the motor driver step divide number
  switch (Mode){
    case 4:
      digitalWrite(Mode_Pin_0 , false);
      digitalWrite(Mode_Pin_1 , true );
      digitalWrite(Mode_Pin_2 , false);
      Angle_90_Count  = Angle_90_Count_M4;
      Angle_180_Count = Angle_180_Count_M4;
      break;

    case 8:
      digitalWrite(Mode_Pin_0 , true );
      digitalWrite(Mode_Pin_1 , true );
      digitalWrite(Mode_Pin_2 , false);
      Angle_90_Count  = Angle_90_Count_M8;
      Angle_180_Count = Angle_180_Count_M8;
      break;
  
    case  16:
      digitalWrite(Mode_Pin_0 , false);
      digitalWrite(Mode_Pin_1 , false);
      digitalWrite(Mode_Pin_2 , true );
      Angle_90_Count  = Angle_90_Count_M16;
      Angle_180_Count = Angle_180_Count_M16;
      break;

    default:
      Serial.println(" Invailed Mode Number. Vaild Mode numbers are 4, 8 or 16");
      Serial.println(" Recompile the sketch with a vailed Mode number");  
      do {
      }while (true);
      break;
  }
  Serial.println();  
  Serial.print(" >> Set Up Complete <<"); 
  Serial.print(" Mode Number = ");
  Serial.print( Mode );
  Serial.print("[ ");  
  Serial.print(digitalRead(Mode_Pin_0));
  Serial.print(digitalRead(Mode_Pin_1));
  Serial.print(digitalRead(Mode_Pin_2));
  Serial.print(" ]");
  Serial.println();
  delay(2000);
} 

I do not see the print in the serial monitor!!! The code compiles and uploads without any errors. So my question is can I run the code in the setup function or is there an error in the code?

I assume that you have a Serial.begin() somewhere in the code that you did not post

Please post a full sketch rather than just an isolated snippet

The setup() function is just like any other function in that you can run whatever code you like but in the Arduino environment it will only run once each time the Arduino is powered up or reset

1 Like

The Serial.begin() is the first line executed in the setup function.

void setup()

// Initialize and start UBS for print debug points
     Serial.begin(115200);

I missed that because of the disjointed way in which you posted your code. Please post your whole sketch as one block of code in a single pair of code tags

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

The MCU that you are using (32U4) has native USB port, and you have to wait for the port to be ready before using. See reference for an example.

Ciao, Ale.

OK, adding a wait period for USB to start did the trick.

void setup(){

// Initialize and start UBS for print debug points
  Serial.begin(115200);
  while (!Serial) continue;

 // Setup and Initialize the GPIO pins

Thank you for sharing your wisdom.

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