Programing Motor Shield V2

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Crock101
 
Posts: 32
Joined: Wed May 28, 2014 2:19 pm

Re: Programing Motor Shield V2

Post by Crock101 »

Ok, so i got the motor-controller software to work with my arduino uno. However when i try to upload this code:

Code: Select all

#include <Adafruit_MotorShield.h>
#include <Wire.h>
const int pingPin = 12;
unsigned int duration, inches;
int Stay = 11;
char s = 0;
boolean lastState = false;
Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x60);
Adafruit_DCMotor *LM = AFMS.getMotor(1);
Adafruit_DCMotor *RM = AFMS.getMotor(2);
/*
  IRSeeker.ino - A library/class for the HiTechnic IRSeeker V2 infrared sensor.

 */

struct InfraredResult
{
  byte Direction;
  byte Strength;
};

class InfraredSeeker
{
public:
  static void Initialize();
  static boolean Test();
  static void ReadACRaw(byte* buffer);
  static void ReadDCRaw(byte* buffer);
  static InfraredResult ReadAC();
  static InfraredResult ReadDC();
  static int DirectionAngle(byte Direction);
private:
  static InfraredResult PopulateValues(byte* buffer);
  static void ReadValues(byte OffsetAddress, byte* buffer);
  static const int Address = 0x10 / 2; //Divide by two as 8bit-I2C address is provided
};

void InfraredSeeker::Initialize()
{
  Wire.begin();
  Wire.beginTransmission(InfraredSeeker::Address);
  Wire.write(0x00);
  Wire.endTransmission();
  while(Wire.available() > 0)
    Wire.read();
}

boolean InfraredSeeker::Test()
{
  Wire.beginTransmission(InfraredSeeker::Address);
  Wire.write(0x08);
  Wire.endTransmission();
  Wire.requestFrom(InfraredSeeker::Address, 16);
  char Manufacturer_Model[16];
  while(Wire.available() < 16);
  for(byte i=0; i < 16; i++)
  {
    Manufacturer_Model[i] = Wire.read();
  }
  while(Wire.available() > 0)
    Wire.read();
  return strncmp(Manufacturer_Model, "HiTechncNewIRDir", 16)==0;
}

void InfraredSeeker::ReadValues(byte OffsetAddress, byte* buffer)
{
  Wire.beginTransmission(InfraredSeeker::Address);
  Wire.write(OffsetAddress);
  Wire.endTransmission();
  Wire.requestFrom(InfraredSeeker::Address, 6);
  while(Wire.available() < 6);
  for(byte i = 0; i < 6; i++)
  {
    buffer[i] = Wire.read();
  }
  while(Wire.available() > 0)
    Wire.read();
}

void InfraredSeeker::ReadACRaw(byte* buffer)
{
  ReadValues(0x49, buffer);
}

void InfraredSeeker::ReadDCRaw(byte* buffer)
{
  ReadValues(0x42, buffer);
}

InfraredResult InfraredSeeker::PopulateValues(byte* buffer)
{
  InfraredResult Data;
  Data.Direction = buffer[0];
  if(buffer[0] != 0)
  {
    if(buffer[0] % 2 == 0)
    {
      Data.Strength = (buffer[buffer[0] / 2] + buffer[buffer[0] / 2 + 1]) / 2;
    }
    else
    {
      Data.Strength = buffer[buffer[0] / 2 + 1];
    }
  }
  else
  {
    Data.Strength = 0;
  }
  return Data;
}

InfraredResult InfraredSeeker::ReadAC()
{
  byte buffer[6];
  ReadACRaw(buffer);
  return PopulateValues(buffer);
}

InfraredResult InfraredSeeker::ReadDC()
{
  byte buffer[6];
  ReadDCRaw(buffer);
  return PopulateValues(buffer);
}

int DirectionAngle(byte Direction)
{
  return Direction * 30 - 150;
}

void setup()
{
  pinMode(Stay,INPUT);
  Serial.begin(9600);
  Serial.println("HiTechnic IRSeeker V2");
  Serial.println();
  Serial.println();
  Serial.println("Dir\tAngle\tStrength");
  Serial.println();
  InfraredSeeker::Initialize();
  AFMS.begin();
}

void loop()
{   
  InfraredResult InfraredBall = InfraredSeeker::ReadAC();
  Serial.print(InfraredBall.Direction);
  Serial.print("\t");
  Serial.print(DirectionAngle(InfraredBall.Direction));
  Serial.print("\t");
  Serial.print(InfraredBall.Strength);
  Serial.println();
  delay(200); //optional

  pinMode(pingPin, OUTPUT);          
  digitalWrite(pingPin, LOW);        
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);       
  delayMicroseconds(5);              
  digitalWrite(pingPin, LOW);        
  pinMode(pingPin, INPUT);           
  duration = pulseIn(pingPin, HIGH); 
  inches = duration / 74 / 2;        
  Serial.println(inches);            
  delay(200);		             

boolean state = digitalRead(Stay) ;
 if( state != lastState )
 {
   if( state == HIGH )
      s = !s;

   lastState = state; 
 }
 
  if (s == 1)
  {
  LM->run(RELEASE);
  RM->run(RELEASE);
  }

if (s == 0)
{
  if (InfraredBall.Direction >= 1 && InfraredBall.Direction < 5)
  {
    LM->run(BACKWARD);
    LM->setSpeed(255);
    RM->run(FORWARD);
    RM->setSpeed(255);
  }

  if (InfraredBall.Direction > 5 && InfraredBall.Direction <= 9)
  {
    LM->run(FORWARD);
    LM->setSpeed(255);
    RM->run(BACKWARD);
    RM->setSpeed(255);
 }


  if (InfraredBall.Direction == 5 && inches > 50)
  {
    LM->run(FORWARD);
    LM->setSpeed(255);
    RM->run(FORWARD);
    RM->setSpeed(255);
  }

  if (InfraredBall.Direction == 5 && inches < 9)
  {
    LM->run(BACKWARD);
    LM->setSpeed(255);
    RM->run(BACKWARD);
    RM->setSpeed(255);
  }

  if (InfraredBall.Direction == 5 && inches <= 50 && inches >= 9)
  {
    LM->run(RELEASE);
    RM->run(RELEASE); 
  }
   if (InfraredBall.Direction == 0)
  {
    LM->run(RELEASE);
    RM->run(RELEASE);
  }
 }
}
I get this back:

Code: Select all

         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2009 Joerg Wunsch

         System wide configuration file is "C:\Users\Colin\Desktop\Arduino\hardware/tools/avr/etc/avrdude.conf"

         Using Port                    : \\.\COM10
         Using Programmer              : arduino
         Overriding Baud Rate          : 115200
avrdude: Send: 0 [30]   [20] 
avrdude: Send: 0 [30]   [20] 
avrdude: Send: 0 [30]   [20] 
avrdude: Recv: . [00] 
avrdude: stk500_getsync(): not in sync: resp=0x00

avrdude done.  Thank you.
Last edited by Crock101 on Sat Sep 19, 2015 6:20 pm, edited 1 time in total.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Programing Motor Shield V2

Post by adafruit_support_bill »

Make sure you have the right board-type and com port selected in the IDE.

Crock101
 
Posts: 32
Joined: Wed May 28, 2014 2:19 pm

Re: Programing Motor Shield V2

Post by Crock101 »

Yes it's set as an arduino uno, and uploading this code works:

Code: Select all

#include <Adafruit_MotorShield.h>
#include <Wire.h>
Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x60);
Adafruit_DCMotor *RFM = AFMS.getMotor(1);
Adafruit_DCMotor *RBM = AFMS.getMotor(2);
Adafruit_DCMotor *LFM = AFMS.getMotor(4);
Adafruit_DCMotor *LBM = AFMS.getMotor(3);

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

void loop()
{
  LFM->run(RELEASE);
  LBM->run(RELEASE);
  RFM->run(RELEASE);
  RBM->run(RELEASE);
  Serial.print("check");
    delay(5000);
  LFM->run(FORWARD);
  LFM->setSpeed(255);
  Serial.print("check");
    delay(5000);
  LFM->run(BACKWARD);
  LFM->setSpeed(255);
  Serial.print("check");
    delay(5000);
  LFM->run(RELEASE);
  RFM->run(FORWARD);
  RFM->setSpeed(255);
  Serial.print("check");
    delay(5000);
  RFM->run(BACKWARD);
  RFM->setSpeed(255);
  Serial.print("check");
    delay(5000);
  RFM->run(RELEASE);
  LBM->run(FORWARD);
  LBM->setSpeed(255);
  Serial.print("check");
    delay(5000);
  LBM->run(BACKWARD);
  LBM->setSpeed(255);
  Serial.print("check");
    delay(5000);
  LBM->run(RELEASE);
  RBM->run(FORWARD);
  RBM->setSpeed(255);
  Serial.print("check");
    delay(5000);
  RBM->run(BACKWARD);
  RBM->setSpeed(255);
  Serial.print("check");
    delay(5000);
  RBM->run(RELEASE);
  Serial.print("repeat");
}
Is there anything blatantly wrong?
Last edited by Crock101 on Fri Jun 27, 2014 9:58 pm, edited 1 time in total.

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: Programing Motor Shield V2

Post by adafruit_support_mike »

That number 'COM10' looks a bit suspicious too. You may have some redundant ports hanging around.

Try the instructions in this tutorial to find and get rid of the unnecessary ones, and see if that helps: https://learn.adafruit.com/how-to-find-hidden-com-ports

Crock101
 
Posts: 32
Joined: Wed May 28, 2014 2:19 pm

Re: Programing Motor Shield V2

Post by Crock101 »

Would that effect the uploading process?

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: Programing Motor Shield V2

Post by adafruit_support_mike »

It could, yes.

Every port consumes resources in the OS. When a bunch of unused ones are sitting around, the one you actually need can get lost in the crowd.

Crock101
 
Posts: 32
Joined: Wed May 28, 2014 2:19 pm

Re: Programing Motor Shield V2

Post by Crock101 »

Sorry for the wait, i got rid of the other extra ports and the upload still fails and i get this back in the dialog box

Code: Select all

Binary sketch size: 9,466 bytes (of a 32,256 byte maximum)
C:\Users\Colin\Desktop\Arduino\hardware/tools/avr/bin/avrdude -CC:\Users\Colin\Desktop\Arduino\hardware/tools/avr/etc/avrdude.conf -v -v -v -v -patmega328p -carduino -P\\.\COM7 -b115200 -D -Uflash:w:C:\Users\Colin\AppData\Local\Temp\build7047363739367887423.tmp\sketch_Robot_code_final.cpp.hex:i 

avrdude: Version 5.11, compiled on Sep  2 2011 at 19:38:36
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2009 Joerg Wunsch

         System wide configuration file is "C:\Users\Colin\Desktop\Arduino\hardware/tools/avr/etc/avrdude.conf"

         Using Port                    : \\.\COM7
         Using Programmer              : arduino
         Overriding Baud Rate          : 115200
avrdude: Send: 0 [30]   [20] 
avrdude: Send: 0 [30]   [20] 
avrdude: Send: 0 [30]   [20] 
avrdude: Recv: . [00] 
avrdude: stk500_getsync(): not in sync: resp=0x00

avrdude done.  Thank you.
Any suggestions?

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Programing Motor Shield V2

Post by adafruit_support_bill »

avrdude: Send: 0 [30] [20]
avrdude: Send: 0 [30] [20]
avrdude: Send: 0 [30] [20]
avrdude: Recv: . [00]
Your processor board is not responding at all. Do you see any LED activity on the board when attempting to upload?

Crock101
 
Posts: 32
Joined: Wed May 28, 2014 2:19 pm

Re: Programing Motor Shield V2

Post by Crock101 »

One orange LED remains steady while the other blinks 7 times during the upload and blinks steadily after the upload fails. Has anyone else had this issue?

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Programing Motor Shield V2

Post by adafruit_support_bill »

Is there any led activity when you press the reset button?

There are a few possible explanations for no response:
1) The 16U2 USB/Serial chip is bad. sometimes these can be re-flashed.
2) The bootloader is corrupted. This may be able to be re-flashed.
3) The processor is dead.

Crock101
 
Posts: 32
Joined: Wed May 28, 2014 2:19 pm

Re: Programing Motor Shield V2

Post by Crock101 »

The oreange LED that is always on flashes a bit then turns off and comes back on when the reset button is pressed. Could my computer be corrupting this boards?

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Programing Motor Shield V2

Post by adafruit_support_bill »

If it flashes 3 times, then the bootloader is running & the main processor is probably fine. In that case, it is most likely the 16U2 chip that is corrupted. If you check over at the Arduino.cc forums, some people have had luck re-flashing the firmware for this.

Crock101
 
Posts: 32
Joined: Wed May 28, 2014 2:19 pm

Re: Programing Motor Shield V2

Post by Crock101 »

Ok, I re-flashed the firmware and nothing changed.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Programing Motor Shield V2

Post by adafruit_support_bill »

I don't know of anything else to try. It sounds like your Arduino is beyond repair.

Crock101
 
Posts: 32
Joined: Wed May 28, 2014 2:19 pm

Re: Programing Motor Shield V2

Post by Crock101 »

Next time I will wait until I have the final code before I start uploading it, because it seems like I'm able to upload once or twice before It starts refusing uploads. According to what I've heard it's because of the new 16u2 chips that Arduino put into their R3 boards.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”