HI everyone. first time posting, long time user.
Im using the nano rp2040 for a project for the first time now. I have 15 of them wich where previously flashed for use with python (OpenMV). This time however im using them with my students and we are running arduino IDE. So i connect the rp2040 with the Arduino IDE running and it gives me a heads up, and flashes the correct firmware it seems. I can program through the IDE interface and upload and run programs as pr. usual.
However my program wont run unless i have the computer connected and am running the Arduino IDE. This is a problem as it is supposed to be used as a servo controller for two small servos. i have tried uploading the Blink.ino.elf.uf2 via bootloader mode, wich does work and start automatically on powerup, and that leads me to believe it may be a simple programming issue.
the following is the code im using:
#include <Arduino_LSM6DSOX.h>
#include <Servo.h>
Servo myservo_x; // create servo object to control a servo' X
Servo myservo_y; // create servo object to control a servo' Y
float Ax, Ay, Az;
float Gx, Gy, Gz;
int rx;
int ry;
int valx;
int valy;
void setup() {
Serial.begin(9600);
myservo_x.attach(2); // attaches the servo on pin 2 to the servo object
myservo_y.attach(3); // attaches the servo on pin 3 to the servo object
while(!Serial);
if (!IMU.begin()) {
Serial.println("Failed to initialize IMU!");
while (1);
}
Serial.print("Accelerometer sample rate = ");
Serial.print(IMU.accelerationSampleRate());
Serial.println("Hz");
Serial.println();
Serial.print("Gyroscope sample rate = ");
Serial.print(IMU.gyroscopeSampleRate());
Serial.println("Hz");
Serial.println();
}
void loop() {
if (IMU.accelerationAvailable()) {
IMU.readAcceleration(Ax, Ay, Az);
}
rx = Ax*1000;
ry = Ay*1000;
valx = map(rx, -1000, 1000, 0, 180); // scale it to use it with the servo (value between 0 and 180)
valy = map(ry, -1000, 1000, 0, 180);
myservo_x.write(valx);
myservo_y.write(valy); // sets the servo position according to the scaled value
Serial.println("Accelerometer data: ");
Serial.print(Ax);
Serial.print('\t');
Serial.print(Ay);
Serial.print('\t');
Serial.print(rx);
Serial.print('\t');
Serial.print(ry);
Serial.print('\t');
Serial.print(valx);
Serial.print('\t');
Serial.print(valy);
Serial.print('\t');
delay(100);
}