Is there a reason why my Arduino Uno freezes after a few minutes of running with power coming from a usb cable connected to my pc?
#include <GY6050.h>
#include <Wire.h>
#include <VarSpeedServo.h>
#define angle1 20
#define angle2 90
#define Sensor_BL A0
#define Sensor_BR A1
VarSpeedServo servo;
GY6050 gyro1(0x68);
GY6050 gyro2(0x69);
int X = 0;
int Y = 0;
int y1_axis = 0;
int y2_axis = 0;
int sensorBL;
int sensorBR;
int speed = 0;
int angle = 0;
int prev_y1_axis = 0;
int y1_hold = 0;
unsigned long timer;
int start = 1;
byte step = 0;
String steps , laststep;
int freq;
void setup() {
Serial.begin(9600);
Wire.begin();
gyro1.initialisation();
gyro2.initialisation();
delay(100);
servo.attach(8);
servo.write(90);
}
void loop() {
y1_axis = gyro1.refresh('A', 'Y');
y2_axis = gyro2.refresh('A', 'Y');
sensorBL = analogRead(Sensor_BL);
sensorBR = analogRead(Sensor_BR);
//0 right - 90 centr - 180 left
if (sensorBL > 250) {
speed = map( sensorBL , 0 , 1023 , 0 , 255);
if (sensorBL < 700) { //less pressure 90+20=110
servo.write(95 , speed);
angle = 95;
}
else if (sensorBL > 700) { //more pressre 90+90 = 180
servo.write(105 , speed);
angle = 105;
}
}
else if (sensorBR > 250) {
speed = map( sensorBR , 0 , 1023 , 0 , 255);
if (sensorBR < 700) {
servo.write(95 , speed);
angle = 95;
}
else if (sensorBR > 700) {
servo.write(105 , speed);
angle = 105;
}
}
else if ((start == 0 || millis() - timer > 800) && y1_axis > -90 && y1_axis < 20 && y2_axis > -90 && y2_axis < 20 && sensorBL < 10 && sensorBR < 20) //flat dead end no servo movement - center position - can we separate on different lines the pressure sensor and accell so that I can set the dead end value separately for the press sens and acc ?
{
servo.write( 85 , 15);
//Serial.println("Servo goes to zero");
start = 1;
step = 0;
angle = 0;
steps = "0";
}
else if ((y1_axis < 60 & y1_axis >40) && (step == 0 || step == 2))
{
step = 1;
steps = "1";
}
else if ((y2_axis < 55 & y2_axis > 40) && (step == 0 || step == 1))
{
step = 2;
steps = "2";
}
if (!steps.equals(laststep)) {
if ((step == 1 || step == 2) && millis() - timer <= 700) {
if (freq > 1) {
servo.write(70 , 20);
angle = 70;
}
else {
servo.write(78 , 20);
angle = 78;
}
freq++;
}
else if ((step == 1 || step == 2 && millis() - timer > 750)) {
servo.write(78 , 20);
angle = 78;
freq = 0;
}
timer = millis();
}
//Serial.println("Y1: " + String(y1_axis) + " Y2: " + String(y2_axis) + " S1: " + String(sensorBL) + " S2: " + String(sensorBR) + " Millis: " + String(millis() - timer) + " Step: " + String(steps) + " Last: " + String(laststep) + " Angle: " + String(angle));
Serial.println("Millis: " + String(millis() - timer) + " Angle: " + String(angle));
laststep = steps;
}
I am using the pc usb power cable to power on the arduino and 2 accelerometers and one SG90 hobby servomotor. Would the best way power on the 1 servo with its own power supply ?
What power supply would you suggest for 1 servomotor?
When not connected to the pc, I use 9v battery to power on together the arduino and the one servomotor
thanks!
It is the part of the code for moving forward and backward in this locomotion system I came up with
https://github.com/marcob2178/Arduino-Based-Game-Locomotion
https://www.youtube.com/watch?v=OYTlF2GhXpQ
Sensors are mounted on shoes