Converting Code from Arduino Nano to Arduino Nano 33 IoT

Hi,

I'm relatively new to Arduino coding and somewhat know the basics. For my work, we are building a heat shrink cutter using the design linked below. This is a project I took over several months ago after taking it over from someone else.

[Handy machine cuts heat shrink tubing to length | Arduino Blog]

The code is already written. However my predecessor seems to have purchased an Arduino Nano 33 IoT instead of the basic Nano that is listed in the design. I'm aware that the 33 IoT doesn't use elements of the code that is written, such as the SoftwareSerial library. I'm not sure how to convert the code to work for the 33 IoT, if that is even possible.

Below should be the code that comes with the project, for the basic Arduino Nano.

#include <Stepper.h>
#include <Arduino.h>
#include "BasicStepperDriver.h"
#include <Servo.h>

SoftwareSerial mySerial(2, 3); // RX, TX
Servo myservo;  

int A = 0;
int B = 0;
int state = 0;

int QTY, numMessages, endBytes;
byte inByte;
int flag = 0;

#define MOTOR_STEPS 200
#define RPM 40
#define MICROSTEPS 16
#define DIR 14
#define STEP 15
int pos = 0;



BasicStepperDriver stepper(MOTOR_STEPS, DIR, STEP);



void setup()
{
  numMessages, endBytes = 0;
  
  
  Serial.begin(9600);
  mySerial.begin(9600);
  myservo.attach(5); 
  myservo.write(180);
  stepper.begin(RPM, MICROSTEPS);
  delay(1000);
  
}

void loop()
{*
  data();
  
  if (A > 0 && B > 0) {
    delay(1000);
    
myservo.write(70);
delay(1000);
    for (int i = 0; i < B; i++) {

 stepper.rotate(-(2.75*A));
 cut();

    }
    A=0;
    B=0;

  }
}

  void data() {
    if (state < 1) {
      if (numMessages == 1) { //Did we receive the anticipated number of messages? In this case we only want to receive 1 message.
        A = QTY;
       // Serial.println(A);//See what the important message is that the Arduino receives from the Nextion
        numMessages = 0; //Now that the entire set of data is received, reset the number of messages received
        state = 1;
      }
    }

    if (state > 0) {
      if (numMessages == 1) { //Did we receive the anticipated number of messages? In this case we only want to receive 1 message.
        B = QTY;
       // Serial.println(B);//See what the important message is that the Arduino receives from the Nextion
        numMessages = 0; //Now that the entire set of data is received, reset the number of messages received
        state = 0;
      }
    }






    if (mySerial.available()) { //Is data coming through the serial from the Nextion?
      inByte = mySerial.read();

      // Serial.println(inByte); //See the data as it comes in

      if (inByte > 47 && inByte < 58) { //Is it data that we want to use?
        message.concat(char(inByte)); //Cast the decimal number to a character and add it to the message
      }
      else if (inByte == 255) { //Is it an end byte?
        endBytes = endBytes + 1; //If so, count it as an end byte.
      }

      if (inByte == 255 && endBytes == 3) { //Is it the 3rd (aka last) end byte?
        QTY = message.toInt(); //Because we have now received the whole message, we can save it in a variable.
        message = ""; //We received the whole message, so now we can clear the variable to avoid getting mixed messages.
        endBytes = 0; //We received the whole message, we need to clear the variable so that we can identify the next message's end
        numMessages  = numMessages + 1; //We received the whole message, therefore we increment the number of messages received.

        //Now lets test if it worked by playing around with the variable.

      }
    }
  }

  void cut (){
    for (pos = 70; pos <= 180; pos += 1) { 
    myservo.write(pos);              
    delay(5);        
  }

  for (pos = 180; pos >= 70; pos -= 1) { 
    // in steps of 1 degree
    myservo.write(pos);              
    delay(10);                       
  }
  }This text will be hidden

If someone is able to clear up how and why things need to be changed it would be greatly appreciated.

the Arduino Nano 33 IoT uses a different processor to the Nano classic
in particular it has a hardware serial port called Serial1 on pins 2 and 3 - see nano-33-iot/uart
for a start you can remove the reference to SoftwareSerial and replace occurances of mySerial with Serial1
also the Arduino Nano 33 IoT uses 3.3Volt logic so be careful connecting it up to 5V logic - use level converters or potential dividers

if your cutter is already working using a Nano it may be simpler just to purchase another Nano and forget the Arduino Nano 33 IoT for now

2 Likes

Thanks for the feedback! Like you suggest, I think we'll likely move back to the Nano Classic. The code has verified in some previous attempts, however there are sometimes errors (that I've pasted below.) Are you able to shed a light on any of them?

M:\Workarea\CJ\Arduino Testing\Arduino\TUBE_cutting_machine_arduino_code\TUBE_cutting_machine_arduino_code.ino: In function 'void loop()':
M:\Workarea\CJ\Arduino Testing\Arduino\TUBE_cutting_machine_arduino_code\TUBE_cutting_machine_arduino_code.ino:47:7: error: void value not ignored as it ought to be
   data()
   ~~~~^~
M:\Workarea\CJ\Arduino Testing\Arduino\TUBE_cutting_machine_arduino_code\TUBE_cutting_machine_arduino_code.ino: In function 'void data()':
M:\Workarea\CJ\Arduino Testing\Arduino\TUBE_cutting_machine_arduino_code\TUBE_cutting_machine_arduino_code.ino:95:9: error: 'message' was not declared in this scope
         message.concat(char(inByte)); //Cast the decimal number to a character and add it to the message
         ^~~~~~~
M:\Workarea\CJ\Arduino Testing\Arduino\TUBE_cutting_machine_arduino_code\TUBE_cutting_machine_arduino_code.ino:95:9: note: suggested alternative: 'numMessages'
         message.concat(char(inByte)); //Cast the decimal number to a character and add it to the message
         ^~~~~~~
         numMessages
M:\Workarea\CJ\Arduino Testing\Arduino\TUBE_cutting_machine_arduino_code\TUBE_cutting_machine_arduino_code.ino:102:15: error: 'message' was not declared in this scope
         QTY = message.toInt(); //Because we have now received the whole message, we can save it in a variable.
               ^~~~~~~
M:\Workarea\CJ\Arduino Testing\Arduino\TUBE_cutting_machine_arduino_code\TUBE_cutting_machine_arduino_code.ino:102:15: note: suggested alternative: 'numMessages'
         QTY = message.toInt(); //Because we have now received the whole message, we can save it in a variable.
               ^~~~~~~
               numMessages
Multiple libraries were found for "Servo.h"
  Used: C:\Users\craig.joiner\Documents\Arduino\libraries\Servo
  Not used: C:\Users\craig.joiner\AppData\Local\Arduino15\libraries\Servo
Multiple libraries were found for "Stepper.h"
  Used: C:\Users\craig.joiner\Documents\Arduino\libraries\Stepper
  Not used: C:\Users\craig.joiner\AppData\Local\Arduino15\libraries\Stepper
exit status 1

Compilation error: void value not ignored as it ought to be

if " The code has verified in some previous attempts" why do you now have errors? have you lost the working versions? always make backup copies of code before you modify it

with seeing the complete code it is difficult to give advice
however, look at each error message and attempt to fix the problem
for eample

states that

error: 'message' was not declared in this scope

what is 'message'? fix the problem, recompile and look at next error
you often find that one error, e.g. missing ;, will generate lots of error messages

can you post your code ?
select < CODE/ > and paste text where it says “type or paste code here”

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