Robin2:
But whatever happens on the browser the Arduino needs to get the PWM value from somewhere and it is much better use of Arduino resources to store it in a variable rather than try and recreate it from the Timer registers....R
I love the insight I am getting.... Your comment you made about the registers made me think How is the arduino compiler actually setting the value for PWM it has to be somewhat easy and simple so I went looking and found what I was looking for. wiring_analog.c is where it is hiding.
so I came up with a function
poof "analouOutputReadPWM" was born ![]()
Here is the working code:
void setup()
{
Serial.begin(57600);
pinMode(11, OUTPUT);
analogWrite(11, 100);
}
void loop() {
delay(2000);
setPWM();
int MyPWMVal = analogOutputReadPWM(11); // this doesn't work but it is what i need to do... HOW can I do this and is it even possible?
Serial.print("My PWM Value is:");
Serial.println(MyPWMVal);
}
void setPWM(){ // represents special code setting the PWM output to some value
int x = random(0,255);
Serial.print("Lets set the PWM Value to:");
Serial.println(x);
analogWrite(11, x);
}
// 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);
}