but i want to know your opinion , in how to make the code better .
For one
thing, it
looks like
it was written
by a drunken
sailor.
Use Tools + Auto Format and sober it up.
Second, code needs to be attached only when it exceeds the 9500 character limit. Yours would not have.
//Pinet e Stepperit
int dirpin = 2;
int steppin = 3;
//Matja e distances
#define sensorIR A0
float sensorValue , cm;
void setup ()
{
pinMode(dirpin, OUTPUT);
pinMode(steppin, OUTPUT);
//Matja e afishuar ne serial monitor
Serial.begin(9600);
}
void loop ()
{
sensorValue=analogRead(sensorIR);
cm=10650.08*pow(sensorValue, -0.935)-10;
delay(50);
Serial.print(" cm");
Serial.println(cm);
{
if(cm<20)
{
int i;
digitalWrite(dirpin, LOW);
delay(100);
for (i = 0; i<4000; i++)
{
digitalWrite(steppin, LOW);
digitalWrite(steppin, HIGH);
delayMicroseconds(500);
}
}
if(cm<30)
{
int i;
digitalWrite(dirpin, LOW);
delay(100);
for (i = 0; i<4000; i++)
{
digitalWrite(steppin, LOW);
digitalWrite(steppin, HIGH);
delayMicroseconds(500);
}
}
if(cm<40)
{
int i;
digitalWrite(dirpin, LOW);
delay(100);
for (i = 0; i<4000; i++)
{
digitalWrite(steppin, LOW);
digitalWrite(steppin, HIGH);
delayMicroseconds(500);
}
}
if(cm>70)
{
int i;
digitalWrite(dirpin, HIGH);
delay(100);
for (i = 0; i<4000; i++)
{
digitalWrite(steppin, LOW);
digitalWrite(steppin, HIGH);
delayMicroseconds(500);
}
}
if(cm>80)
{
int i;
digitalWrite(dirpin, HIGH);
delay(100);
for (i = 0; i<4000; i++)
{
digitalWrite(steppin, LOW);
digitalWrite(steppin, HIGH);
delayMicroseconds(500);
}
}
if(cm>80)
{
int i;
digitalWrite(dirpin, HIGH);
delay(100);
for (i = 0; i<4000; i++)
{
digitalWrite(steppin, LOW);
digitalWrite(steppin, HIGH);
delayMicroseconds(500);
}
}
if(cm>90)
{
int i;
digitalWrite(dirpin, HIGH);
delay(100);
for (i = 0; i<4000; i++)
{
digitalWrite(steppin, LOW);
digitalWrite(steppin, HIGH);
delayMicroseconds(500);
}
}
if(cm>100)
{
int i;
digitalWrite(dirpin, LOW);
delay(100);
for (i = 0; i<4000; i++)
{
digitalWrite(steppin, LOW);
digitalWrite(steppin, HIGH);
delayMicroseconds(500);
}
}
}
}
{
if(cm<20)
What is THAT curly brace for?
delay(100);
Completely f**king useless.
if(cm<20)
{
int i;
digitalWrite(dirpin, LOW);
delay(100);
for (i = 0; i<4000; i++)
{
digitalWrite(steppin, LOW);
digitalWrite(steppin, HIGH);
delayMicroseconds(500);
}
}
It seems to me that the number of steps to take should be based on the distance, not hardcoded to 4000 steps. You might have different ideas.
You have a lot of replicated code that belongs in a function.
You have a lot of replicated code that belongs in a function.
You have a lot of replicated code that belongs in a function.
You have a lot of replicated code that belongs in a function.
You have a lot of replicated code that belongs in a function.
You have a lot of replicated code that belongs in a function.
You have a lot of replicated code that belongs in a function.