Hello, I'm new to arduino and I need this code, I can't try it out since I don't have the items from this, they are at the scholl. All the ? markers is simply because I dont have that information before tomorrow.
This code should calibrate it's own max and min distance after that an DC motor (AnalogDC) should go backwards or forwards depending on the readed distance.
Also KDC Should run forward only.
Last thing, ignore the // parts, those are in danish and are only nodes to me.
// Disse er konstante
const int AnalogIn = ?;
const int AnalogDC = ?;
const int KDC = ?;
const int threshhold = ?;// Distancen skal findes
const int Led = ?;
// ? skal skiftes ud med den pin som de sidder på.
// Variere sammen med kalibreringen i ved opstart.
int sensorValue =0;
int sensorMin = 1023;
int sensorMax = 0;
void setup (){
pinMode (AnalogDC, OUTPUT);
pinMode (AnalogIn, INPUT) ;
pinMode (KDC, OUTPUT) ;
// Får en LED til at tænde i kalibrerings perioden.
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
// 5000 giver 5 sekunder til kalibreringen.
while (millis()<5000) {
sensorValue = analogRead (AnalogIn);
// Definere max
if (sensorValue > sensorMax) {
sensorMax = sensorValue;
}
// Definere min
if (sensorValue < sensorMin) {
sensorMin = sensorValue;
}
}
// Får LED'en til at slå fra og vise at kalibreringen er overstået
digitalWrite(13, LOW);
}
void loop() {
//Læser distancen
int analogValue = analogRead (AnalogIn);
// Hvis kritereiet mødes køre moteren fremad
if (analogValue > threshold {
digitalWrite(AnalogDC, HIGH);
digitalWrite(KDC, HIGH);
}
else {
digitalWrite(AnalogDC, REVERSE);
digitalWrite(KDC, HIGH);
}
delay(1) //Tid mellem læsning