Cannot get Code to run Motor on Crickit on CPX button press

Play with it! Please tell us which board you're using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
TitaniumTeeth
 
Posts: 4
Joined: Sun Jul 04, 2021 4:45 pm

Cannot get Code to run Motor on Crickit on CPX button press

Post by TitaniumTeeth »

Hi,

I am a newbie, and I cobbled together some arduino code to run a peristaltic pump hooked up to a CPX and Crickit.

My code compiles and runs fine and lights up the neopixels no problem, but I cannot get the motor to run when either the left or right CPX buttons are pressed.

It must be a fault in my logic or code, as I can get the motor to run with the CPX example sketch: dual motor demo.

Any help would be appreciated.

-----------------------------------

Code: Select all

#include "Adafruit_Crickit.h"
#include "seesaw_motor.h"
#include <Adafruit_CircuitPlayground.h>

bool leftButtonPressed;
bool rightButtonPressed;

int leftbuttonmotordelay = 5000;
int rightbuttonmotordelay = 1000;

Adafruit_Crickit crickit;

seesaw_Motor motor_a(&crickit);


void setup() 
{ CircuitPlayground.begin();
  //attach motor a
  motor_a.attach(CRICKIT_MOTOR_A1, CRICKIT_MOTOR_A2);

 Serial.begin(9600);           // set up Serial library at 9600 bps
  



}

void loop() {
 CircuitPlayground.clearPixels();
 CircuitPlayground.setPixelColor(4, 255,   0,   0);
 delay(1000);
  CircuitPlayground.setPixelColor(2, 255,   0,   0);

  Serial.println("Hello world!");  // prints hello with ending line break
  delay(1000);

  leftButtonPressed = CircuitPlayground.leftButton();
  rightButtonPressed = CircuitPlayground.rightButton();

  if (leftButtonPressed) {
    CircuitPlayground.setPixelColor(2, 255,   0,   0);
   motor_a.throttle(1);
   CircuitPlayground.setPixelColor(0, 255,   0,   0);
  delay(leftbuttonmotordelay);}
  else {
    motor_a.throttle(0);;
  }

 if (rightButtonPressed) {
   motor_a.throttle(1);
   CircuitPlayground.setPixelColor(2,   0, 255,   0);
  delay(rightbuttonmotordelay);}
else {
    motor_a.throttle(0);;
  }



  // exit the loop 
//exit(0);  //0 is required to prevent error.

}
Attachments
Pump Circuit and Hookup.jpg
Pump Circuit and Hookup.jpg (417.88 KiB) Viewed 222 times

User avatar
dastels
 
Posts: 15662
Joined: Tue Oct 20, 2015 3:22 pm

Re: Cannot get Code to run Motor on Crickit on CPX button pr

Post by dastels »

Does the code on https://learn.adafruit.com/adafruit-cri ... c-motors-2 work (after BANNED themotor_b related bits)?

Dave

User avatar
TitaniumTeeth
 
Posts: 4
Joined: Sun Jul 04, 2021 4:45 pm

Re: Cannot get Code to run Motor on Crickit on CPX button pr

Post by TitaniumTeeth »

Hi Dave,

Thanks for your help.

Yes, the code you linked to runs the motor no problem.

That is the code example I used in my sketch.

I think the problem lies somewhere in here:

Code: Select all

  leftButtonPressed = CircuitPlayground.leftButton();
  rightButtonPressed = CircuitPlayground.rightButton();

  if (leftButtonPressed) {
    CircuitPlayground.setPixelColor(2, 255,   0,   0);
   motor_a.throttle(1);
   CircuitPlayground.setPixelColor(0, 255,   0,   0);
  delay(leftbuttonmotordelay);}
  else {
    motor_a.throttle(0);;
  }

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

Return to “Circuit Playground Classic, Circuit Playground Express, Circuit Playground Bluefruit”