Hello lads, my task is to make sweep(servo ) function repeat N times with a step X only if 180(degrees) can be divided by X(step) without reminder (example : if step is 5 (180/5=30, reminder=0)it asks how many times to do sweep) if step is 7 (180/7 = 25 ,reminder=5 so it just prints out reminder). My problem is that i don't know how to input N and X i somehow get stuck in infinity loop here is my code:
#include <Servo.h>
const int trigPin=3;
const int echoPin=4;
long time;
int distance;
Servo Servomot;
String n;
String x;
int i;
int calcdistance(){
digitalWrite(trigPin,LOW);
delayMicroseconds(2);
digitalWrite(trigPin,HIGH);
delayMicroseconds(10);
digitalWrite(trigPin,LOW);
time=pulseIn(echoPin,HIGH);
distance=time*0,034/2;
return distance;
}
void sweep(int k,int z){
for(int j=0;j<=k;j++)
{
for( i=0;i<=180;i=i+z)
Servomot.write(i);
delay(30);
distance=calcdistance();
Serial.println();
Serial.print("stepen:");
Serial.print(i);
Serial.print("°");
Serial.println();
Serial.print("udaljenost");
Serial.print(udaljenost);
Serial.print("cm");
}
for( i=180;i>0;i=i-z){
Servomot.write(i);
delay(30);
distance=calcdistance();
Serial.println();
Serial.print("stepen:");
Serial.print(i);
Serial.print("°");
Serial.println();
Serial.print("udaljenost");
Serial.print(udaljenost);
Serial.print("cm");
}
}
void setup(){
pinMode (trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
Servomot.attach(10);
Serial.println("Insert step");
}
void loop(){
if(Serial.available()){
x=Serial.readStringUntil("\n");
int f = x.toInt();
if(180 % f==0){
Serial.println("insert N");
if(Serial.available()){
n=Serial.readStringUntil("\n");
int l=n.toInt
sweep(l,f);
}
}
else {
Serial.println("");
}
}
}
Sorry if the question may seem stupid, but im new to programming and arduino overall, and thanks.