Close

~ code of my project ~

A project log for S3P ~ supercaps and solar panels ~

The powersolution for your trip to the wilderness!!

kirschner-christophKirschner Christoph 03/30/2017 at 17:203 Comments

If you have any improvements to my code please tell me !! :) I`m still quite a noob in programming so it would be great if you guys could help me and tell me what i can do better or i did wrong!!

Best regards!!

Down there you can see my "code":


#include <Servo.h>

Servo xServo;
int positionServo = 90; // Anfangsposition und Speicher für Gradstellung des Servos / startposition

int const DCMotorNegative = 8;
int const DCMotorPositive = 7;
int const endPosition1 = 6;
int const endPosition2 = 5;
int const movingOFF = 4; 

int DCaccuracy = 5;
int servoAccuracy = 8;

int endPositionState1 = LOW;
int endPositionState2 = LOW;

int lastEndPositionState1 = LOW;   // the previous reading from the input pin
int lastEndPositionState2 = LOW;   // the previous reading from the input pin

long lastDebounceTime1 = 0;  // the last time the output pin was toggled
long lastDebounceTime2 = 0;  // the last time the output pin was toggled
long debounceDelay = 30;    // the debounce time; increase if the output flickers


//Prädeklaration
void servoTurning ();
void endPositionReading ();
void DCTurning ();


void setup() {

  Serial.begin(9600);
  xServo.attach(9);
  xServo.write(positionServo);
 
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
  pinMode(A3, INPUT);
  pinMode(A4, INPUT);
  pinMode(A5, INPUT);
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  pinMode(movingOFF, INPUT_PULLUP);
  pinMode(10, INPUT);
  pinMode(11, INPUT);
  pinMode(12, INPUT);
  pinMode(13, INPUT);
  pinMode(DCMotorPositive, OUTPUT); // Pin 7
  pinMode(DCMotorNegative, OUTPUT); // Pin 8
  pinMode(endPosition1, INPUT); // Pin 6
  pinMode(endPosition2, INPUT); // Pin 5
  void endPositionReading ();

}

void loop() {

  
// when D4 is LOW (Switch) all movings stop and the voltages of the caps get measured
  while ( digitalRead(movingOFF) == LOW ) {
    delay(1000);
    float voltage = 0;
    voltage = analogRead(A0);
    voltage = map(voltage, 0, 1023, 0, 5000);
    voltage = voltage  / 87 ;
    Serial.print ("Spannung:  ");
    Serial.print (voltage);
    Serial.println ("  V");
  }

  int WertA1 = analogRead(A1); // Auslesen der / reading of LDRs A1-A4
  int WertA2 = analogRead(A2);
  int WertA3 = analogRead(A3);
  int WertA4 = analogRead(A4);
  // int WertA5 = analogRead(A5);

  if ( abs(WertA2 - WertA3) < abs(WertA1 - WertA4) ) {
    servoTurning ();
    DCTurning ();
  }
  else if ( abs (WertA2 - WertA3) > abs (WertA1 - WertA4)) {
    DCTurning ();
    servoTurning ();
  }

  // if ( WertA5 != analogRead(A5)) {
  //   Serial.println (analogRead(A5));
  // }
  delay(25);

}

void DCTurning () {

  // Auslesen der LDRs A2, A4 / reading of A2 and A4

  int WertA2 = analogRead(A2);
  int WertA4 = analogRead(A4);


 //wenn die Differenz von A2 und A4 größer als der spezifische Toleranzbereich ist und der Endpositionsschalter nicht betätigt wurde, dann bewegt sich der Motor in die jeweilige Richtung / comparing A2 and A4 
  if ( (WertA2 - WertA4) > DCaccuracy ) {
    endPositionReading ();
    if ( endPositionState2 == HIGH)
    { digitalWrite(DCMotorPositive, HIGH);
      delay (10);
      digitalWrite(DCMotorPositive, LOW);
    }
    
  }
  if ( (WertA2 - WertA4) < - DCaccuracy ) {
    endPositionReading ();
    if ( endPositionState1 == HIGH)
    { digitalWrite(DCMotorNegative, HIGH);
      delay (10);
      digitalWrite(DCMotorNegative, LOW);
    }
    
  }

}

void servoTurning () {
  int OldPositionServo;
  OldPositionServo = positionServo;
  int WertA1 = analogRead(A1); // Auslesen der LDRs / reading of A1, A3, A5
  int WertA3 = analogRead(A3);
  int WertA5 = analogRead(A5);

  //Serial.print ("A1  ");
  //Serial.println (WertA1);
  //Serial.print ("A2  ");
  //Serial.println (WertA2);
  //Serial.print ("A3  ");
  //Serial.println (WertA3);
  //Serial.print ("A4  ");
  //Serial.println (WertA4);
  //Serial.println ();

  //Vergleicht die LDrs A1 und A3, je nach Wert wird die Gradzahl erhöht oder erniedrigt / comparing A1 and A3
  if ( positionServo > 0 && ((WertA1 - WertA3) > servoAccuracy) && WertA5 < 100) {

    positionServo = positionServo - 1 ;
    DCaccuracy = 5;
    
  }


  //Erhöhen der Genauigkeit des DC-Motors, wenn Servo bei 0° / increasing dc-motor accuracy when reaching 0°
  if ( positionServo == 0 && ((WertA1 - WertA3) > servoAccuracy)) {

    DCaccuracy = 1;
    
  }




  if ( positionServo < 180 && ((WertA1 - WertA3 ) > servoAccuracy) && WertA5 > 1000) {

    positionServo = positionServo + 1 ;
    DCaccuracy = 5;
    
  }


  // //Erhöhen der Genauigkeit des DC-Motors, wenn Servo bei 180° / increasing accuracy of the dc-motor when reaching 180°
  if ( positionServo == 180 && ((WertA1 - WertA3 ) > servoAccuracy)) {

    DCaccuracy = 1;

  }



  if  ( positionServo < 180 && ((WertA1 - WertA3) < - servoAccuracy) && WertA5 < 100) {

    positionServo = positionServo + 1 ;
    DCaccuracy = 5;
    
  }

  if  ( positionServo > 0 && ((WertA1 - WertA3) < -servoAccuracy) && WertA5 > 1000) {

    positionServo = positionServo - 1 ;
    DCaccuracy = 5;
   
  }

  if ( OldPositionServo != positionServo ) {
    //Serial.print ("positionServo:  ");
    //Serial.println (positionServo);
    xServo.write(positionServo);
  }

}


void endPositionReading () {

  // read the state of the switch into a local variable:
  int reading1 = digitalRead(endPosition1);
  int reading2 = digitalRead(endPosition2);

  //Entprellen Endpositionstaster 1
  // If the switch changed, due to noise or pressing:
  if (reading1 != lastEndPositionState1) {
    // reset the debouncing timer
    lastDebounceTime1 = millis();
  }

  if ((millis() - lastDebounceTime1) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading1 != endPositionState1) {
      endPositionState1 = reading1;


    }
  }

  //Entprellen Positionstaster 2
  // If the switch changed, due to noise or pressing:
  if (reading2 != lastEndPositionState2) {
    // reset the debouncing timer
    lastDebounceTime2 = millis();
  }

  if ((millis() - lastDebounceTime2) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading2 != endPositionState2) {
      endPositionState2 = reading2;

    }

  }
  // save the reading.  Next time through the loop,
  // it'll be the lastButtonState:
  lastEndPositionState1 = reading1;
  lastEndPositionState2 = reading2;

  //Anpassen der Servo-Genauigkeit bei Anschlag der Endpositionstaster / increasing servo accuracy when reaching endposition switch
  if ( endPositionState1 == LOW || endPositionState2 == LOW ) {
    servoAccuracy = 1;
  } else {
    servoAccuracy = 8;
  }

}

Discussions

Stefan-Xp wrote 03/30/2017 at 20:11 point

A nice read :) looks not that bad so far. Here what i would like to mention

- I don't get why you setup those inputs in the Setup with actual numbers and no constants.

- From the code i have no idea why you calculate the voltage like that

- What is about this:

if ( abs(WertA2 - WertA3) < abs(WertA1 - WertA4) ) {
servoTurning ();
DCTurning ();
}
else if ( abs (WertA2 - WertA3) > abs (WertA1 - WertA4)) {
DCTurning ();
servoTurning ();
}

What is if those values are equal? ;-) and why is it important in which order those functions are executed? You might at least save some computing time and use "else" only

- Keep less than 80 columns for better readability

- It looks a bit like you just put more and more code onto this. Did you a program flow diagram for this application? For such stuff PAP Designer https://www.heise.de/download/product/papdesigner-51889 is amazing. 

- What is switch D4 for?

- Why do you read the Analog Voltages of the LDRs in the Main Loop if you read them again in the sub functions?

- In general it would be efficient to name function and variables like what they are doing. F. Ex.:

servoTurning ==> "turnServo" or even "findHorizontalPosition"

DCTurning ==> "turnDcMotor" or even "findVerticalPosition"

WertA1 ==> "ValueLdr1" or "ValueLeftHorizontalLdr"

- Have you done some thoughts what hapens from dusk till down? ;) 

- Regarding Power saving it might be effective to shut down the Servo Supply and goto sleep mode while there is no adjustment neccessary. You might want to just adjust positions every x minutes.

I hope you like my thoughts. Please don't take them as offence.

  Are you sure? yes | no

Kirschner Christoph wrote 03/30/2017 at 20:58 point

Ehm and switch for is for stopping the movement f. E. In case of uploading new code or something like that 

  Are you sure? yes | no

Kirschner Christoph wrote 03/30/2017 at 20:59 point

this is exactly what im searching for !! Thanks !! I'm "quite" new in this hole thing and i'm fighting my way trough this :D

  Are you sure? yes | no