#include #define PWM 11 //PB3 #define left 9 //PB1 #define right 10 //PB2 #define minPWM 90 //minimal Duty-Cycle #define maxPWM 180 //maximal Duty-Cycle bool direction = true; int counter = 0; void setup() { // put your setup code here, to run once: pinMode(PWM , OUTPUT); digitalWrite(PWM,0); pinMode(left, OUTPUT); pinMode(right, OUTPUT); setDir(direction); TCCR2A = 0b10100011; TCCR2B = 0b00000010; OCR2A = 0; Serial.begin(9600); Serial.println("--- Beginn ---"); OCR2A = minPWM; delay(2000); OCR2A = 0; delay(2000); } void loop() { // put your main code here, to run repeatedly: Serial.println("slowUP"); slowUp(random(OCR2A, maxPWM), 15); delay(2000); Serial.println("slowDown"); slowDown(random(minPWM, OCR2A), 15); delay(2000); if (counter == 0){ direction = false; setDir(direction); } counter++; if (counter==10) { direction = true; setDir(direction); counter = 0; } } void setDir(bool dir) { Serial.print("Direction: "); if(dir){ Serial.println("<="); } else { Serial.println("=>"); } slowDown(0, 5); digitalWrite(PWM, 0); delay(100); if (dir) { digitalWrite(left, 0); digitalWrite(right, 1); } else { digitalWrite(right, 0); digitalWrite(left, 1); } delay(2000); TCCR2A = 0b10100011; TCCR2B = 0b00000010; OCR2A = 0; } void setSpeed(int value) { if (value > 254) { value = 254; } OCR2A = value; } void slowUp(int value, int d) { if (value > 254) { value = 254; } if (value < minPWM) { value = minPWM; } Serial.print("UpValue: "); Serial.println(value); for (int i = OCR2A; i <= value + 1; i++) { OCR2A = i; delay(d); } } void slowDown(int value, int d) { if (value != 0) { if (value < minPWM) { value = minPWM; } } Serial.print("DownValue: "); Serial.println(value); for (int i = OCR2A; i != value - 1; i--) { if (OCR2A < value) { break; } OCR2A = i; delay(d); } }