Saturday, December 14, 2013

Coding is so fun.

SO for Mr. Marvin I was able to get both components working.

 Unfortunately I was not able to get them to work together.
 So I was able to get the trigger and sound fx working, and the motors to work with code, but couldn't get them in unison.

 Here is the very simple code that was used to move Marvin. (note: a separate Adafruit library has to be installed in order for it to work.)


#include <AFMotor.h>

AF_DCMotor motor3(3, MOTOR34_64KHZ); // create motor #3, 64KHz pwm

AF_DCMotor motor4(4, MOTOR34_64KHZ); // create motor #3, 64KHz pwm


void setup()
{
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Motor test!");
 
  motor3.setSpeed(200);     // set the speed to 200/255
  motor4.setSpeed(200);     // set the speed to 200/255
}

void loop()
{
  Serial.print("tick");
 
  motor3.run(FORWARD);      // turn it on going forward
  motor4.run(FORWARD);      // turn it on going forward
  delay(1100);

 Serial.print("tack");
  motor3.run(RELEASE);      // stopped
  motor4.run(RELEASE);      // stopped
  delay(1000);

  Serial.print("tock");
  motor3.run(BACKWARD);     // the other way
  motor4.run(BACKWARD);     // the other way
  delay(1000);
 
  Serial.print("tack");
  motor3.run(RELEASE);      // stopped
  motor4.run(RELEASE);      // stopped
  delay(1000);
}





 This is the code that was written for 'everything' and it was very quickly garbage because the motor controller that we used didnt follow the Arduino format at all. And thats OK, you win some and lose others.



int valm=0;
int valm1=0;
int trigger=0;
int triggerlock=0;
const int pwmA=3;
const int pwmB=11;
const int brakeA=9;
const int brakeB=8;
const int dirA=12;
const int dirB=13;
const int relay=2;




void setup()

{
pinMode(relay, OUTPUT);

digitalWrite(relay, HIGH);

delay(500);//leave relay on .5 second--makes "eek" sound
digitalWrite(relay, LOW);
}


void loop()

{

    trigger=analogRead(3);
    if(trigger<500 or triggerlock>0) {//light shining on sensor now or previously
   
      triggerlock=100;//don't check light again
   
   
  pinMode(dirA, OUTPUT);
  pinMode(brakeA, OUTPUT);
 
  pinMode(dirB, OUTPUT);
  pinMode(brakeB, OUTPUT);
 
  digitalWrite(dirA, HIGH);//forward A
  digitalWrite(brakeA, LOW);//release brake A
  analogWrite(pwmA, 150);//set speed A
 
  digitalWrite(dirB, HIGH);//forward B motor
  digitalWrite(brakeB, LOW);
  analogWrite(pwmB, 150);//set speed B
 


valm=analogRead(0);
valm1=analogRead(1);
if(valm>520 or valm1>520)

{

      digitalWrite(relay, HIGH);//turn on "eek" sound
      delay(100);
      digitalWrite(relay, LOW);
      digitalWrite(brakeA, HIGH);//stop motor A
      digitalWrite(brakeB, HIGH);//stop Motor B
     
      digitalWrite(dirA, LOW);//reverse A
      digitalWrite(brakeA, LOW);//release brake A
      analogWrite(pwmA, 150);//set speed A
     
      digitalWrite(dirB, LOW);//reverse B
      digitalWrite(brakeB, LOW);
      analogWrite(pwmB, 150);//set speed B
     
      delay(300);
     
      digitalWrite(brakeA, HIGH);//stop one wheel
     
      delay(400);
      digitalWrite(brakeB, HIGH);//stop other wheel



    //start both wheels forward
    digitalWrite(dirA, HIGH);//forward A
    digitalWrite(brakeA, LOW);//release brake A
    analogWrite(pwmA, 150);//set speed A
   
    digitalWrite(dirB, HIGH);//forward B
    digitalWrite(brakeB, LOW);
    analogWrite(pwmB, 150);//set speed B
   
    delay(700);//get past startup current

}
}
}

No comments:

Post a Comment