Controls a red LED through Serial Monitor thanks to PWM. Enter a brightness percentage (number between 1 and 100) to light up the LED. PIN number changes according to the board. You must check the pinout diagram to be sure you’re using a PWM pin !
//Arduino Uno
#define LED_R 3 //PWM according to the pinout
int del = 1000; //delay
void setup() {
pinMode(LED_R, OUTPUT);
digitalWrite(LED_R, LOW);
Serial.begin(9600);
}
void loop() {
if (Serial.available()) {
float brightness = Serial.parseInt(); // enter brightness in % (between 1 and 100)
int state = int(brightness/100.0*255.0);
if ((state < 256) & (state > 0)) {
analogWrite(LED_R, state);
Serial.println("LED R turned at " + String(brightness) + " % brightness");
delay(del);
delay(del);
}
}
}
add instruction to evaluate the SRAM left
//Arduino Uno
#define LED_R 3 //PWM according to the pinout
int del = 1000; //delay
void setup() {
pinMode(LED_R, OUTPUT);
digitalWrite(LED_R, LOW);
Serial.begin(9600);
}
void loop() {
if (Serial.available()) {
float brightness = Serial.parseInt(); // enter brightness in % (between 1 and 100)
int state = int(brightness/100.0*255.0);
if ((state < 256) & (state > 0)) {
analogWrite(LED_R, state);
Serial.println("LED R turned at " + String(brightness) + " % brightness");
delay(del);
display_freeram(); // evaluate the freeram
delay(del);
}
}
}
void display_freeram() {
Serial.print(F("- SRAM left: "));
Serial.println(freeRam());
}
int freeRam() {
extern int __heap_start,*__brkval;
int v;
return (int)&v - (__brkval == 0
? (int)&__heap_start : (int) __brkval);
}
No serial communication nor float nor calculation, the brightness value is given by a random function (integer between 1 and 255)
//Arduino Uno
#define LED_R 3 //PWM according to the pinout
int del = 1000; //delay
void setup() {
pinMode(LED_R, OUTPUT);
digitalWrite(LED_R, LOW);
}
void loop() {
int state = random(1,255);
analogWrite(LED_R, state);
delay(del);
}
add instruction to evaluate the SRAM left
//Arduino Uno
#define LED_R 3 //PWM according to the pinout
int del = 1000; //delay
void setup() {
pinMode(LED_R, OUTPUT);
digitalWrite(LED_R, LOW);
Serial.begin(9600);
}
void loop() {
int state = random(1,255);
analogWrite(LED_R, state);
display_freeram();
delay(del);
}
void display_freeram() {
Serial.print(F("- SRAM left: "));
Serial.println(freeRam());
}
int freeRam() {
extern int __heap_start,*__brkval;
int v;
return (int)&v - (__brkval == 0
? (int)&__heap_start : (int) __brkval);
}