I’m trying to turn a vibration motor disc on and off using buttons. I can upload a video if you need to see it. But when I upload the program to the samd 21 board, instead of doing what I want when I press the buttons. It exhibits a bunch of odd behaviors. As a way to see it, I’ve been using an LED instead of the VMD, and with just the program uploaded and nothing pressed. The light keeps flickering on an off. It is using a PWM wave and AnalogOutput. I’ve been using pin 9 as the output.
If you can provide any help, I would really appreciate it. I am pretty much done my project. I’m just miniaturizing it before sending it to my customer, and this has held me up nearly 2 months dealing with this issue.
Any info you need to help me, let me know and I can provide it.
A schematic diagram and a copy of your code would be helpful in determining what the issue might be. When you post your code, please the ‘code display’ button in the button bar above your post so that it’s easier to read.
Chris, I’ve added a picture of the schematic below and the code. There is a 220 Ohm resistor for the LED and a 10k Ohm resistor for each of the buttons. There’s actually 4 buttons and they are wired the same way to separate pins. Something else that might be important is how I am powering it. I am using https://www.adafruit.com/product/1944 as an intermediary board between my battery and the board so I can recharge and such. That would be wired to the Vin and GND.
//Specs:
// - Button (1) is a small impulse,
// - Button (2) is a small continuous vibration,
// - Button (3) is a big impulse and
// - Button (4) a big continuous vibration.
// 0 = stopped / 1 = small impulse / 2 = small continuous
// 3 = big impulse / 4 = big continuous
int stateLED = 0;
int ledHIGH = 255;
int ledLOW = 155;
// Variable to allow buttons to turn on and off
int stateButtonOne;
int stateButtonTwo;
int stateButtonThree;
int stateButtonFour;
long time = 0;
long debounce = 200;
long startImpulseTime = 0;
long impulseDuration = 2000; // 2 seconds
// Pin definition
int LED = 9;
int pinButtonOne = 3;
int pinButtonTwo = 4; //no 4???
int pinButtonThree = 5;
int pinButtonFour = 6;
//------------------- useful functions -------------------------
// Return the strength of the motor/LED (weak/strong)
int getLEDstrength()
{
if((stateLED == 1) || (stateLED == 2))
return ledLOW; //small -> to change with the motor
else if((stateLED == 3) || (stateLED == 4))
return ledHIGH; //strong
else
return LOW;
}
//------------------- useful functions -------------------------
//Returns true if we are in an impulse state
bool isImpulse()
{
return ((stateLED == 1) || (stateLED == 3));
}
//------------------- main functions -------------------------
void setup()
{
// INPUTS
pinMode (pinButtonOne,INPUT);
pinMode (pinButtonTwo,INPUT);
pinMode (pinButtonThree,INPUT);
pinMode (pinButtonFour,INPUT);
// OUTPUTS
pinMode (LED,OUTPUT);
Serial.begin(9600);
}
//---------------------------------------------------------
void loop()
{
long millisVar = millis(); //do it once for all
//not enough time since last call
if(millisVar - time <= debounce)
{
return;
}
else
{
time = millisVar; //do it only once
}
//End of previous impulse?
if(isImpulse() && (millisVar - startImpulseTime > impulseDuration))
{
//turn off the LED
stateLED = 0;
startImpulseTime = 0;
digitalWrite(LED, 0);
return;
}
// READ INPUTS
stateButtonOne = digitalRead(pinButtonOne);
stateButtonTwo = digitalRead(pinButtonTwo);
stateButtonThree = digitalRead(pinButtonThree);
stateButtonFour = digitalRead(pinButtonFour);
//BUTTONS
if(stateButtonOne == HIGH)
{
stateLED = 1;
startImpulseTime = millisVar;
}
else if(stateButtonTwo == HIGH)
{
//switch on/off mode
if(stateLED != 2)
stateLED = 2;
else
stateLED = 0;
}
else if(stateButtonThree == HIGH)
{
stateLED = 3;
startImpulseTime = millisVar;
}
else if(stateButtonFour == HIGH)
{
//switch on/off mode
if(stateLED != 4)
stateLED = 4;
else
stateLED = 0;
}
// OUTPUTS
// does the LED have different states (weak/strong)?
digitalWrite(LED, getLEDstrength());
}
//---------------------------------------------------------
That button is an SPST button with two pins tied together on each end. You treat it as a 2-pin device (I’d tie one side to ground, the other to the SAMD21 with a pullup resistor).
I can’t help debug your code, but it looks like the code is looking for the button pins to be pulled low by default and brought high when you press a button. If you’re pulling them high, I think your code is thinking they are always active. Have you tried a resistor to ground on all of the button pins and then have the buttons connect the button pins 3.3V?