Skip to content

Using A Pressure sensor to control the brightness of LEDs; making LEDs blink (Both with Arduino) #1

@hellanoob

Description

@hellanoob

EXAMPLE CODE USED FOR PRESSURE MAPPING FOR LED BRIGHTNESS: https://github.com/DigitalFuturesOCADU/CC2024/tree/31af1a8070f3d7e2629c496767633e48b1e6b189/experiment1/analogMapping_1sensorvalue

EXAMPLE CODE USED FOR LED LIGHTS BLINKING: https://github.com/DigitalFuturesOCADU/CC2025/blob/d2d4f1ba8b68a398844e9cd4729d82e5c48f15fc/experiment1/leds/singleLEDBlink/singleLEDBlink.ino

// initialize variables
int sensorValue = 0;
int sensorPin = A0;

int LEDpin = 2; //CPR
int LEDpin3 = 12; //CPR

int LEDpin2 = 3; //heartbeat
int LEDpin4 = 11; //heartbeat

int mappedSensorValue = 0;

int led_BlinkTime = 500; //time for one complete blinik cycle (mms)
bool led_BlinkBeginState = false; // Initial state for blinking led (true=on, false=off)

int inputMin = 0;
int inputMax = 1023;

int outputMin = 0;
int outputMax = 255;

void setup() {
// initialize serial communication:
Serial.begin(9600);
// intialize LED pin as output
pinMode(LEDpin2, OUTPUT);
pinMode(LEDpin4, OUTPUT);
}

void loop() {
// read sensor pin and store value in a variable:
sensorValue = analogRead(sensorPin);

// map sensor value 0-255
mappedSensorValue = map(sensorValue, inputMin, inputMax, outputMin, outputMax);

//control the blinking LED
bool ledState = blink(led_BlinkTime, led_BlinkBeginState);
digitalWrite(LEDpin2, ledState);
digitalWrite(LEDpin4, ledState);

// pressure LEDs
analogWrite(LEDpin, mappedSensorValue);
analogWrite(LEDpin3, mappedSensorValue);

// debug, print the sensor and mapped sensor values:
Serial.print("Sensor Value: ");
Serial.print(sensorValue);
Serial.print(", Mapped Sensor Value: ");
Serial.println(mappedSensorValue);

// delay between readings:
delay(5);
}

// Function to determine LED state for blinking
// Uses timing logic to control the behaviour
// blinkTime = how fast it is blinking (milliseconds)
// beginState = determines whether its starting state is ON or OFF
bool blink(int blinkTime, bool beginState) {
// Define the boolean variable we will return
bool ledShouldBeOn = false;

// Calculate current position in blink cycle
// Using modulo (%) to get remainder when dividing current time by blink time
// millis() keeps counting up continuously (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11...)
// millis() % blinkTime creates a repeating pattern (0, 1, 2, 3, 4...497, 498, 499, 0, 1, 2, 3...)
// This gives us a way to know where we are within each blink cycle
unsigned long currentTime = millis() % blinkTime;

// Determine LED state based on timing and beginState
if(currentTime < (blinkTime / 2)) {
// We're in the first half of the cycle
ledShouldBeOn = beginState; // LED matches beginState in first half
} else {
// We're in the second half of the cycle
ledShouldBeOn = !beginState; // LED is opposite of beginState in second half
}

return ledShouldBeOn;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions