I've been playing with the code for hours. It originally started as AnalogInOutSerial. I started down this rabbit hole trying to get the serial monitor to refresh on a constant rather than waiting for the LED (pin 11) to flash.
I have the LED set up the way I want, it delays a flash based on position of the pot. But I want to untether the display so that it displays the pot position without delay.
I thought if I put the LED code was moved to setup then the loop would run without delay but now I'm at a loss.
Any guidance would be appreciated...
Thanks
/*
http://www.arduino.cc/en/Tutorial/AnalogInOutSerial
*/
const int analogInPin = A0;
const int analogOutPin = 11;
unsigned long sensorValue = 0;
unsigned long outputValue = 0;
void setup() {
Serial.begin(9600);
}
void sensorValue11 ()
{
sensorValue = analogRead(analogInPin) * 10;
digitalWrite(analogOutPin, HIGH);
delay(15);
digitalWrite(analogOutPin, LOW);
delay(sensorValue);
/*
void screenLoop ()
{
}
*/
}
void loop() {
if ( (millis () - sensorValue) >= sensorValue)
sensorValue11 ();
analogWrite(analogOutPin, outputValue);
Serial.print("Sensor = ");
Serial.print(sensorValue);
Serial.print("\t Seconds = ");
Serial.println(sensorValue / 100);
/*
sensorValue = analogRead(analogInPin) * 10;
digitalWrite(analogOutPin, HIGH);
delay(15);
digitalWrite(analogOutPin, LOW);
delay(sensorValue);
*/
}
A without-delay example would be modeled as follows (compiled, not tested...)
/*
http://www.arduino.cc/en/Tutorial/AnalogInOutSerial
*/
const uint8_t analogInPin = A0;
const uint8_t analogOutPin = 11;
unsigned long sensorValue = 0;
unsigned long outputValue = 0;
void setup()
{
Serial.begin(9600);
pinMode( analogOutPin, OUTPUT );
}
void loop()
{
ReadSensor();
BlinkLED();
}//loop
void ReadSensor( void )
{
static uint32_t
timeSensor=0;
uint32_t
timeNow = millis();
//has 100mS passed since the last sensor read?
if ( (timeNow - timeSensor) >= 100ul )
{
//yes; reset the timer
timeSensor = timeNow;
//read the analog chan and mul by 10 to get an off delay value
sensorValue = analogRead(analogInPin) * 10;
//print the value
Serial.print("Sensor = ");
Serial.println( sensorValue );
}//if
}//ReadSensor
void BlinkLED( void )
{
static uint8_t
bLEDState = false;
static uint32_t
timeDelay=0,
timeLED=0;
uint32_t
timeNow = millis();
//update the "off" time every pass
//if delay is set long and user rotates pot to low value, we
//want the delay to change as he turns it rather than waiting
//until the last recorded period times out
if( bLEDState == LOW )
timeDelay = sensorValue;
//has the elapsed time delay passed?
if( timeNow - timeLED >= timeDelay )
{
//yes. toggle the flag
bLEDState ^= HIGH;
//if the LED will be 'on' now...
if( bLEDState )
//...set a 15mS on time
timeDelay = 15ul;
else
//...else set off time based on sensorValue
timeDelay = sensorValue;
//write the new state to the LED
digitalWrite( analogOutPin, bLEDState );
}//if
}//BlinkLED
Blackfin:
A without-delay example would be modeled as follows (compiled, not tested...)
/*
http://www.arduino.cc/en/Tutorial/AnalogInOutSerial
*/
const uint8_t analogInPin = A0;
const uint8_t analogOutPin = 11;
unsigned long sensorValue = 0;
unsigned long outputValue = 0;
void setup()
{
Serial.begin(9600);
pinMode( analogOutPin, OUTPUT );
}
void loop()
{
ReadSensor();
BlinkLED();
}//loop
void ReadSensor( void )
{
static uint32_t
timeSensor=0;
uint32_t
timeNow = millis();
//has 100mS passed since the last sensor read?
if ( (timeNow - timeSensor) >= 100ul )
{
//yes; reset the timer
timeSensor = timeNow;
//read the analog chan and mul by 10 to get an off delay value
sensorValue = analogRead(analogInPin) * 10;
//print the value
Serial.print("Sensor = ");
Serial.println( sensorValue );
}//if
}//ReadSensor
void BlinkLED( void )
{
static uint8_t
bLEDState = false;
static uint32_t
timeDelay=0,
timeLED=0;
uint32_t
timeNow = millis();
//update the "off" time every pass
//if delay is set long and user rotates pot to low value, we
//want the delay to change as he turns it rather than waiting
//until the last recorded period times out
if( bLEDState == LOW )
timeDelay = sensorValue;
//has the elapsed time delay passed?
if( timeNow - timeLED >= timeDelay )
{
//yes. toggle the flag
bLEDState ^= HIGH;
//if the LED will be 'on' now...
if( bLEDState )
//...set a 15mS on time
timeDelay = 15ul;
else
//...else set off time based on sensorValue
timeDelay = sensorValue;
//write the new state to the LED
digitalWrite( analogOutPin, bLEDState );
}//if
}//BlinkLED
Thank you!! I have a basic understanding of what you did. I uploaded the code and the LED flashes once and then stays on constantly, no matter how I swipe the pot.
I'm going over the code and changing things to see if I could get it figured out but no such luck...
Oops. Please add the line indicated:
.
.
.
//has the elapsed time delay passed?
if( timeNow - timeLED >= timeDelay )
{
timeLED = timeNow; //<-------------add this line
//yes. toggle the flag
bLEDState ^= HIGH;
.
.
.
Yes, that worked, now I have to wrap my head around it... Thank you!!!