Electromaniacs
Would you like to react to this message? Create an account in a few clicks or log in to continue.
Electromaniacs

Where Imagination resonates with Innovation


You are not connected. Please login or register

Arduino Programming

2 posters

Go down  Message [Page 1 of 1]

1Arduino Programming Empty Arduino Programming Fri Dec 19, 2014 6:36 pm

Admin


Admin

Lets try to learn arduino programming and implement it in some useful application.

https://electromaniacs.rpg-board.net

2Arduino Programming Empty Re: Arduino Programming Fri Dec 19, 2014 6:40 pm

pravin



Ok. Let's first try to understand the basics

3Arduino Programming Empty Re: Arduino Programming Fri Dec 19, 2014 9:35 pm

Admin


Admin

The setup() function is called when a sketch starts. Use it to initialize variables, pin modes, start using libraries, etc. The setup function will only run once, after each powerup or reset of the Arduino board.
After creating a setup() function, the loop() function does precisely what its name suggests, and loops consecutively, allowing your program to change and respond as it runs. Code in the loop() section of your sketch is used to actively control the Arduino board.

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

}

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

https://electromaniacs.rpg-board.net

4Arduino Programming Empty Re: Arduino Programming Fri Dec 19, 2014 9:38 pm

Admin


Admin

A led Blinking program:
/*
 Blink
 Turns on an LED on for one second, then off for one second, repeatedly.

 This example code is in the public domain.
*/

// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;

// the setup routine runs once when you press reset:
void setup() {                
 // initialize the digital pin as an output.
 pinMode(led, OUTPUT);    
}

// the loop routine runs over and over again forever:
void loop() {
 digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
 delay(1000);               // wait for a second
 digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
 delay(1000);               // wait for a second
}

https://electromaniacs.rpg-board.net

5Arduino Programming Empty Re: Arduino Programming Fri Dec 19, 2014 9:39 pm

Admin


Admin

/*
 DigitalReadSerial
Reads a digital input on pin 2, prints the result to the serial monitor

This example code is in the public domain.
*/

// digital pin 2 has a pushbutton attached to it. Give it a name:
int pushButton = 2;

// the setup routine runs once when you press reset:
void setup() {
 // initialize serial communication at 9600 bits per second:
 Serial.begin(9600);
 // make the pushbutton's pin an input:
 pinMode(pushButton, INPUT);
}

// the loop routine runs over and over again forever:
void loop() {
 // read the input pin:
 int buttonState = digitalRead(pushButton);
 // print out the state of the button:
 Serial.println(buttonState);
 delay(1);        // delay in between reads for stability
}

https://electromaniacs.rpg-board.net

6Arduino Programming Empty Re: Arduino Programming Fri Dec 19, 2014 9:40 pm

Admin


Admin

/*
AnalogReadSerial
Reads an analog input on pin 0, prints the result to the serial monitor.
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

This example code is in the public domain.
*/

// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// print out the value you read:
Serial.println(sensorValue);
delay(1); // delay in between reads for stability
}

https://electromaniacs.rpg-board.net

7Arduino Programming Empty Re: Arduino Programming Fri Dec 19, 2014 9:40 pm

Admin


Admin

/*
Fade

This example shows how to fade an LED on pin 9
using the analogWrite() function.

This example code is in the public domain.
*/

int led = 9;           // the pin that the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

// the setup routine runs once when you press reset:
void setup()  {
 // declare pin 9 to be an output:
 pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop()  {
 // set the brightness of pin 9:
 analogWrite(led, brightness);    

 // change the brightness for next time through the loop:
 brightness = brightness + fadeAmount;

 // reverse the direction of the fading at the ends of the fade:
 if (brightness == 0 || brightness == 255) {
   fadeAmount = -fadeAmount ;
 }    
 // wait for 30 milliseconds to see the dimming effect    
 delay(30);                            
}

https://electromaniacs.rpg-board.net

8Arduino Programming Empty Re: Arduino Programming Fri Dec 19, 2014 9:41 pm

Admin


Admin

/*
ReadAnalogVoltage
Reads an analog input on pin 0, converts it to voltage, and prints the result to the serial monitor.
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

This example code is in the public domain.
*/

// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
// print out the value you read:
Serial.println(voltage);
}

https://electromaniacs.rpg-board.net

9Arduino Programming Empty Re: Arduino Programming Fri Dec 19, 2014 9:47 pm

Admin


Admin

The above listed examples contain all basic commands of arduino IDE from their official website.



Pls refer to for installation basics and for arduino digital,analog and PWM port details.

https://electromaniacs.rpg-board.net

10Arduino Programming Empty To sum things up: Fri Dec 19, 2014 10:14 pm

Admin


Admin


pinMode(pinnumber, INPUT/OUTPUT);                //To designate a pin to be output or input
digitalWrite(pinnumber,HIGH/LOW);                   //To make a pin output 5v or 0v
digitalRead(pinnumber);                                   // Returns the state of the pin
Serial.begin(9600);                                          // To start a serial communication between two devices.
Serial.println(char);                                         // To print the character given as argument(inside brackets) to the computer screen.
analogRead(pinnumber);                                   //uses adc converter to read analog value and give o/p between 0-1023
analogWrite(pinnumber,value);                          // based on the magnitude of 'value'(function argument), proportional output is given between 0-255.

https://electromaniacs.rpg-board.net

Admin


Admin

The code below uses the millis() function, a command that returns the number of milliseconds since the Arduino board started running its current program, to blink an LED.
/* Blink without Delay

Turns on and off a light emitting diode(LED) connected to a digital  
pin, without using the delay() function.  This means that other code
can run at the same time without being interrupted by the LED code.

The circuit:
* LED attached from pin 13 to ground.
* Note: on most Arduinos, there is already an LED on the board
that's attached to pin 13, so no hardware is needed for this example.

*/

// constants won't change. Used here to
// set pin numbers:
const int ledPin =  13;      // the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
 // set the digital pin as output:
 pinMode(ledPin, OUTPUT);      
}

void loop()
{
 // here is where you'd put code that needs to be running all the time.

 // check to see if it's time to blink the LED; that is, if the
 // difference between the current time and last time you blinked
 // the LED is bigger than the interval at which you want to
 // blink the LED.
 unsigned long currentMillis = millis();

 if(currentMillis - previousMillis > interval) {
   // save the last time you blinked the LED
   previousMillis = currentMillis;  

   // if the LED is off turn it on and vice-versa:
   if (ledState == LOW)
     ledState = HIGH;
   else
     ledState = LOW;

   // set the LED with the ledState of the variable:
   digitalWrite(ledPin, ledState);
 }
}

https://electromaniacs.rpg-board.net

12Arduino Programming Empty controlling an led using a push button Fri Dec 19, 2014 11:47 pm

Admin


Admin

Arduino Programming Button_schem */
/*
 Button

Turns on and off a light emitting diode(LED) connected to digital  
pin 13, when pressing a pushbutton attached to pin 2.


The circuit:
* LED attached from pin 13 to ground
* pushbutton attached to pin 2 from +5V
* 10K resistor attached to pin 2 from ground

* Note: on most Arduinos there is already an LED on the board
attached to pin 13.

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
 // initialize the LED pin as an output:
 pinMode(ledPin, OUTPUT);      
 // initialize the pushbutton pin as an input:
 pinMode(buttonPin, INPUT);    
}

void loop(){
 // read the state of the pushbutton value:
 buttonState = digitalRead(buttonPin);

 // check if the pushbutton is pressed.
 // if it is, the buttonState is HIGH:
 if (buttonState == HIGH) {    
   // turn LED on:    
   digitalWrite(ledPin, HIGH);  
 }
 else {
   // turn LED off:
   digitalWrite(ledPin, LOW);
 }
}

https://electromaniacs.rpg-board.net

13Arduino Programming Empty how to debounce an input Sat Dec 20, 2014 8:51 pm

pravin



/*
Debounce(checking twice in a short period of time to make sure it's definitely pressed.)

Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
press), the output pin is toggled from LOW to HIGH or HIGH to LOW.  There's
a minimum delay between toggles to debounce the circuit (i.e. to ignore
noise).  

The circuit:
* LED attached from pin 13 to ground
* pushbutton attached from pin 2 to +5V
* 10K resistor attached from pin 2 to ground

* Note: On most Arduino boards, there is already an LED on the board
connected to pin 13, so you don't need any extra components for this example.
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2;    // the number of the pushbutton pin
const int ledPin = 13;      // the number of the LED pin

// Variables will change:
int ledState = HIGH;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
 pinMode(buttonPin, INPUT);
 pinMode(ledPin, OUTPUT);

 // set initial LED state
 digitalWrite(ledPin, ledState);
}

void loop() {
 // read the state of the switch into a local variable:
 int reading = digitalRead(buttonPin);

 // check to see if you just pressed the button
 // (i.e. the input went from LOW to HIGH),  and you've waited
 // long enough since the last press to ignore any noise:  

 // If the switch changed, due to noise or pressing:
 if (reading != lastButtonState) {
   // reset the debouncing timer
   lastDebounceTime = millis();
 }
 
 if ((millis() - lastDebounceTime) > debounceDelay) {
   // whatever the reading is at, it's been there for longer
   // than the debounce delay, so take it as the actual current state:

   // if the button state has changed:
   if (reading != buttonState) {
     buttonState = reading;

     // only toggle the LED if the new button state is HIGH
     if (buttonState == HIGH) {
       ledState = !ledState;
     }
   }
 }
 
 // set the LED:
 digitalWrite(ledPin, ledState);

 // save the reading.  Next time through the loop,
 // it'll be the lastButtonState:
 lastButtonState = reading;
}

Sponsored content



Back to top  Message [Page 1 of 1]

Similar topics

-

» CLONING ARDUINO

Permissions in this forum:
You cannot reply to topics in this forum