/* This code was written for the Stitch the Loop e-textiles curriculum by the Exploring Computer Science e-textiles team. ECS 2018 GPL V3 for non commercial use. ECS 2018 CC- BY NC SA. */ /* ◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇ This program is an EXAMPLE of the many possible solutions. This code will compile as is. ◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇ */ /* STUDENT META HERE STUDENT NAME(S) Date Written Brief description of what program does here. */ // The first section is where to declare global objects and call up additional files the program needs to use. /*NAMING SECTION: We name things to keep track of them easily. What will you name your other lights? */ //1,2,9,12 int led1 = 1; int led2old = 2; int led2 = 3; int led3 = 9; int led4 = 6; int led4old = 12; int blinkCounter = 1; int heartCounter = 1; int fadeBrightness = 5; bool fadeUp = true; /*SETUP SECTION: Put things here that you only need to do once: For instance, OUTPUTS (lights) */ void setup() { // The second section is for things that only need to be done once at the beginning of the program. pinMode(led1, OUTPUT); pinMode(led2, OUTPUT); pinMode(led2old, OUTPUT); pinMode(led3, OUTPUT); pinMode(led4, OUTPUT); pinMode(led4old, OUTPUT); } void loop() { /* The third section is for things that happen repeatedly in the program loop while the program is running. The code is executed in the order coded. */ blinkPattern(led1); twinkle(led2); twinkle(led2old); heartBeat(led3); fade(led4); fade(led4old); delay(100); } // The fourth section is for functions that are called up by the third section. void randomPattern(int led) { //random brightness 50-250 int chance = random(1000); int brightness = random(200) + 50; if (chance <500) analogWrite(led, brightness); else off(led); } void twinkle(int led) { int brightness = random(250); analogWrite(led, brightness); } void blinkPattern(int led) { if (blinkCounter == 1) on(led); else if (blinkCounter == 6) off(led); else if(blinkCounter > 10) blinkCounter = 0; blinkCounter ++; } void heartBeat(int led) { if (heartCounter == 1) on(led); else if (heartCounter == 3) off(led); else if (heartCounter == 5) on(led); else if (heartCounter == 7) off(led); else if(heartCounter > 15) heartCounter = 0; heartCounter ++; } void fade(int led) { if (fadeUp) { if(fadeBrightness >= 200) fadeUp = false; //fadeBrightness = 5; else fadeBrightness += 10; } else { if(fadeBrightness <= 5) fadeUp = true; //fadeBrightness = 5; else fadeBrightness -= 10; } analogWrite(led, fadeBrightness); } void on(int led) { digitalWrite(led, HIGH); } void off(int led) { digitalWrite(led, LOW); }