Skip to content

Instantly share code, notes, and snippets.

@vinayvinay
Created March 21, 2026 14:19
Show Gist options
  • Select an option

  • Save vinayvinay/d0485912c15ea5b4b123a606115bb0f0 to your computer and use it in GitHub Desktop.

Select an option

Save vinayvinay/d0485912c15ea5b4b123a606115bb0f0 to your computer and use it in GitHub Desktop.
STEM birthday code
// www.elegoo.com
// 2016.12.8
// Define Pins
#define BLUE 3
#define GREEN 5
#define RED 6
void setup()
{
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
digitalWrite(RED, HIGH);
digitalWrite(GREEN, LOW);
digitalWrite(BLUE, LOW);
}
// define variables
int redValue;
int greenValue;
int blueValue;
// main loop
void loop()
{
#define delayTime 10 // fading time between colors
redValue = 255; // choose a value between 1 and 255 to change the color.
greenValue = 0;
blueValue = 0;
// this is unnecessary as we've either turned on RED in SETUP
// or in the previous loop ... regardless, this turns RED off
// analogWrite(RED, 0);
// delay(1000);
for (int i = 0; i < 255; i += 1) // fades out red bring green full when i=255
{
redValue -= 1;
greenValue += 1;
// The following was reversed, counting in the wrong directions
// analogWrite(RED, 255 - redValue);
// analogWrite(GREEN, 255 - greenValue);
analogWrite(RED, redValue);
analogWrite(GREEN, greenValue);
delay(delayTime);
}
redValue = 0;
greenValue = 255;
blueValue = 0;
for (int i = 0; i < 255; i += 1) // fades out green bring blue full when i=255
{
greenValue -= 1;
blueValue += 1;
// The following was reversed, counting in the wrong directions
// analogWrite(GREEN, 255 - greenValue);
// analogWrite(BLUE, 255 - blueValue);
analogWrite(GREEN, greenValue);
analogWrite(BLUE, blueValue);
delay(delayTime);
}
redValue = 0;
greenValue = 0;
blueValue = 255;
for (int i = 0; i < 255; i += 1) // fades out blue bring red full when i=255
{
// The following code has been rearranged to match the other two similar sections
blueValue -= 1;
redValue += 1;
// The following was reversed, counting in the wrong directions
// analogWrite(BLUE, 255 - blueValue);
// analogWrite(RED, 255 - redValue);
analogWrite(BLUE, blueValue);
analogWrite(RED, redValue);
delay(delayTime);
}
}
//www.elegoo.com
//2023.05.06
int buzzer = 12;//the pin of the active buzzer
void setup()
{
pinMode(buzzer, OUTPUT); //initialize the buzzer pin as an output
}
void loop()
{
int sound_duration = 500;
for (int i = 0; i < 20; i++)
{
//use the if function to gradually shorten the interval of the sound
if (i < 5)
{
sound_duration = 500;
} else if (i < 10)
{
sound_duration = 300;
} else if (i < 20)
{
sound_duration = 100;
}
//activate the active buzzer
digitalWrite(buzzer, HIGH);
delay(sound_duration);//wait for sound_duration ms
//deactivate the active buzzer
digitalWrite(buzzer, LOW);
delay(sound_duration);//wait for sound_duration ms
}
//activate the active buzzer
digitalWrite(buzzer, HIGH);
delay(5000);//keep playing sound for 5 seconds.
}
//www.elegoo.com
//2016.12.08
int ledPin = 5;
int buttonApin = 9;
int buttonBpin = 8;
byte leds = 0;
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(buttonApin, INPUT_PULLUP);
pinMode(buttonBpin, INPUT_PULLUP);
}
void loop()
{
if (digitalRead(buttonApin) == LOW)
{
digitalWrite(ledPin, HIGH);
}
if (digitalRead(buttonBpin) == LOW)
{
digitalWrite(ledPin, LOW);
}
}
int tiltPin = 2;
int buzzerPin = 12;
void setup() {
pinMode(tiltPin, INPUT_PULLUP);
pinMode(buzzerPin, OUTPUT);
}
void loop() {
if (digitalRead(tiltPin) == LOW) {
digitalWrite(buzzerPin, HIGH);
} else {
digitalWrite(buzzerPin, LOW);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment