I'm trying to control the light output of an LED using a potentiometer connected to an analog pin on my Arduino Uno. The concept of project is that the depending on the serial value of the potentiometer (demonstrated on serial monitor), the LED will light up in a certain fashion (i.e. if serial/POT value is between X and Y, LED strobes at 1 flash/sec; between A and B, LED stays solid).
In addition to the thresholds for each light setting, I used the CurrentMillis command to constantly check the value of the POT- if the value of the POT changed during one of the light patterns, the Arduino should switch to the setting that fits within the correct threshold.
After setting up the thresholds for each light setting using if-statements, I tested my code by slowly turning my POT clockwise and counterclockwise (waiting a second or two between turns), however, the LED stays on. I even opened the serial monitor after uploading the code, and it was blank.
I would really appreciate it if someone can help me out with my code.
Thank You,
Mark
Here is my code:
#include <SoftwareSerial.h>
//#include <avr/interrupt.h>
#include <avr/io.h>
//#include <pins_arduino.h>
unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 1000; //the value is a number of milliseconds, ie 1 second
int RX = 2; // POT IN A2 PIN
int TX = 1; // LED PIN
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)
SoftwareSerial mySerial(TX, RX);
//byte TX = LOW; // used to record whether the LEDs are on or off
void setup(){
// initialize serial communications at 9600 bps:
mySerial.begin(9600);
pinMode(RX, INPUT);
pinMode(TX, OUTPUT);
startMillis = millis(); //initial start time
//PORTB = (1<<PB2);
//DDRB = (1<<DDB2);
// _NOP(); // Insert nop for synchronization
//i = PINB; // Read port pins
//#ifdef PCICR
//PCICR |= (1 << PCIE0); // set PCIE0 to enable PCMSK0 scan
//PCMSK0 |= (1 << PCINT2); // set PCINT0 to trigger an interrupt on state change
// sei(); // enables interrupts
}
void loop()
{
currentMillis = millis(); //get the current "time" (actually the number of milliseconds since the program started)
if (currentMillis - startMillis >= period) //test whether the period has elapsed
{
digitalWrite(TX, !digitalRead(TX)); //if so, change the state of the LED. Uses a neat trick to change the state
startMillis = currentMillis; //IMPORTANT to save the start time of the current LED state.
}
}
//========
void updateOnBoardLedState() {
/////////////////////////////////////////////////////////////////////////////////////
sensorValue = analogRead(RX);
Serial.println(sensorValue); // Read POT value on Serial Monitor
mySerial.write(sensorValue);
// map it to the range of the analog out:
outputValue = (sensorValue, 0, 1023);
////////////////////////////////////////////////////////////////////////////////////
if (1 < outputValue && outputValue < 10){ // Static Light Mode
digitalWrite(TX, HIGH); // turn the ledPin on
}
else if (30< outputValue && outputValue <90){ //Strobe Light Mode 1
digitalWrite(TX, HIGH);
delay (300);
digitalWrite(TX, LOW);
delay (150);
}
else if (95< outputValue && outputValue < 150){ //Strobe Light Mode 2
digitalWrite(TX, HIGH);
delay (300);
digitalWrite(TX, LOW);
delay (150);
digitalWrite(TX, HIGH);
delay (300);
digitalWrite(TX, LOW);
delay (150);
digitalWrite(TX, HIGH);
delay (300);
digitalWrite(TX, LOW);
delay (150);
digitalWrite(TX, HIGH);
delay (150);
digitalWrite(TX, LOW);
delay (75);
digitalWrite(TX, HIGH);
delay (150);
digitalWrite(TX, LOW);
delay (75);
digitalWrite(TX, HIGH);
delay (150);
digitalWrite(TX, LOW);
delay (75);
digitalWrite(TX, HIGH);
delay (300);
digitalWrite(TX, LOW);
delay (150);
digitalWrite(TX, HIGH);
delay (300);
digitalWrite(TX, LOW);
delay (150);
digitalWrite(TX, HIGH);
delay (300);
digitalWrite(TX, LOW);
delay (150);
}
else{
digitalWrite(TX,LOW);
}
// wait 2 milliseconds before the next loop for the analog-to-digital
// converter to settle after the last reading:
delay(2);
}