Fading LEDs:

/* This code was written for the M.A.D.E. e-textiles
curriculum by the Penn e-textiles team. M.A.D.E. 2019
GPL V3 for non commercial use. M.A.D.E. 2019 CC- BY NC SA.


◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇
This program is an EXAMPLE of Blinking an LED.
This code will compile as is.
◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇

ADD STUDENT META HERE
STUDENT NAME(S):
Date Written:
Description: Add a brief description of what program does
*/

int led = 13;
//use Circuit Playground pins 13, 3, 6, 9, or 10

void setup() {
pinMode(led, OUTPUT);
}

void loop() {
analogWrite(led,255);
delay(1000);
analogWrite(led,0);
delay(1000);
}
/* This code was written for the M.A.D.E. e-textiles
curriculum by the Penn e-textiles team. M.A.D.E. 2019
GPL V3 for non commercial use. M.A.D.E. 2019 CC- BY NC SA.

◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇
This program is an DEBUG EXERCISE EXAMPLE of how to fade an LED on and off. The speed
is set too fast, and the debug needed is to set it at a slower pace and to also add
the code necessary to create the second half of the fading sequence, fading the LED off.
This code will compile as is.
◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇

ADD STUDENT META HERE
STUDENT NAME(S):
Date Written:
Description: Add a brief description of what program does
*/

int led = 13;
//You can use any analog pin: 3 6 9 10
//or the red one already attached, 13

void setup() {
pinMode(led, OUTPUT);
}

void loop() {
//This for()loop SHOULD be fading in and out like breathing,
//NOT hyperventilating!

for (int brightness = 0; brightness < 255; brightness = brightness + 15) {
//brightness is 0, as long as brightness is less than 255, increase by 15
//brightness is a descriptive variable to remind us what this code does.
//You can change it to i if it helps you to read the code better

analogWrite(led, brightness);
delay(10);
}
analogWrite(led, 0);
delay(2000);
//once all the way bright, turn off the LED for 2 seconds

/*Once you have the inhale at slower pace, code a steady
exhale for() loop with your choice of descriptive variable
name for fading the light out.*/
}
/* This code was written for the M.A.D.E. e-textiles
curriculum by the Penn e-textiles team. M.A.D.E. 2019
GPL V3 for non commercial use. M.A.D.E. 2019 CC- BY NC SA.
◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇
This program is an DEBUG EXERCISE EXAMPLE COMPLETED of how to fade an LED on and off. The speed
has been adjusted to be slower, and the the second half of the fading sequence, fading the LED
off, has been added.
This code will compile as is.
◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇

ADD STUDENT META HERE
STUDENT NAME(S):
Date Written:
Description: Add a brief description of what program does
*/

int led = 13;
//You can use any analog pin: 3 6 9 10
//or the red one already attached, 13

void setup() {
pinMode(led, OUTPUT);
}

void loop() {
//This for()loop slowly breathes the LED on

for (int brightness = 0; brightness < 255; brightness = brightness + 1) { //brightness is 0, as long as brightness is less than 255, increase by 15 //brightness is a descriptive variable to remind us what this code does. //You can change it to i if it helps you to read the code better analogWrite(led, brightness); delay(20); } //this for() loop slowly breathes the LED off for (int brightness = 255 ; brightness >= 0; brightness--) {
analogWrite(led, brightness);
delay(20);
}
delay(1000);
} 
/*  This code was written for the M.A.D.E. e-textiles
     curriculum by the Penn e-textiles team. M.A.D.E. 2019
     GPL V3 for non commercial use. M.A.D.E. 2019 CC- BY NC SA.

  ◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇
  This program is an FADING CHALLENGE SOLUTION for how to fade one LED off and one
  LED on within the same for() loop. 
  This code will compile as is.
  ◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇

  ADD STUDENT META HERE
  STUDENT NAME(S):
  Date Written:
  Description: Add a brief description of what program does
*/


int firstLed = 6;
int secondLed = 9;
//You can use any analog pin: 3 6 9 10

void setup() {
}

void loop() {
  for (int i = 0; i < 255; i++) { analogWrite(firstLed, i); analogWrite(secondLed, 255 - i); delay(10); } for (int i = 255; i > 0; i--) {
    analogWrite(firstLed, i);
    analogWrite(secondLed, 255 - i);  //notice this stays the same
    delay(10);
  }
}

//You might think this looks wrong. The problem is our eyes, not the code
//Our eyes can pick up variations in low light, much easier than bright light,
//so it may look like the bright is on too long, but in reality, it is on the same as the dark

Mapping:

/* This code was written for the M.A.D.E. e-textiles
curriculum by the Penn e-textiles team. M.A.D.E. 2019
GPL V3 for non commercial use. M.A.D.E. 2019 CC- BY NC SA.

◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇
This program is an EXAMPLE of how to write if/else() function to create multiple
light pattern options depending on the interaction with the sensor. First we use
Serial communications to find our current highest and current lowest values. Second,
we map the narrow range of current high/low numbers to stretch it out wider to make
it easier to read minute variations with the sensor. Third is a set of if/else()
statements with different light patterns for different sensor readings.
This code will compile as is.
◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇

ADD STUDENT META HERE
STUDENT NAME(S):
Date Written:
Description: Add a brief description of what program does
*/

int lightSensorPin = A5; //sensors: A5 light, A4 sound, A0 temperature
int lightSensorValue;
int mappedLightSensorValue;
int mappedOutputLED = 13; //try connecting a different LED with alligator clips: D0, D1, D2, D3, D6, D9, D10, D12. Digital pins should be programmed without the "D".

int blinkTime = 1000; //milliseconds, using a variable to make it easier to change the delay timing in the future
int shortPause = 2; //delay variable, change to increase the delay and slow down the output numbers

void setup() {
//we want what is coming IN to the sensor that we are reading.
//PULLUP means we use an internal resistor.
pinMode(lightSensorPin, INPUT_PULLUP);
Serial.begin(9600); //comment out when done with Serial

}

void loop() {
lightSensorValue = analogRead(lightSensorPin);
mappedLightSensorValue = map(lightSensorValue, 11, 330, 0, 1024);
//I have a narrow reading range, I want to see more differences with a wide one;
Serial.println(lightSensorValue ); //comment out when done with Serial

//add in the conditions that you want to be met to start a blinking pattern
if (mappedLightSensorValue >= 0 && mappedLightSensorValue <= 100) { //just LED on analogWrite(mappedOutputLED, 255); } else if (mappedLightSensorValue >= 101 && mappedLightSensorValue <= 400) { //fast blink pattern analogWrite(mappedOutputLED, 255); delay(blinkTime / 4); analogWrite(mappedOutputLED, 0); delay(blinkTime / 4); } else if (mappedLightSensorValue >= 401 && mappedLightSensorValue <= 700) {
//medium blink pattern
analogWrite(mappedOutputLED, 255);
delay(blinkTime / 2);
analogWrite(mappedOutputLED, 0);
delay(blinkTime / 2);
} else {
//slow blink pattern
analogWrite(mappedOutputLED, 255);
delay(blinkTime);
analogWrite(mappedOutputLED, 0);
delay(blinkTime);
}

delay(shortPause); //comment out when done with Serial
}
/* This code was written for the M.A.D.E. e-textiles
curriculum by the Penn e-textiles team. M.A.D.E. 2019
GPL V3 for non commercial use. M.A.D.E. 2019 CC- BY NC SA.

◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇
This program is an EXAMPLE of how to map a range of numbers gathered
with serial communications to an analog sensor on the Circuit Playground
Classic. It takes the 0-1023 range of the sensor and smooshes it down to
a range that can be used with the red LED on pin #13 (0-255).
This code will compile as is.

◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇
ADD STUDENT META HERE
STUDENT NAME(S):
Date Written:
Description: Add a brief description of what program does
*/

int lightSensor = A5; //sensors: A5 light, A4 sound, A0 temperature
int mappedLED = 13; //try connecting a different LED with alligator clips: 0, 1,2, D3, 6, 9, 10, 12.
int lightValue;
int mappedValue = 0;

void setup() {
//we want what is coming IN to the sensor that we are reading.
//PULLUP means we use an internal resistor.
pinMode(lightSensor, INPUT_PULLUP);
Serial.begin(9600); // initialize serial communications at 9600 bps
pinMode(mappedLED, OUTPUT);
}

void loop() {
/*We want to read the sensor pin and then take that reading and
map it to a smaller or larger range of numbers. If we are
mapping a large set to a smaller range of numbers, we need
to squoosh it down. To map a small set to a larger range of
numbers, we need to stretch it out. To use the "map" function
fill in your numbers in this order:
map(variable, fromLow, fromHigh, toLow, toHigh);
Example: variableToHoldMappedNumbers = map(variableYouJustRequestedWithAnalogRead, #lowestItIs,#highestItIs, #lowestYouWantItToBe, #highestYouWantItToBe);
For further mapping knowledge: https://www.arduino.cc/reference/en/language/functions/math/map/
*/

lightValue = analogRead(lightSensor);
mappedValue = map(lightValue, 0, 1023, 0, 255); //you can also reverse the last two numbers for a different effect

analogWrite(mappedLED, mappedValue);

// write out the results to the Serial Monitor:
Serial.print("sensor reading = ");
Serial.print(lightValue);
Serial.print("\t mapped output = ");
Serial.println(mappedValue);

// wait a bit (in milliseconds)before repeating everything in the void loop section
delay(2);
}
/* This code was written for the M.A.D.E. e-textiles
curriculum by the Penn e-textiles team. M.A.D.E. 2019
GPL V3 for non commercial use. M.A.D.E. 2019 CC- BY NC SA.

◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇
This program is an EXAMPLE of how to map a range of numbers gathered
with serial communications to an analog sensor on the Circuit Playground
Classic. It takes the 0-1023 range of the sensor and smooshes it down to
0-255. This is prep for linking the sensor reading INPUT to an LED OUTPUT.
This code will compile as is.
◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇
ADD STUDENT META HERE
STUDENT NAME(S):
Date Written:
Description: Add a brief description of what program does
*/

int lightSensor = A5; //sensors: A5 light, A4 sound, A0 temperature
int lightValue;
int mappedValue;

void setup() {
//we want what is coming IN to the sensor that we are reading.
//PULLUP means we use an internal resistor.
pinMode(lightSensor, INPUT_PULLUP);
Serial.begin(9600); // initialize serial communications at 9600 bps
}

void loop() {
/*We want to read the sensor pin and then take that reading and
map it to a smaller or larger range of numbers. If we are
mapping a large set to a smaller range of numbers, we need
to squoosh it down. To map a small set to a larger range of
numbers, we need to stretch it out. To use the "map" function
fill in your numbers in this order:
map(variable, fromLow, fromHigh, toLow, toHigh);
Example: variableToHoldMappedNumbers = map(variableYouJustRequestedWithAnalogRead, #lowestItIs,#highestItIs, #lowestYouWantItToBe, #highestYouWantItToBe);
For further mapping knowledge: https://www.arduino.cc/reference/en/language/functions/math/map/
*/

lightValue = analogRead(lightSensor);
mappedValue = map(lightValue, 0, 1023, 0, 255); //you can also reverse the last two numbers for a different effect

Serial.print("sensor reading = ");
Serial.print(lightValue);
Serial.print("\t mapped output = ");
Serial.println(mappedValue);

// wait a bit (in milliseconds)before repeating everything in the void loop section
delay(2);
}
/*  This code was written for the M.A.D.E. e-textiles
     curriculum by the Penn e-textiles team. M.A.D.E. 2019
     GPL V3 for non commercial use. M.A.D.E. 2019 CC-BY NC SA.

  ◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇
  This program is an EXAMPLE of how to setup reading an analog sensor through Serial communications. 
  It uses Serial at 9600 baud, and the analogRead function on the light sensor pin (A5). 
  Open the Serial monitor to see what the sensor is reading. This code will compile as is.
  ◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇

  ADD STUDENT META HERE
  STUDENT NAME(S):
  Date Written:
  Description: Add a brief description of what program does
*/

int lightSensorPin = A5;  //sensors: A5 light, A4 sound, A0 temperature
int lightSensorValue;
int shortPause = 2;       //delay variable, change to increase the delay and slow down the output numbers

void setup() {
  //we want what is coming IN to the sensor that we are reading. 
  //PULLUP means we use an internal resistor.
  pinMode(lightSensorPin, INPUT_PULLUP);
  // start serial communications at 9600 bps aka baud rate
  Serial.begin(9600);
}

void loop() {
  lightSensorValue = analogRead(lightSensorPin);

  // write out the results to the Serial Monitor:
  Serial.println(lightSensorValue);

  // wait a bit (in milliseconds)before repeating everything in the void loop section
  delay(shortPause);
}

Music:

/*This is music SAMPLE code.  You will need to 
PUT YOUR OWN NOTES IN!!!*/
int speaker = 5;
int pace = 2200; //length of whole note; change this for speed
float durations[] = {     //note that we use FLOAT here 
  3.0/8.0,     3.0/8.0,     1.0/4.0,     1.0/8.0,     3.0/8.0,         
  1.0/4.0,     1.0/8.0,     1.0/4.0,     1.0/8.0,     3.0/4.0 
};
int pitches[] = {
  294,    294,   294,   330,  370,
  370,    330,   370,   392,  440
};



void setup() {
  /* put your setup code here, like the INPUTS and OUTPUTS (including the speaker!) you need*/

}

void loop() {
  for (int i = 0; i < 10; i = i+1 ) {   //Change the “i<10 ” value for the number of notes in the song
        tone(speaker, pitches[i]); //play note 
        delay(pace * durations[i]); //for a certain length of time
        noTone(speaker); //stop playing a note
        delay(10); //for a short amount of time
     }

}