Issues Controlling LED Using Serial Reading of Potentiometer

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);

}

Please follow the advice given in the link below when posting code , use code tags and post the code here to make it easier to read and copy for examination

Pin 1 appears to be being used for both the LED and SoftwareSerial. What is going on here ?
What have you got connected to the SoftwareSerial interface ?

By the way, in

SoftwareSerial mySerial(TX, RX);

you have the pin names reversed from the conventional way of naming them. The first parameter is the receive pin and the second is the transmit pin

I've tried this in the past and it seems unreliable depending on the platform on which its being used. It may be a "neat trick" but is it SO hard to maintain a "ledState" variable?

Please: strip all the garbage commented outs etc out and the

which as far as I can see is never called.

Why are you using SoftwareSerial?

Hello
I´ve read your post and the sketch.
I don´t know what your intention is to use an Adruino?
Please summarize in clear words and in short sentences your project.
p.s.
Take a view into the IDE exampe BWOD to replace these ammount of delays:

    Line 52:     delay (300);
	Line 54:     delay (150);
	Line 58:     delay (300);
	Line 60:     delay (150);
	Line 62:     delay (300);
	Line 64:     delay (150);
	Line 66:     delay (300);
	Line 68:     delay (150);
	Line 70:     delay (150);
	Line 72:     delay (75);
	Line 74:     delay (150);
	Line 76:     delay (75);
	Line 78:     delay (150);
	Line 80:     delay (75);
	Line 82:     delay (300);
	Line 84:     delay (150);
	Line 86:     delay (300);
	Line 88:     delay (150);
	Line 90:     delay (300);
	Line 92:     delay (150);
	Line 99:   delay(2);

Hi UKHeliBob, thanks for the tip on reversing RX and TX. I'm not sure which part of my code indicates Pin 1 is connected to the LED and SoftwareSerial. Can you please highlight the line (or lines)?

Thanks for the note on the LEDState. Are there any other more reliable ways to maintain the LEDState variable?

I'll also polish the code more (avoid the excessive comments)

I'm using SoftwareSerial to use the serial values of the POT to modify the LEDState using if-statements. Since the range is from 0 to 1023, I set a threshold between 0 and 10 for the first state, and other thresholds for the other LEDStates.

In a normal program the POT does not have a Serial value. It has an analog value from 0-1023 when you read it with analogRead(). It's that value, i.e. sensorValue that your if() statements should be checking.

There is no point messing with Software Serial at all. It does nothing useful if all you want to do is check the value from an analogRead(). I have a feeling that you don't understand what Serial and SoftwareSerial do at all.

Also those delays make your program very unresponsive . If you ever get into Strobe Light Mode 2 it will be more than 2 seconds before the pot is checked again.

Steve

This one

int TX = 1; // LED PIN

and this one

SoftwareSerial mySerial(TX, RX);

It looks to me as though you are very confused about the use of SoftwareSerial

mySerial.write(sensorValue);

What exactly do you think that this line does ?

I am a bit confused with SoftwareSerial...a friend of mine suggested using it for my project and I didn't have much background on it. To be honest, this is one of my first coding projects, and would really appreciate all the help/guidance I can get.

How do you think I should go about reading the analog values and using them in my if-statements?

Here is one of the 4 million+ pages on how to read a potentiometer, with an Arduino, on the internet.

Software serial is a method for asynchronous serial communication between 2 processors. It has nothing to do with analog read.

This might get you going...


unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 1000; //the value is a number of milliseconds, ie 1 second

const int potPin = A2; // POT IN A2 PIN
const int ledPin = 3; // LED PIN

enum { OFF_STATE, ON_STATE, STROBE1_STATE, STROBE2_STATE };
int state;

void setup() {

  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  startMillis = millis(); //initial start time
  state = OFF_STATE;
}

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
  {
    getState();
    startMillis = currentMillis; //IMPORTANT to save the start time of the current LED state.
  }
  updateOnBoardLedState();
}

//========

void getState() {

  int sensorValue = analogRead(potPin);
  Serial.println(sensorValue); // Read POT value on Serial Monitor

  switch (sensorValue) {
    case 0 ... 29 :
      state = ON_STATE;
      break;

    case 30 ... 90 :
      state = STROBE1_STATE;
      break;

    case 91 ... 150 :
      state = STROBE2_STATE;
      break;

    default :
      state = OFF_STATE;
  }
}

void updateOnBoardLedState() {

  switch (state) {
    case ON_STATE:
      digitalWrite(ledPin, HIGH); // turn the ledPin on
      break;

    case STROBE1_STATE:
      strobe1();
      break;

    case STROBE2_STATE:
      break;

    case OFF_STATE:
      digitalWrite(ledPin, LOW);
      break;
  }
}

void strobe1() {

  const byte maxSteps = 2;
  static unsigned long lastTime;
  static byte step;
  static unsigned long period;

  if ( currentMillis - lastTime >= period ) {
    lastTime = currentMillis;
    step++;
    if ( step >= maxSteps ) step = 0;
    switch (step) {
      case 0:
        digitalWrite(ledPin, HIGH);
        period = 300;
        break;

      case 1:
        digitalWrite(ledPin, LOW);
        period = 150;
        break;
    }
  }
}

void strobe2() {

  const byte maxSteps = 12;
  static unsigned long lastTime;
  static byte step;
  static unsigned long period;

  if ( currentMillis - lastTime >= period ) {
    lastTime = currentMillis;
    step++;
    if ( step >= maxSteps ) step = 0;
    switch (step) {
      case 0:
        digitalWrite(ledPin, HIGH);
        period = 300;
        break;

      case 1:
        digitalWrite(ledPin, LOW);
        period = 150;
        break;

      case 2:
        digitalWrite(ledPin, HIGH);
        period = 300;
        break;

      case 3:
        digitalWrite(ledPin, LOW);
        period = 150;
        break;

      case 4:
        digitalWrite(ledPin, HIGH);
        period = 300;
        break;

      case 5:
        digitalWrite(ledPin, LOW);
        period = 150;
        break;

      case 6:
        digitalWrite(ledPin, HIGH);
        period = 150;
        break;

      case 7:
        digitalWrite(ledPin, LOW);
        period = 75;
        break;

      case 8:
        digitalWrite(ledPin, HIGH);
        period = 150;
        break;

      case 9:
        digitalWrite(ledPin, LOW);
        period = 75;
        break;

      case 10:
        digitalWrite(ledPin, HIGH);
        period = 150;
        break;

      case 11:
        digitalWrite(ledPin, LOW);
        period = 75;
        break;
    }
  }
}