top of page

Blog Entry 3: Arduino Programming

Input devices: Interface a potentiometer analog input to maker UNO
board and measure/show its signal in serial monitor Arduino IDE.

Below is the code I used for the potentiometer.

const int analogInPin = A0;  
const int analogOutPin = 9;

 

 

int sensorValue = 0;        
int outputValue = 0;        

 

void setup() {
  Serial.begin(9600);
}

 

void loop() {
  sensorValue = analogRead(analogInPin);

 

 

  outputValue = map(sensorValue, 0, 1023, 0, 255);
 

 

  analogWrite(analogOutPin, outputValue);

 

 

  Serial.print("sensor = ");
  Serial.print(sensorValue);
  Serial.print("\t output = ");
  Serial.println(outputValue);

 

delay(2);
}

These two lines of code establishes that the analog input would be at pin A0 (where the potentiometer is connected) while analog output is pin 9 (where the LED would be attached).

These two lines are so that the sensor and output values are read.

This is to establish a serial connection between the arduino and computer. at 9600 bps.

This line is to store the raw analog values read from the potentiometer

This is to convert the data read from the analogRead to a smaller range of analogWrite

This is line is to map the new sensor data of 0 to 255 to the analog output pin

These four lines are to basically display the data read from the potentiometer to the serial monitor

This is just to add a delay to between the data reading and the displaying of data. Delay between the next loop of analog-to-digital

Clive and I did not really experience any problems with this part. However, we did have a bit of trouble setting up the wiring between the arduino and the breadboard. 

​

We were able to figure it out in the end after re-reading the material on brightspace and the given resources. It took us a while to understand why it was wired that way due to having to wire the analog pin. The wiring looked like this,

potentiometer wiring.jpeg

This is the video showing that the code works.

Input devices: Interface a LDR to maker UNO board and
measure/show its signal in serial monitor Arduino IDE:

As for the LDR, I used the same exact code. As this code is for any general sensor that converts analog signals to digital.

const int analogInPin = A0;  
const int analogOutPin = 9;

 

 

int sensorValue = 0;        
int outputValue = 0;        

 

void setup() {
  Serial.begin(9600);
}

 

void loop() {
  sensorValue = analogRead(analogInPin);

 

 

  outputValue = map(sensorValue, 0, 1023, 0, 255);
 

 

  analogWrite(analogOutPin, outputValue);

 

 

  Serial.print("sensor = ");
  Serial.print(sensorValue);
  Serial.print("\t output = ");
  Serial.println(outputValue);

 

delay(2);
}

These two lines of code establishes that the analog input would be at pin A0 (where the LDR is connected) while analog output is pin 9 (where the LED would be attached).

These two lines are so that the sensor and output values are read.

This is to establish a serial connection between the arduino and computer. at 9600 bps.

This line is to store the raw analog values read from the LDR

This is to convert the data read from the analogRead to a smaller range of analogWrite

This is line is to map the new sensor data of 0 to 255 to the analog output pin

These four lines are to basically display the data read from the LDR to the serial monitor

This is just to add a delay to between the data reading and the displaying of data. Delay between the next loop of analog-to-digital

Again, since the code is the same as the potentiometer, we did not experience much problems. Although, after wiring the LDR we kept on testing and looking at the serial monitor and realised that the output is not changing even when we shine a light. We then found out that we did not wire the analog wire properly to the respective part of the breadboard. We just put it in the proper 'hole' and it began to work fine.

​

Below is how the wiring for the LDR looks like.

LDR wiring_edited.jpg

This is the video for proof that the code works

Output devices: Interface 3 LEDs (Red, Yellow, Green) to maker
UNO board and program it to perform something (fade or flash etc)

Below is the code to allow the fade in and out of an LED

int brightness = 0;

 

 

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

 

​

void loop() {
  for (brightness = 0; brightness <=     255; brightness += 1) {
   
analogWrite(9, brightness);

     delay(5); 
  }
 

​

   for (brightness = 255; brightness >=     0; brightness -= 1) {
    analogWrite(9, brightness);
    delay(5);
  }
}

This line is to create an integer variable called "brightness" that starts at a value of 0

This makes pin 9 the output pin

This whole void loop basically says that when the value of the brightness of the LED is 0 it will increase by increments of 1 as long as the brightness value is lesser or equal to 255. The delay gives the effect of a fade.

This is similar to the above explanation in that once the brightness is 255, the brightness will decrease by increments of 1 for brightness values greater than or equal to 0. The delay, again, is to add the effect of the fade.

I got the code from the video:

https://www.youtube.com/watch?v=X8dHbdhnGKY

We did not experience much problems as the video we watched was very helpful. For this instance, we did not have any trouble setting up the wiring but we did have trouble trying to understand the code as the code worked but we did not know why.

​

Below is a video on the code working.

Output devices: Include pushbutton to start/stop the previous task

Below is the code used to program the button to activate the fade in and out of the LEDs.

int brightness = 0;

​

​

void setup() {
Serial.begin(9600);

 

 

pinMode(2, INPUT_PULLUP);
 

 

pinMode(13, OUTPUT);

}

 

 

void loop() {
int sensorVal = digitalRead(2);

 

 

Serial.println(sensorVal);

 

 

if (sensorVal == HIGH) {}

 

 

else { 
 

 

   for (int i=0; i < 5; i++)
 

 

  {for (brightness = 0; brightness <= 255; brightness += 1) {
    analogWrite(9, brightness);
    delay(10); 
  }
 

  for (brightness = 255; brightness >= 0; brightness -= 1) {
    analogWrite(9, brightness);
    delay(10); 
  }
}
}
}

The same as the previous code, this line creates a variable "brightness" that starts at a value of 0

Like previously, this starts a serial connection

This line sets up pin 2 at the input and the "PULLUP" inverts the logic, so it will be HIGH signal when released and LOW when pressed

This line sets up pin 13 as the output

This basically makes it so the the value of pin 2 will be a variable "sensorVal"

This line displays the value of pin 2 (HIGH or LOW) in the serial monitor page

This line will make it so that if the button has not been pressed, there will be nothing happening

This "else" line is the opposite of "if", so this program will run when the button is pushed.

This line makes it so that the fade program, runs 5 times after the button is pushed without having to hold it down.

The explanation for the fade in and out code for this last part is the same as the explanation for the previous section

The code for this is a combination of the code taken from the previously explained code of the fade in and out without the button and from

https://www.arduino.cc/en/Tutorial/BuiltInExamples/InputPullupSerial

I think the biggest problem Clive and I faced was trying to program the button to activate the fade in and out. We tried to use the tinkerCAD website that does the block coding thing to create the code. However, when we tried to put the blocks together to program the button, we did not succeed.

​

We eventually scrapped the block coding and just tried to copy and paste the lines of the fade in and out code into the digitalInputPULLUP example code. We noticed that the code would be similar to programming the pin LED blink except now we had to add the fade effect. To our delight, it worked and the LED lights were fading in and out when the button was pushed.

​

Below is a video of our code working.

Below is my Learning Reflection on the overall Arduino Programming activities.

If I am being very honest, I heard that in year 2 we would be coding and I did not look forward to it at all. I really hated coding although I have never tried it before now. However, after having gone through this series of tutorials and a practical based on Arduino programming, dare I say that may enjoy programming now.

​

I would say what changed my mind on coding was when I took the time to try and understand what each line of code meant. During the first Arduino tutorial, I had no clue what was going on. My mind was all over the place. I was speed reading through the learning packages and just mindlessly copy and pasting the codes into the Arduino IDE hoping for the codes to work so I can move on to the next question. Eventually, I had to do the pre-practical report for the Arduino practical and that was when I went through the learning package to understand the different lines. After finding out what everything meant, there was a sort of ease to my mind. The many lines of code did not look so intimidating anymore. It also helped me better understand the codes during the practical with the trial and error Clive and I went through as we had to troubleshoot the code by constantly verifying and correcting the mistakes. Ultimately, I did not like coding before this because I did not know what anything meant but having a better understanding on what the different lines mean makes it a slightly better experience.

​

Another reason I would say is I enjoy coding is the satisfaction of seeing your code work in the end. Comparing mindlessly copy and pasting a code to having to put together different codes for a specific program you desire has two very different outcomes in terms of feelings. I did not feel very accomplished after I just copy and pasted the Hello World code into the Arduino IDE and the light started blinking but I did feel very accomplished during the Arduino practical when Clive and I was able to program the arduino to play a song, blink an LED light, and rotate a servo all in the same code. Although it seems like a simple task, it was quite daunting to us and we are proud of what we have achieved. Yes, we did not get it right on the first try but I think that the 'hardships' and effort having to be put into trying to solve these kind of problems is what makes seeing your code work at the end of the day.

​

Overall, I think this the first blog in both ICPD and CPDD where I would actually want to do this again. It is nice getting to learn new and different kinds of codes as well as be able to program different things to function. I started out this by not wanting to do anything related but I have progressed to now where I say, 10/10 would want to do this again.

Let me know what's on your mind

Thanks for submitting!

Reinard is Great

bottom of page