State Machine

I again,
you all were able to help me with my last question about running state machine and now i have had some time to sit down and play with it some.
So here I am, I am able to set an input, resistor that will become a thermistor, I can start and stop the state but when I stop the state the LED continues to flash, not as bright but it still flashes.
I look at the serial data, all looks good, tried a different ground on the board and tried different LED, I knew that would not change any thing but I am swinging in the dark! :slight_smile:
I hope I have used the proper terminology, as I am not a programmer!

Linc:
I am swinging in the dark! :slight_smile:

Without seeing your actual code, any advice I could give would pretty much be the same thing.

OK her is the code, I hope I do this right!

const int ledPin =  12;      // the number of the LED pin
const int thermistorPin = A0;
// Variables will change:
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 250;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT); 
  pinMode(thermistorPin, INPUT); 
 Serial.begin(9600); 
}

void loop()
{
   int sensorValue = analogRead(A0);
  // print out the value you read:
  Serial.println(sensorValue);
  delay(1);        // delay in between reads for stability
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the 
  // difference between the current time and last time you blinked 
  // the LED is bigger than the interval at which you want to 
  // blink the LED.
  if(analogRead(thermistorPin) >= 90) digitalWrite(ledPin,LOW); //blinking to show  it's getting  hot
  if(analogRead(thermistorPin) <45)  digitalWrite(ledPin,HIGH); //on all the  time to show you  have over heated
  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState == HIGH)
      ledState = LOW;
    else
      ledState = HIGH;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}
[code]

pretty much cut and pasted the code :)

[/code]

I see know semblance of a state machine in that code. You should have some sort of blinkLEDState variable that is checked before you even check the blinking LED interval.

if(analogRead(thermistorPin) >= 90) digitalWrite(ledPin,LOW); //blinking to show  it's getting  hot
if(analogRead(thermistorPin) <45)  digitalWrite(ledPin,HIGH);

Telling the LED to turn on or off doesn't make much of a difference when you go on to blink it no matter what. This is where you should be updating your state variable.

Arrch:
I see know semblance of a state machine in that code. You should have some sort of blinkLEDState variable that is checked before you even check the blinking LED interval.

if(analogRead(thermistorPin) >= 90) digitalWrite(ledPin,LOW); //blinking to show  it's getting  hot

if(analogRead(thermistorPin) <45)  digitalWrite(ledPin,HIGH);



Telling the LED to turn on or off doesn't make much of a difference when you go on to blink it no matter what. This is where you should be updating your state variable.

even with the second line omitted I am still having the light flash with out the input of the first line, am I making sense?

Linc:
even with the second line omitted I am still having the light flash with out the input of the first line, am I making sense?

I know what's happening because I can see it in the code. You need to follow the logic and realize why it's still blinking. There are two parts of code that interact with the LED, the analogRead lines and the BlinkWithoutDelay code. Neither of them, however, interact with each other. Your analogRead lines should be setting a variable that your BlinkWithoutDelay code can look at to know if it should be blinking or not.

OK that makes sense to me, now how do I fix it? :~

Is it in these two lines

 int sensorValue = analogRead(A0);
  // print out the value you read:
  Serial.println(sensorValue);
  delay(1);

Linc:
Is it in these two lines

 int sensorValue = analogRead(A0);

// print out the value you read:
  Serial.println(sensorValue);
  delay(1);

Those lines simply report a value to serial, they don't affect the state of the LED. You need to think about the logic for than 2 minutes. You only want your Blink code to run if(hint hint) you're analog value is within a certain threshold.

yea after looking at that, I was wrong there.

I moved this around but I am getting an error for "currentMills" not declared in this scope

I know what I want, just dont know how to write it

if(analogRead(thermistorPin) >= 90)
  unsigned long currentMillis = millis(); 
 if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis; 
  digitalWrite(ledPin,LOW);

Linc:
I moved this around but I am getting an error for "currentMills" not declared in this scope

Variables need to be declared before they can be used. In this instance, it should also be initialized with some sort of value. Take another look at the BlinkWithoutDelay example, it should show you where to declare it and how to put a value in it.

the "unsinged long currentmills" needed to be defined in the beginning after the "long previousMills=0 with a value and I used 0

I now have it working! thanks for taking your time to led me to the proper answer! XD
Now to go uot the the car, hook up the laptop read the thermistor, with the analog read serial to make sure i have it dialed in right.
once again thanks!