Jump to content

Blog post: Lightmeow: The Basics Of Programming with Arduino

Featured Replies



DOWNLOAD LINK FOR THE PROGRAM: http://arduino.cc/en/Main/Software   


Visit my site, were you can find the original, and much more.

https://sites.google.com/site/liqhtm3owsthoughts

So I go to the official website, and they have a "Learn the Basics" page, at http://arduino.cc/en/Tutorial/HomePage.  I go there, and am overwhelmed by the amount of information presented to me.  Luckily, on the left side, they have a basics section.

So now to the programming.  I start up the program, and I see this:
Splash%2BScreen.PNG

Then the program loads, and I get this:
New+Sketch.PNG

Pretty simple...

THE BARE MINIMUM

So now I need to go on their site, and look at some of the examples: http://arduino.cc/en/Tutorial/HomePage

In their basics section, the first link is called "Bare Minimum"  I click into that, and am presented with this code:


void setup() {
  // put your setup code here, to run once:}

void loop() {
  // put your main code here, to run repeatedly:
 
}




I understand this.  The void setup() means that this code is run once, and the void loop() is run over and over.

I click the check mark button, and, it says that I am good to go.  So now, note to self, memorize the void setup() and the void loop().

BLINK

Now, I need to do the next part of the basics, which is turning an LED on and off, making it blink. Go here: http://arduino.cc/en/Tutorial/Blink

So for this, I need both the board, and an LED.  Because I don't have anything, I am going to have to use my imagination, until I get a board that is.  So, I am looking at the diagram of the board and the LED that is on the site, and I see this:
Ardunio Diagram

This Picture is from the Arduino.cc site, under the Creative Commons Attribution ShareAlike 3.0. License

So I see that a 220ohm resistor is hocked up to a LED(read this to learn how to decode a resistor), that is in turn hocked up to the GND(I don't know what that is). A LED has two legs. The longer leg, called the anode, is the positive end.  This is the end that will be receiving the electricity.  The anode is attached to a 220ohm transistor, so we regulate the voltage, and don't burn out the LED.  The shorter end of the LED, called the cathode, is the negative end, and is going back to the battery.  ALWAYS PLUG THE LED IN THE RIGHT WAY, AND ALWAYS HAVE THE LED's ANODE CONNECTED INTO THE RESISTOR!!!

I need the code to explain it:
int LED = 13;void setup() {               
  pinMode(LED, OUTPUT);    
}

void loop() {
  digitalWrite(LED, HIGH);   // turn the LED on
  delay(1000);               // wait for a second
  digitalWrite(LED, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}


Lets see, so we need to name the LED 13.  So when we talk about the LED, the LED is 13.  Then in the void setup(), we tell the board that the pinMode(); is, the LED is the input(which is 13), and it is what the voltage is going to  Now, in the void loop(), we need to tell the board to send some electricity to the LED, which is pin 13, have the voltage on high, and turn the LED on, then the delay(); has it wait a second (the board, I assume updates itself every 1/1000 of a second), and then send not a lot of electricity to the board, causing it to turn off, wait a second, and repeat the whole thing all over again until we unplug it from the computer because it has no more electricity.

DIGITAL READ SERIAL

Now, I am going to do the next tutorial, the Digital Read Serial.  In this, the board is going to be sending binary digits to your computer, 1 for on, and 0 for off.
Here is the page on the site: http://arduino.cc/en/Tutorial/DigitalReadSerial
Here is the diagram for how you need to set up the board:
button.png

This Picture is from the Arduino.cc site, under the Creative Commons Attribution ShareAlike 3.0. License

I see that the board is hocked up to another board.  This is called the bread box.  Set up the board like so with the wires, est.  Now, here is the code
int pushButton = 2;void setup() {
  Serial.begin(9600);
  pinMode(pushButton, INPUT);
}

void loop() {
  int buttonState = digitalRead(pushButton);
  Serial.println(buttonState);
  delay(1);
}


So lets see, Pin two, as seen on the diagram, is connected to a push button.  We need to name the push button, and we name it pushButton.  In the void setup(), we tell the board to use the serial connection, and make it so it is sending 9600 bits per second to our computer.  After that, we need to tell the board that the push button is an output, instead of an input, like it was in the last example.
Next, in the void loop(), we are telling the board to read if the button is pressed or not.  We are also creating another variable, buttonState.  Then in Serial.printIn , we are telling the board to print the variable, buttonState, to our computer.  If the button is pressed, then buttonState will be zero, if it is not pressed, then buttonState will be one.  Then it prints it to the Serial Monitor.  The delay(); , in this instance, makes it so we get a steady stream of digits.  See what happens if you take out the delay, or comment it out.

Example of comments:
//Hello, Computer will skip over me and doesn't care what I put here/*
Same here
Computer will skip
over me
*/


Fade

So, the next part of the Basic's Section is entitled Fade.  You can find the link to the tutorial on the site here: http://arduino.cc/en/Tutorial/Fade.


So, this is the diagram:
Simple Fade Diagram

This Picture is from the Arduino.cc site, under the Creative Commons Attribution ShareAlike 3.0. License

So, for what we see in the diagram, is the Arduino board hocked up to the bread board.  A LED and a 220ohm resistor, nothing complicated.

And here is the code:


int led = 9;
int brightness = 0;
int fadeAmount = 5;void setup()  {
  pinMode(led, OUTPUT);
}

void loop()  {
  analogWrite(led, brightness);    

  brightness = brightness + fadeAmount;

  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ;
  }       
  delay(30);                            
}




So lets see, we tell the program that pin 9 is the LED, that we need a variable brightness and to set brightness to zero.  Then we need to have an amount to fade by, or a fading speed, so the variable fadeAmount is created and that is going to be 5.

Continuing along, in the void setup()we are telling the Arduino to set the LED, which is pin 9, as an output.  Then we go to the void loop().

I can't explain what the analogWrite does, so this is what the Arduino.cc tells us:"analogWrite() can change the PWM value very fast, so the delay at the end of the sketch controls the speed of the fade. Try changing the value of the delay and see how it changes the program."


After that, we are adding the brightness to the fadeAmount.  The if statement tells us, if the brightness is 0(all off) OR the brightness is 255(all on), then invert the value of the fadeAmount.  So when the Brightness hits 255, fadeAmount will be -5.  Then we are telling the program to stop for 30/1000's of a second, then the cycle repeats.

Read Analog Voltage

The last part of the basics section, is the Read Analog Voltage Tutorial.  You can find the link of this tutorial here:  http://arduino.cc/en/Tutorial/ReadAnalogVoltage

This is the diagram:
AnalogReadSerial_BB.png
This Picture is from the Arduino.cc site, under the Creative Commons Attribution ShareAlike 3.0. License

Connect the analog in, pin zero, into the center pin of the potentiometer.  Then, then next thing you need to do is connect the Ground(GND), to one of the outer pins.  The last one, goes from the power to the other outer pin.  The potentiometer is a variable resistor.  When you turn the shaft, the resistance increases, and when you turn it the other way, it decreases.  Then the voltage is converted to a digital value between 0 and 1023.  When it reads 0, there is no voltage, and when it reads 1023, 5 volts are going through the system.

Without Further Ado, the Code:(this is a very basic example)
void setup() {

  Serial.begin(9600);
}
void loop() {
  int sensorValue = analogRead(A0);
  float voltage = sensorValue * (5.0 / 1023.0);
  Serial.println(voltage);
}



So, in the void setup(), we are telling the Arduino to begin serial communication, at 9600 bits per second.  Then in the void loop(), we now tell the sensorValue to read the analogRead voltage, at analog pin 0(A0).  Then we find the voltage of the analogRead, turn it into a float(a whole number), and print the value.  And that's it for the basics section

Thanks for Reading
Liqhtm3ow
Read and comment on the full post

Archived

This topic is now archived and is closed to further replies.

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.