0% found this document useful (0 votes)
45 views

Arduino Analog Read

This document contains code for controlling a dual motor shield using an Arduino. It defines functions for initializing the motor shield pins and setting the speed for motor 1. The setup function initializes serial communication and sets the initial motor speeds to 0. The loop function reads analog pins and prints the values with delays.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Arduino Analog Read

This document contains code for controlling a dual motor shield using an Arduino. It defines functions for initializing the motor shield pins and setting the speed for motor 1. The setup function initializes serial communication and sets the initial motor speeds to 0. The loop function reads analog pins and prints the values with delays.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <L298.

h>

#define s Serial
L298 m(10,11,8,9);

void setup(){
s.begin(9600);
m.setSpeed(0,0);
}
void loop(){
for(int i =0; i <5; i++){
s.print(analogRead(i));
s.print(",");
}
s.println(analogRead(5));
delay(100);
}
DualVNH5019MotorShield::DualVNH5019MotorShield(unsigned char INA1, unsigned char INB1,
unsigned char EN1DIAG1, unsigned char CS1,
unsigned char INA2, unsigned char INB2, unsigned char EN2DIAG2, unsigned
char CS2)
{
//Pin map
//PWM1 and PWM2 cannot be remapped because the library assumes PWM is on timer1
_INA1 = INA1;
_INB1 = INB1;
_EN1DIAG1 = EN1DIAG1;
_CS1 = CS1;
_INA2 = INA2;

_INB2 = INB2;
_EN2DIAG2 = EN2DIAG2;
_CS2 = CS2;
}

// Public Methods //////////////////////////////////////////////////////////////


void DualVNH5019MotorShield::init()
{
// Define pinMode for the pins and set the frequency for timer1.

pinMode(_INA1,OUTPUT);
pinMode(_INB1,OUTPUT);
pinMode(_PWM1,OUTPUT);
pinMode(_EN1DIAG1,INPUT);
pinMode(_CS1,INPUT);
pinMode(_INA2,OUTPUT);
pinMode(_INB2,OUTPUT);
pinMode(_PWM2,OUTPUT);
pinMode(_EN2DIAG2,INPUT);
pinMode(_CS2,INPUT);
#if defined(__AVR_ATmega168__)|| defined(__AVR_ATmega328P__)
// Timer 1 configuration
// prescaler: clockI/O / 1
// outputs enabled
// phase-correct PWM
// top of 400
//
// PWM frequency calculation
// 16MHz / 1 (prescaler) / 2 (phase-correct) / 400 (top) = 20kHz

TCCR1A = 0b10100000;
TCCR1B = 0b00010001;
ICR1 = 400;
#endif
}
// Set speed for motor 1, speed is a number betwenn -400 and 400
void DualVNH5019MotorShield::setM1Speed(int speed)
{
unsigned char reverse = 0;

if (speed < 0)
{
speed = -speed; // Make speed a positive quantity
reverse = 1; // Preserve the direction
}
if (speed > 400) // Max PWM dutycycle
speed = 400;
#if defined(__AVR_ATmega168__)|| defined(__AVR_ATmega328P__)
OCR1A = speed;
#else
analogWrite(_PWM1,speed * 51 / 80); // default to using analogWrite, mapping 400 to 255
#endif
if (speed == 0)
{
digitalWrite(_INA1,LOW); // Make the motor coast no
digitalWrite(_INB1,LOW); // matter which direction it is spinning.
}
else if (reverse)
{

digitalWrite(_INA1,LOW);
digitalWrite(_INB1,HIGH);
}
else
{
digitalWrite(_INA1,HIGH);
digitalWrite(_INB1,LOW);
}
}

You might also like