You are on page 1of 13

No es eficiente por el polling

boolean anterior = 0;
boolean actual = 0;
int contador = 0;
void setup()
{
Serial.begin(9600);
pinMode(2,INPUT);
}
boolean debounce(boolean dato_anterior)
{
boolean dato_actual = digitalRead(2);
if (dato_anterior != dato_actual)
{
delay(10);
dato_actual = digitalRead(2);
}
return dato_actual;
}
void loop()
{
actual = debounce(anterior);
if ( anterior == 0 && actual == 1)
{
contador++;
delay (100);
Serial.println(contador);
}
}

anterior = actual;

// by Jorge A. Sanchez, Revisa cuantos pulsos PPR


int long encoder0PinA = 3; //pin 3 aurduino uno
int long encoder0PinB = 4; //pin 4 aurduino uno
int long encoder0Pos = 0;
int encoder0PinALast = LOW;
int n = LOW;
float grados=0;
void setup() {
pinMode (encoder0PinA,INPUT);
pinMode (encoder0PinB,INPUT);
Serial.begin (115200);
}
void loop() {
n = digitalRead(encoder0PinA);
if ((encoder0PinALast == LOW) && (n == HIGH)) {
if (digitalRead(encoder0PinB) == LOW) {
encoder0Pos--;
grados = ((encoder0Pos*360/255)); //cambia 255 por tus
pulsos de tu encoder
if (encoder0Pos == -255){(encoder0Pos = 0);}
}
else {
encoder0Pos++;
grados = ((encoder0Pos*360/255));
if (encoder0Pos == 255){(encoder0Pos = 0);}
}
Serial.print ("GRADOS = ");
Serial.println (grados);
}
encoder0PinALast = n;
}

Uso de interrupcin externa 0, pin 2, tarjeta


arduino UNO
int contador = 0;
int n = contador ;
void setup()
{
Serial.begin(9600);
attachInterrupt( 0, SerIntExt_0, RISING);
}
void loop()
{
if (n != contador)
{
Serial.println(contador);
n = contador ;
}
}
Void SerIntExt_0 ()
{
contador++ ;
}

/*

MonsterMoto Shield Example Sketch

date: 5/24/11
code by: Jim Lindblom
hardware by: Nate Bernstein
SparkFun Electronics

//License: CC-SA 3.0, feel free to use this code however you'd like.
Please improve upon it! Let me know how you've made it better.

This is really simple example code to get you some basic


functionality with the MonsterMoto Shield. The MonsterMote uses
two VNH2SP30 high-current full-bridge motor drivers.

Use the motorGo(uint8_t motor, uint8_t direct, uint8_t pwm)


function to get motors going in either CW, CCW, BRAKEVCC, or
BRAKEGND. Use motorOff(int motor) to turn a specific motor off.

The motor variable in each function should be either a 0 or a 1.


pwm in the motorGo function should be a value between 0 and 255.
*/
#define BRAKEVCC 0
#define CW 1
#define CCW 2
#define BRAKEGND 3
#define CS_THRESHOLD 15

/* VNH2SP30 pin definitions


xxx[0] controls '1' outputs

xxx[1] controls '2' outputs */


int inApin[2] = {7, 4}; // INA: Clockwise input
int inBpin[2] = {8, 9}; // INB: Counter-clockwise input
int pwmpin[2] = {5, 6}; // PWM input
int cspin[2] = {2, 3}; // CS: Current sense ANALOG input
int enpin[2] = {0, 1}; // EN: Status of switches output (Analog pin)

int statpin = 13;

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

pinMode(statpin, OUTPUT);

// Initialize digital pins as outputs


for (int i=0; i<2; i++)
{
pinMode(inApin[i], OUTPUT);
pinMode(inBpin[i], OUTPUT);
pinMode(pwmpin[i], OUTPUT);
}
// Initialize braked
for (int i=0; i<2; i++)
{
digitalWrite(inApin[i], LOW);
digitalWrite(inBpin[i], LOW);
}

void loop()
{
motorGo(0, CW, 0);
//motorGo(0, CCW, 26);
//motorGo(1, CCW, 128);
delay(500);
//motorGo(0, CW, 128);
//delay(1000);
if(analogRead(cspin[0]) > CS_THRESHOLD)
//if ((analogRead(cspin[0]) < CS_THRESHOLD) && (analogRead(cspin[1]) <
CS_THRESHOLD))
digitalWrite(statpin, HIGH);
}

void motorOff(int motor)


{
// Initialize braked
for (int i=0; i<2; i++)
{
digitalWrite(inApin[i], LOW);
digitalWrite(inBpin[i], LOW);
}
analogWrite(pwmpin[motor], 0);
}

/* motorGo() will set a motor going in a specific direction


the motor will continue going in that direction, at that speed
until told to do otherwise.

motor: this should be either 0 or 1, will selet which of the two


motors to be controlled

direct: Should be between 0 and 3, with the following result


0: Brake to VCC
1: Clockwise
2: CounterClockwise
3: Brake to GND

pwm: should be a value between ? and 1023, higher the number, the faster
it'll go
*/
void motorGo(uint8_t motor, uint8_t direct, uint8_t pwm)
{
if (motor <= 1)
{
if (direct <=4)
{
// Set inA[motor]
if (direct <=1)
digitalWrite(inApin[motor], HIGH);
else
digitalWrite(inApin[motor], LOW);

// Set inB[motor]
if ((direct==0)||(direct==2))
digitalWrite(inBpin[motor], HIGH);
else
digitalWrite(inBpin[motor], LOW);

analogWrite(pwmpin[motor], pwm);
}
}
}

/*
Copyright 2011 Lex Talionis (Lex.V.Talionis at gmail)
This program is free software: you can redistribute it
and/or modify it under the terms of the GNU General Public
License as published by the Free Software Foundation,
either version 3 of the License, or (at your option) any
later version.

This uses pin change

interrupts and timer 1 to

mesure the
time between the rise and fall

of 3 channels of PPM

(Though often called PWM, see http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?


num=1253149521/all)
on a typical RC car reciver. It could be extended to as
many channels as you like.

*/
#include <PinChangeInt.h>
//
http://www.arduino.cc/playground/Main/PinChangeInt
#include <PinChangeIntConfig.h>
#include <TimerOne.h>
//
http://www.arduino.cc/playground/Code/Timer1

#define NO_PORTB_PINCHANGES //PinChangeInt setup


#define NO_PORTC_PINCHANGES
//only port D pinchanges (see:
http://www.arduino.cc/playground/Learning/Pins)
#define PIN_COUNT 3
#define

//number of channels attached to the reciver

MAX_PIN_CHANGE_PINS PIN_COUNT

#define RC_TURN 3

//arduino pins attached to the reciver

#define RC_FWD 2
#define RC_FIRE 4
byte pin[] = {RC_FWD, RC_TURN, RC_FIRE}; //for maximum efficency thise pins
should be attached
unsigned int time[] = {0,0,0};
in the order listed here

// to the reciver's channels

byte state=0;
byte burp=0;

// a counter to see how many times the int has executed

byte cmd=0;

// a place to put our serial data

byte i=0;

// global counter for tracking what pin we are on

void setup() {
Serial.begin(115200);
Serial.print("PinChangeInt ReciverReading test");
Serial.println();

Timer1.initialize(2200);

//warm up the serial port

//longest pulse in PPM is usally 2.1 milliseconds,


//pick a period that gives

you a little headroom.


Timer1.stop();
Timer1.restart();

//stop the counter


//set the clock to zero

for (byte i=0; i<3; i++)


{
pinMode(pin[i], INPUT);

//set the pin to input

digitalWrite(pin[i], HIGH); //use the internal pullup resistor


}
PCintPort::attachInterrupt(pin[i], rise,RISING); // attach a PinChange
Interrupt to our first pin
}

void loop() {

cmd=Serial.read();

//while you got some time gimme a systems

report
if (cmd=='p')
{
Serial.print("time:\t");
for (byte i=0; i<PIN_COUNT;i++)
{
Serial.print(i,DEC);
Serial.print(":");
Serial.print(time[i],DEC);
Serial.print("\t");
}
Serial.print(burp, DEC);
Serial.println();
/*

Serial.print("\t");
Serial.print(clockCyclesToMicroseconds(Timer1.pwmPeriod), DEC);
Serial.print("\t");
Serial.print(Timer1.clockSelectBits, BIN);
Serial.print("\t");
Serial.println(ICR1, DEC);*/
}
cmd=0;

switch (state)
{
case RISING: //we have just seen a rising edge
PCintPort::detachInterrupt(pin[i]);

PCintPort::attachInterrupt(pin[i], fall, FALLING); //attach the


falling end
state=255;
break;
case FALLING: //we just saw a falling edge
PCintPort::detachInterrupt(pin[i]);
i++;

//move to the next pin

i = i % PIN_COUNT; //i ranges from 0 to PIN_COUNT


PCintPort::attachInterrupt(pin[i], rise,RISING);
state=255;
break;
/*default:
//do nothing
break;*/
}
}

void rise()

//on the rising edge of the currently intresting pin

{
Timer1.restart();
Timer1.start();

//set our stopwatch to 0


//and start it up

state=RISING;
//

Serial.print('r');
burp++;

void fall()

//on the falling edge of the signal

{
state=FALLING;

time[i]=Timer1.read();
Timer1.stop();
//
}

Serial.print('f');

// Needs Timer1-v2

You might also like