AWOL:
analogWrite(3,0);Already I'm seeing problems.
what problems do you see?
I finished my example and here is the working sketch:
void setup()
{
Serial.begin(57600);
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(2, INPUT);
}
void loop(){
if (digitalRead(2)){ // slow down or trun off PWM
analogWrite(3,0);
analogWrite(5,max(0,analogOutputReadPWM(5)-10));
analogWrite(6,max(0,(analogOutputReadPWM(6)*.5)-1)); // lets ramp Down the PWM to 0 with some decelleration
}
else { // Speed up PWM
analogWrite(3,min(255,analogOutputReadPWM(3)+1)); // lets ramp up the PWM to 255 by 1
analogWrite(5,min(255,analogOutputReadPWM(5)+5)); // lets ramp up the PWM to 255 by 5
analogWrite(6,min(255,(analogOutputReadPWM(6)*1.5)+1)); // lets ramp up the PWM to 255 with some acceleration
}
if(analogRead(A4)>=500){
analogWrite(5,max(0,analogOutputReadPWM(5)-5)); //this would cancel the Speed up PWM if analogRead shows a value less than 500
}
delay(100);
Serial.print(" PWM 3 value:");
Serial.print(analogOutputReadPWM(3));
Serial.print(" PWM 5 value:");
Serial.print(analogOutputReadPWM(5));
Serial.print(" PWM 6 value:");
Serial.print(analogOutputReadPWM(6));
Serial.println("");
delay(500);
}
// THE FOLLOWING FUNCIOTN IS DERIVED FROM:
/*
wiring_analog.c - analog input and output
Part of Arduino - http://www.arduino.cc/
Copyright (c) 2005-2006 David A. Mellis
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
Modified 28 September 2010 by Mark Sproul
$Id: wiring.c 248 2007-02-03 15:36:30Z mellis $
*/
// Right now, PWM output only works on the pins with
// hardware support. These are defined in the appropriate
// pins_*.c file. For the rest of the pins, are default
// to digital output.
byte analogOutputReadPWM(uint8_t pin){
byte val;
switch(digitalPinToTimer(pin)) // returns timer location if it is on a timer see NOT_ON_TIMER
{
// XXX fix needed for atmega8
#if defined(TCCR0) && defined(COM00) && !defined(__AVR_ATmega8__)
case TIMER0A:
val = OCR0; // get pwm duty
break;
#endif
#if defined(TCCR0A) && defined(COM0A1)
case TIMER0A:
val = OCR0A; // get pwm duty
break;
#endif
#if defined(TCCR0A) && defined(COM0B1)
case TIMER0B:
val = OCR0B; // get pwm duty
break;
#endif
#if defined(TCCR1A) && defined(COM1A1)
case TIMER1A:
val = OCR1A; // get pwm duty
break;
#endif
#if defined(TCCR1A) && defined(COM1B1)
case TIMER1B:
val = OCR1B; // get pwm duty
break;
#endif
#if defined(TCCR2) && defined(COM21)
case TIMER2:
val = OCR2; // get pwm duty
break;
#endif
#if defined(TCCR2A) && defined(COM2A1)
case TIMER2A:
val = OCR2A; // get pwm duty
break;
#endif
#if defined(TCCR2A) && defined(COM2B1)
case TIMER2B:
val = OCR2B; // get pwm duty
break;
#endif
#if defined(TCCR3A) && defined(COM3A1)
case TIMER3A:
val = OCR3A; // get pwm duty
break;
#endif
#if defined(TCCR3A) && defined(COM3B1)
case TIMER3B:
val = OCR3B; // get pwm duty
break;
#endif
#if defined(TCCR3A) && defined(COM3C1)
case TIMER3C:
val = OCR3C; // get pwm duty
break;
#endif
#if defined(TCCR4A)
case TIMER4A:
val = OCR4A; // get pwm duty
break;
#endif
#if defined(TCCR4A) && defined(COM4B1)
case TIMER4B:
val = OCR4B; // get pwm duty
break;
#endif
#if defined(TCCR4A) && defined(COM4C1)
case TIMER4C:
val = OCR4C; // get pwm duty
break;
#endif
#if defined(TCCR4C) && defined(COM4D1)
case TIMER4D:
val = OCR4D; // get pwm duty
break;
#endif
#if defined(TCCR5A) && defined(COM5A1)
case TIMER5A:
val = OCR5A; // get pwm duty
break;
#endif
#if defined(TCCR5A) && defined(COM5B1)
case TIMER5B:
val = OCR5B; // get pwm duty
break;
#endif
#if defined(TCCR5A) && defined(COM5C1)
case TIMER5C:
val = OCR5C; // get pwm duty
break;
#endif
case NOT_ON_TIMER:
default:
val = digitalRead(pin);
}
return(val);
}