Noob Arduino owner, make a blinking light go solid given analog input

Hello,

I am a new Arduino user and I'm very excited to try it out on a small project. My goal is to link my arduino circuit to a force sensitive resistor (FSR) and have a blinking LED on standby when there is no force detected. Upon application of force, I'd like the LED to stop blinking and emit a solid light while the force is applied.

I've been looking at some example sketches for blinking light tutorials but I'm having trouble integrating the analog code I've been using into those examples. Is anybody able to nudge me in the right direction?

Here is my code so far:

 /* FSR testing sketch.
Connect one end of FSR to 5V, the other end to Analog 0.
Then connect one end of a 10K resistor from Analog 0 to ground
Connect LED from pin 13 through a resistor to ground */
 
int fsrAnalogPin = 0; // FSR is connected to analog 0
int LEDpin = 13;      // connect Red LED to pin 13 (PWM pin)
int fsrReading;      // the analog reading from the FSR resistor divider
int LEDbrightness;
 
void setup(void) {
  Serial.begin(9600);   // We'll send debugging information via the Serial monitor
  pinMode(LEDpin, OUTPUT);
}
 
void loop(void) {
  fsrReading = analogRead(fsrAnalogPin);
  Serial.print("Analog reading = ");
  Serial.println(fsrReading);
 
  // we'll need to change the range from the analog reading (0-1023) down to the range
  // used by analogWrite (0-255) with map!
  LEDbrightness = map(fsrReading, 0, 1023, 0, 255);
  // LED gets brighter the harder you press
  analogWrite(LEDpin, LEDbrightness);
 
  delay(1);
}

Pin 13 is not a PWM pin.

Kudos for code tags on first post.

That is not your code, the code is provided by Adafruit and the only changes you’ve made is a pin number and a delay.

As a new arduino user, I've been referring to sample code to get my bearings with programming. I did not mean to say that I created this code myself, I'm just trying to understand it and modify it.

You will need an LED and resistor connected to a PWM pin (3,4,5,9,10,11 on an Uno) and change the led pin to suit.

There is no blink in that code.

I don't know how to add a blink while keeping the analog part of the code intact.

Armadillo1:
I don't know how to add a blink while keeping the analog part of the code intact.

These threads may help Using millis() for timing. A beginners guide, Several things at the same time and look at the BlinkWithoutDelay example in the IDE.

You need to acquire some experience.

There are a myriad of examples that comes with your IDE.

To learn about Arduino C++ programming and hardware go through these examples.

OK, you have 2 things that you want to happen depending on the state of the FSR output. So set a threshold that the FSR force must exceed. If the force is lower than the threshold, blink the LED and if higher than the threshold fad the LED.

Here is a sketch that illustrates the above. The program is broken into functions. The function that is called depends on the FSR reading. Reading under 100 (change to suit) calls the blink function. Above 100 calls the fade function. Note that there is no delay in the blink function. It uses the blink without delay method (see the beginner's millis() tutorial).

LED connected to pin 6, change to suit, but must be a PWM pin (~ by the pin number on the board).

const byte ledPin = 6;
const byte fsrPin = A0;

int fsrthreshold = 100;  // adjust threshold for your setup

void setup()
{
   Serial.begin(115200);
}

void loop()
{
  int fsrValue = readfsr();
  if( fsrValue > fsrthreshold) // force over threshold, fade
  {
   fadeLed(fsrValue / 4); // 1023 adc to 255 pwm
  }
  else  // force under threshold, blink
  {
   blinkLed(500);
  }
}

int readfsr()
{
   return(analogRead(fsrPin));
}

void blinkLed(unsigned long blinkInterval)
{
   static unsigned long blinkTimer = 0;
   if(millis() - blinkTimer >= blinkInterval)
   {
      blinkTimer = millis();
      digitalWrite(ledPin, !digitalRead(ledPin));
   }
}

void fadeLed(byte fadeValue)
{
   analogWrite(ledPin, fadeValue);
}

Hello!

I'd just like to say thanks to groundFungus and the other people who commented on this question. I looked at the code groundFungus posted, played around with a sample Blink sketch, tried cobbling the code together and it works! :slight_smile:

I must admit that I need to review these sketches so I can better understand what each line of code does. Starting in September I will be enrolled in a more programming-heavy engineering class so I plan to capitalize on that and will ask lots of questions real-time.

I appreciate the patience from those who commented on this post. I don't really know what I'm doing and I find it hard to start from scratch.