🇺🇸Adafruit will not be shipping orders on Independence Day Monday 7/4/2022. Please allow extra time for your order to ship and plan accordingly.🇺🇸
0

Air Valve Frenzy (Crickit + CPB + Arduino)
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Air Valve Frenzy (Crickit + CPB + Arduino)

by omnistraight on Wed May 18, 2022 11:31 pm

Hey y'all, so I am excited to say that much of my code is finally working for my class project, but I am having issues with the Air Valve. To explain, I'm running two air pumps (one for air and a second one to use as a vacuum) to blow up/bring back an air cushion, via Crickit and the Circuit Playground Bluefruit. All was going well until I tried to code it to activate via the front buttons on the CPB, wherein I realized my delays were an issue. The problem is that I can't remove the 500ms delay from the system (in order to make the pumps active on the button press itself), without the air valve going haywire and switching from off to on rapidly. Is there some way to keep the valve driver on or off by itself? I've included the Arduino code below, I'm super new to this, so sorry if the problems are glaring.

Code: Select all | TOGGLE FULL SIZE
#include <Adafruit_Crickit.h>
#include <seesaw_motor.h>
#include <Adafruit_CircuitPlayground.h>

Adafruit_Crickit crickit;

seesaw_Motor motor_a(&crickit);
seesaw_Motor motor_b(&crickit);

bool leftButtonPressed;
bool rightButtonPressed;

void setup() {
  Serial.begin(9600);
  CircuitPlayground.begin();
   if(!crickit.begin()){
    Serial.println("ERROR!");
    while(1) delay(1);
  }
  else Serial.println("Crickit started");
  crickit.setPWMFreq(CRICKIT_DRIVE1, 1000);


  if(!crickit.begin()){
    Serial.println("ERROR!");
    while(1) delay(1);
  }
  else Serial.println("Crickit started");
   motor_a.attach(CRICKIT_MOTOR_A1, CRICKIT_MOTOR_A2);
   motor_b.attach(CRICKIT_MOTOR_B1, CRICKIT_MOTOR_B2);
}

void loop() {
  leftButtonPressed = CircuitPlayground.leftButton();
  rightButtonPressed = CircuitPlayground.rightButton();

  if (leftButtonPressed) {
     motor_a.throttle(1);
  motor_b.throttle(0);
  crickit.analogWrite(CRICKIT_DRIVE1, CRICKIT_DUTY_CYCLE_MAX);


  } else {
     motor_a.throttle(0);
  motor_b.throttle(0);
  crickit.analogWrite(CRICKIT_DRIVE1, CRICKIT_DUTY_CYCLE_OFF);


  }
  if (rightButtonPressed) {
     motor_a.throttle(0);
  motor_b.throttle(-1);
  crickit.analogWrite(CRICKIT_DRIVE1, CRICKIT_DUTY_CYCLE_OFF);
 

  } else {
    motor_a.throttle(0);
  motor_b.throttle(0);
  crickit.analogWrite(CRICKIT_DRIVE1, CRICKIT_DUTY_CYCLE_MAX);

  }

}

Message #general


Thanks!

omnistraight
 
Posts: 6
Joined: Wed May 11, 2022 10:33 pm

Re: Air Valve Frenzy (Crickit + CPB + Arduino)

by neradoc on Thu May 19, 2022 4:24 am

Hi, it looks like you need edge detection on your button.
The simplest way is to have a state variable to remember if the button was pressed.
That way you only record a press once per click. A small delay of a few milliseconds will do debouncing if necessary.

Look at the code in this tutorial: https://www.arduino.cc/en/Tutorial/Buil ... eDetection
You would use CircuitPlayground.leftButton() instead of digitalRead(buttonPin) for the left button for example.

neradoc
 
Posts: 186
Joined: Wed Apr 27, 2016 2:38 pm

Please be positive and constructive with your questions and comments.