Today I added two transistors to my prototyping-shield. Their purpose is to cut down the energy-consumption of the servo and the DC-motor, if their are not in use.
In order to get faster and better results I also did some coding.
Special thanks goes out to @Stefan-Xp who gave me a few really good advices. I hope the programming got a little bit better. :D
Here you can see the result:
#include <Servo.h>
Servo xServo;
int positionServo = 90; // Anfangsposition und Speicher für Gradstellung des Servos / startposition
//Pin-Belegung
int const DCMotorNegative = 8;
int const DCMotorPositive = 7;
int const endPosition1 = 6;
int const endPosition2 = 5;
int const movingOFF = 4;
int const powerServo = 3;
int const powerDCmotor = 2;
// Sensorwerte / values of the sensors
int valueLDR1 = 0;
int valueLDR2 = 0;
int valueLDR3 = 0;
int valueLDR4 = 0;
int valueTiltSwitch = 0;
// Präzision der Motoren / accuracy of the motors
int DCaccuracy = 5;
int servoAccuracy = 8;
//Endpositionstaster-Speicher / endposition memory
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
// Zeitkonstanten/ timevalues
long lastAdjustmentTime = 0;
long sleepTime = 60000;
long activeTime = 80000;
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 turnServo ();
void endPositionReading ();
void turnDC ();
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(powerDCmotor, OUTPUT);
pinMode(powerServo, OUTPUT);
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 ();
digitalWrite(powerServo, LOW); // schaltet Spannung ab / cuts down power
digitalWrite (powerDCmotor, LOW);
}
void loop() {
// 60s Schlafzustand, 20s aktiv/ 60s sleeping time, 20s active
lastAdjustmentTime = millis();
if ( (millis() - lastAdjustmentTime) > sleepTime ) {
while ( (millis() - lastAdjustmentTime) < activeTime ) {
// Anhalten der Bewegung, Messen der Kondensatorspannung / stopping movement, measure capacitor-voltage
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");
}
//Auslesen der LDRs und des Kipptasters / reading of the LDRs and of the tilt switch
valueLDR1 = analogRead (A1);
valueLDR2 = analogRead (A2);
valueLDR3 = analogRead (A3);
valueLDR4 = analogRead (A4);
valueTiltSwitch = analogRead (A5);
turnDC ();
turnServo ();
delay(25);
}
}
}
void turnDC () {
//Vergleichen von LDR2 und LDR4, je nachdem dann folgt Bewegung / comparing LDR2 and LDR4
if ( (valueLDR2 - valueLDR4) > DCaccuracy ) {
digitalWrite (powerDCmotor, HIGH);
endPositionReading ();
if ( endPositionState2 == HIGH)
{ digitalWrite(DCMotorPositive, HIGH);
delay (10);
digitalWrite(DCMotorPositive, LOW);
}
digitalWrite (powerDCmotor, LOW);
}
if ( (valueLDR2 - valueLDR4) < - DCaccuracy ) {
digitalWrite (powerDCmotor, HIGH);
endPositionReading ();
if ( endPositionState1 == HIGH)
{ digitalWrite(DCMotorNegative, HIGH);
delay (10);
digitalWrite(DCMotorNegative, LOW);
}
digitalWrite (powerDCmotor, LOW);
}
}
void turnServo () {
int OldPositionServo;
OldPositionServo = positionServo;
//Vergleichen von LDR1 und LDR3/ comparing LDR1 and LDR3,
if ( positionServo > 0 && ((valueLDR1 - valueLDR3) > servoAccuracy) && valueTiltSwitch < 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 && ((valueLDR1 - valueLDR3) > servoAccuracy)) {
DCaccuracy = 1;
}
if ( positionServo < 180 && ((valueLDR1 - valueLDR3 ) > servoAccuracy) && valueTiltSwitch > 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 && ((valueLDR1 - valueLDR3 ) > servoAccuracy)) {
DCaccuracy = 1;
}
if ( positionServo < 180 && ((valueLDR1 - valueLDR3) < - servoAccuracy) && valueTiltSwitch < 100) {
positionServo = positionServo + 1 ;
DCaccuracy = 5;
}
if ( positionServo > 0 && ((valueLDR1 - valueLDR3) < -servoAccuracy) && valueTiltSwitch > 1000) {
positionServo = positionServo - 1 ;
DCaccuracy = 5;
}
if ( OldPositionServo != positionServo ) {
digitalWrite (powerServo, HIGH);
//Serial.print ("positionServo: ");
//Serial.println (positionServo);
xServo.write(positionServo);
digitalWrite (powerServo, LOW);
}
}
void endPositionReading () {
int reading1 = digitalRead(endPosition1);
int reading2 = digitalRead(endPosition2);
//Entprellen Endpositionstaster 1
if (reading1 != lastEndPositionState1) {
lastDebounceTime1 = millis();
}
if ((millis() - lastDebounceTime1) > debounceDelay) {
if (reading1 != endPositionState1) {
endPositionState1 = reading1;
}
}
//Entprellen Positionstaster 2
if (reading2 != lastEndPositionState2) {
lastDebounceTime2 = millis();
}
if ((millis() - lastDebounceTime2) > debounceDelay) {
if (reading2 != endPositionState2) {
endPositionState2 = reading2;
}
}
lastEndPositionState1 = reading1;
lastEndPositionState2 = reading2;
//Anpassen der Servo-Genauigkeit / increasing servo accuracy when reaching endposition switch
if ( endPositionState1 == LOW || endPositionState2 == LOW ) {
servoAccuracy = 1;
} else {
servoAccuracy = 8;
}
}
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.
Looks much better for me. Congratulations!
Are you sure? yes | no
Thanks for your advices !! :)
Are you sure? yes | no