Setting Up and Programming Photo Resistor on Arduino:
I learned how to set up and read a photo resistor on the Arduino program. I added the photo resistor to my breadboard by putting a 10k resistor behind the short led of the photo resistor and a wire going from right behind the 10k resistor to the analog zero pin on my arduino, which shows that this pin was an analog pin vs. a digital pin which we have used in the past, which makes this pin an INPUT vs. an OUTPUT when coding. I also added another wire going from behind the other end of the 10k resistor into the negative side of the breadboard rail and then another wire going from right next to the wire that went to the analog pin and went to the positive side of the breadboard rail. Then in order to see code the photo resistor, I had to put under void setup pinMode (AO, INPUT);, which allows the photo resistor to be an input vs. an output which we have been working with in order to make LEDs blink, and therefore this code will the photo resistor not exactly to be seen what is going on, but later add code to read and understand it. Then under void loop, I put int LDR = analogRead(AO);, so it would read the photo resistor even though I will be unable to see anything change with this code at the moment. In order to be able to download this code, I had to make sure the correct serial port was connected before and after in order for it to work.
*we used analog inputs, which give a range of number like we used in the photo resistor, unlike a digital input, where there only one number has been defined. Since they are both inputs, they will not be visibly seen, but more a code working inside the arduino that we are able to see using the serial monitor.
Then, we worked on being able to read the photo resistor on out arduinos by using serial print commands to "look inside the arduino." I created the code starting with the void setup being pinMode (AO, INPUT), then Serial.begin(9600). This sets the data rate in bits per second for serial data transmission, and allows the photo resistor to be read or transmitted. Then under void loop, I put int LDR = analogRead (AO), then Serial.println(LDR), and that allows whatever the variable define as LDR, which in this case is the analog pin AO to be read using the serial monitor. In order to see what the photo resistor is doing, I had to press the serial monitor which looks like a magnifying glass, and if I didn't I would still not notice any changes in the photo resistor. Then I was able to see many numbers on the screen and when I put my hand over the photo resistor or made it darker around the photo resistor, the numbers decreased and when it became more light the numbers increased. The lowest number is zero and the highest is 1023, but mine only got to the mid 900s. The photo resistor is a pull down resistor.
Adding Button and Code For Button to Photo Resistor Setup:
I added a button to my photo resistor setup on my arduino. I added the button a few components down from the photo resistor and added a 10k resistor behind the first leg of the button and the positive side of the breadboard rail. Then I added another wire behind the second leg of the button and put the other side of the wire in the positive side of the breadboard rail. I also had another wire that went in front of the first front leg of the button and into the digital pin 8. I added to my code from the photo resistor with the serial print commands by adding the void setup of pinMode (8, INPUT), so I won't be able to physically see anything unless I use the serial monitor because I used the serial.begin (9600) command and also reused the pinMode (AO, INPUT) for the photo resistor. Then under void loop, I put int LDR = analogRead (AO) and then int button = digitalRead(8), so the input or the code would reflect on those to pins. Then I put Serial.println("LDR = "), and then Serial.println(LDR), and then I put Serial.println("button = ") and then put Serial.println(button), so the serial monitor would not only read the photo resistor but also the button. When, I had this code successfully work when I clicked on the serial monitor, the photo resistor changed the same, when it became darker the numbers decreased and when it got lighter the numbers increased, but now instead of just the numbers, it says LDR= and the number value. The serial monitor also added a line saying button = 1, and when I pressed the button, it changed to zero, therefore the values changed and were reflected as an input on the serial monitor vs. an output.
LED Effected by Photo Resistor:
I worked on having an LED turn on and off due to the amount of light on the photo resistor. I already had the correct setup to the digital pin 9 from when I was working with making 8 LEDs blink, and in order to start my code, I had to define my variable as int LED = 9, in order to move on further with my code. I added to my code from making the button change values along with the photo resistor due to darkness and added pinMode (9, OUTPUT), so I would actually be able to see the change in brightness in the LED vs. it being an internal change within the arduino. Then additional to the photo resistor and button code under void loop, I added if (LDR<600) then digitalWrite (LED, HIGH), and then else digitalWrite(LED, LOW), and this allowed additional to the code from the button and photo resistor to have the LED turn on when the number value of the photo resistor is less than 600 and if the number is greater than 600, then the LED would be off. This statement acts similar to the if then loop on scratch. When it worked, it allowed the LED to turn off or on depending on how much light there was on the photo resistor. I can also adjust the number the code to make it more easily turn on or off depending on how I adjust the number. It was successful visually and when I looked at the serial monitor, I was able to see the number the LED turned on at, as well.
MBlock to Loop LEDS
I worked on trying to make all 8 of my LEDs fade up to eventually reach maximum power. I started with the starting command of arduino program. Then I created a variable for pin and set it to 6, which is my lowest variable number. Then, I created another variable for power and set power to 0, so it can gradually increase. Then, I created a forever loop. Then I created a repeat until loop, and used the loop to say repeat until pin> 14, therefore, the pin number will increase until it is greater than 14, therefore it will increase up until 13, which is my greatest pin. Then I created another repeat until loop, and it said repeat until power = 255, therefore, the LEDs can gradually increase to 255, the maximum brightness of the LEDs. Then I said set pwm pin to the variable pin and output as the variable power, so the whatever pin is fading up at the moment, it will fade up at the same rate as the variable power, which is by 1 until 255. Then I used a wait command of .1 seconds, so it would increase vastly, but I could still see the change in brightness. Then I put the command change power by 1, so the power will gradual increase to maximum brightness. Then I put the command of change pin by 1, so after one pin increases to maximum brightness, it moves onto the next pin until after pin 13.
I also worked on trying to make all 8 of my LEDs fade up to eventually reach maximum power. I started with the starting command of arduino program. Then I created a variable for pin and set it to 6, which is my lowest variable number. Then, I created another variable for power and set power to 0, so it can gradually increase. Then, I created a forever loop. Then I created a repeat until loop, and used the loop to say repeat until pin> 14, therefore, the pin number will increase until it is greater than 14, therefore it will increase up until 13, which is my greatest pin. Then I created another repeat until loop, and it said repeat until power = 255, therefore, the LEDs can gradually increase to 255, the maximum brightness of the LEDs. Then I said set pwm pin to the variable pin and output as the variable power, so the whatever pin is fading up at the moment, it will fade up at the same rate as the variable power, which is by 1 until 255. Then I used a wait command of .1 seconds, so it would increase vastly, but I could still see the change in brightness. Then I put the command change power by 1, so the power will gradual increase to maximum brightness. Then I put the command of change pin by 1, so after one pin increases to maximum brightness, it moves onto the next pin until after pin 13.
We used the Mblock in order to loop LEDs, but Mblock proved to be too unstable for practical use.
(ino code from arduino is in google drive folder section of portfolio)