I have an int array of melodies. If i press the button it plays the whole song but if i put a break after the delay, than it just reset the i. How do i make it so that only after another button-press it continues? (i'm still a newbie at this sorry and thanks in advance)
int buttonPin = 12;
void setup()
{
// put your setup code here, to run once:
pinMode(buttonPin, INPUT);
}
void loop()
{
// put your main code here, to run repeatedly:
int buttonState = digitalRead(buttonPin);
for(int i = 0; i < sizeof(mariomelody); i++)
{
if(buttonState == HIGH)
{
tone(8, mariomelody[i], 70);
delay();
}
}
}
Stop the loop while the button press is still held in:
int buttonPin = 12;
void setup()
{
// put your setup code here, to run once:
pinMode(buttonPin, INPUT);
}
void loop()
{
// put your main code here, to run repeatedly:
int buttonState = digitalRead(buttonPin);
for(int i = 0; i < sizeof(mariomelody); i++)
{
if(buttonState == HIGH)
{
tone(8, mariomelody[i], 70);
delay();
}
while(digitalRead(buttonPin) == HIGH)
{
// wait until the button is released
}
while(digitalRead(buttonPin) == LOW)
{
//wait until the button is pressed again
}
}
}
I'm guessing that you want to play the melody while the button is pressed, and stop if the button is released.
Then it would be something like:
int i = 0;
void loop()
{
if(digitalRead(buttonPin) == HIGH)
{
tone(8, mariomelody[i], 70);
i = (i + 1) % sizeof(mariomelody);
delay();
}
}
To avoid resetting the position to the begin of the melody you need i to be a global variable.
If you want the button to switch on and off the melody you'll need another global variable playing:
bool playing = false;
void loop()
{
if(digitalRead(buttonPin) == HIGH)
{
playing = !playing;
while (digitalRead(buttonPin) == HIGH)
; //wait for release
}
if (playing) {
//the rest is the same
}
}
Related
#define CLK 2
#define DT 3
#define SW 4
#include <EEPROM.h>
#include <Wire.h>
#include <LiquidCrystal_PCF8574.h>
LiquidCrystal_PCF8574 lcd(0x27);
int counter = 0;
int currentStateCLK;
int lastStateCLK;
int lastCLK,cnt=0,btnState,lastPress=0;
String currentDir ="";
unsigned long lastButtonPress = 0;
char *mainmenu[] ={"SET MODE","SET TEMP","SET HUMD","SERVO","RESET"};
char *setmode[] ={"INCUBATOR","HATCHER","BACK"};
void setup() {
// Set encoder pins as inputs
Wire.begin();
Wire.beginTransmission(0x27);
pinMode(CLK,INPUT);
pinMode(DT,INPUT);
pinMode(SW, INPUT_PULLUP);
lcd.begin(16, 2);
lcd.setBacklight(255);
lcd.home(); lcd.clear();
Serial.begin(9600);
lastStateCLK = digitalRead(CLK);
delay(100);
if(EEPROM_READ(0)==NULL){
SET_MODE();
}
Serial.print(EEPROM_READ(0));
}
void loop(){
disp();
rot();
}
void disp(){
lcd.setCursor(0,0);
lcd.print(" KGF");
}
void rot() {
int lim=sizeof(mainmenu)/2;
Serial.print(lim);
currentStateCLK = digitalRead(CLK);
if (currentStateCLK != lastStateCLK && currentStateCLK == 1){
lcd.clear();
lcd.setCursor(0, 1);
if (digitalRead(DT) != currentStateCLK) {
counter --;
if(counter<0){
counter=lim-1;
}
}
else {
// Encoder is rotating CW so increment
counter ++;
if(counter>lim-1){
counter=0;
}
lcd.print(mainmenu[counter]);
}
lastStateCLK = currentStateCLK;
int btnState = digitalRead(SW);
if (btnState == LOW) {
//if 50ms have passed since last LOW pulse, it means that the
//button has been pressed, released and pressed again
if (millis() - lastButtonPress > 50) {
if(counter==0){
SET_MODE();
}
}
}
lastButtonPress = millis();
}
delay(1);
}
void SET_MODE(){
int lim=sizeof(setmode)/2;
int currentCLK = digitalRead(CLK);
if (currentCLK != lastCLK && currentCLK == 1){
lcd.clear();
lcd.setCursor(0, 1);
if (digitalRead(DT) != currentCLK) {
cnt --;
if(cnt<0){
cnt=lim-1;
}
}
else {
// Encoder is rotating CW so increment
cnt ++;
if(cnt>lim-1){
cnt=0;
}
}
lcd.print(setmode[cnt]);
}
lastCLK = currentCLK;
btnState = digitalRead(SW);
if (btnState == LOW) {
//if 50ms have passed since last LOW pulse, it means that the
//button has been pressed, released and pressed again
if (millis() - lastButtonPress > 50) {
if(setmode[cnt]=="BACK"){
exit(0);
}
lcd.clear();
lcd.setCursor(0, 1);
EEPROM_WRITE(0,setmode[cnt]);
lcd.print("DONE");
}
lastPress = millis();
}
delay(1);
}
void EEPROM_WRITE(int addrOffset, const String &strToWrite)
{
byte len = strToWrite.length();
EEPROM.write(addrOffset, len);
for (int i = 0; i < len; i++)
{
EEPROM.write(addrOffset + 1 + i, strToWrite[i]);
}
}
String EEPROM_READ(int addrOffset)
{
int newStrLen = EEPROM.read(addrOffset);
char data[newStrLen + 1];
for (int i = 0; i < newStrLen; i++)
{
data[i] = EEPROM.read(addrOffset + 1 + i);
}
data[newStrLen] = '\0';
return String(data);
}
I want to call the SET_MODE() function in the loop from rot() function, I am building a menu based program so the SET MODE menu should redirect to the SET_MODE() function, and as I will be adding more menu and sub-menus how can I perform this task.
The SET_MODE() function doesn't work in loop I do not know why, it only works when I all it under void loop() directly.
I need make a traffic light that works on countdown:
when number on timer == 0 or if button pressed green led should turn on
otherwise red led
There is my loop,
I tried without for loop and button worked but if I add for loop (for timer) button not responding
thank you
crosswalk_button = digitalRead(2); //That will read the state of the button, if it's pressed or not
for (int i = numberfor7digit; i >= 0; i--) { //numberfor7digit is = 9
numbers(i); //numbers is a function i wrote which shows int it takes currently i
delay(1000);
if (crosswalk_button == 0) { //If you press the button for the crosswalk on with the green one for the crosswalk
numbers(0);
greenhigh(); // green high is a function too, which turns on green light
}
// when number in 7 segment is 0 it will turn on green
else if (i == 0) {
numbers(0);
greenhigh();
}
// for any other number it turns on red
else {
digitalWrite(RED_LED, HIGH);
digitalWrite(GREEN_LED, LOW);
}
}
}
Few things which will help you to design this system effectively are:
Use Interrupts to read the value of the button
This will make the turning on Green Light irrespective of what's been executed by the Arduino when the button is pressed.
Example: Illustration to show how to configure a button for interrupt
int interruptPin = 2; //button attached to this pin
void setup() {
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), greenhigh, LOW);
}
void loop() {
//Your code inside this loop
}
void greenhigh() {
//Your code for turning green light high
numbers(0);
digitalWrite(GREEN_LED, HIGH);
digitalWrite(RED_LED, LOW);
}
Seven Segment Display
Now, since the button is fixed writing code for seven segment display will be simpler.
Example: Illustration to show SSD working
digitalWrite(RED_LED, HIGH);
for (int i = numberfor7digit; i >= 0; i--) {
numbers(i);
delay(1000);
}
greenhigh();
delay(5000);
Conclusion
Complete code can be written as:
int interruptPin = 2; //button attached to this pin
int numberfor7digit = 9;
int GREEN_LED = 10; //TODO: Input here your actual green led
int RED_LED = 11; //TODO: Input here your actual red led
void setup() {
pinMode(interruptPin, INPUT_PULLUP);
pinMode(GREEN_LED, OUTPUT);
pinMode(RED_LED, OUTPUT);
attachInterrupt(digitalPinToInterrupt(interruptPin), greenhigh, LOW);
}
void greenhigh() {
//TODO: Your code for turning green light high
numbers(0);
digitalWrite(GREEN_LED, HIGH);
digitalWrite(RED_LED, LOW);
}
void numbers(int n) {
//Your implementaion of numbers()
}
void loop() {
digitalWrite(RED_LED, HIGH);
for (int i = numberfor7digit; i >= 0; i--) {
numbers(i);
delay(1000);
}
greenhigh();
delay(5000);
}
You have to read the status of your button every time the loop runs i.e. the for loop that you are using.
And I have also removed the delay and replaced it with an if statement similar to blink without delay example.
One more thing, there is no need to create a crosswalk_button variable
You can directly replace it with digitalRead(2)
Use this code, it should work.
void loop() {
for(int i = numberfor7digit; ; i >= 0; i--) {
static long previousMillis = 0;
if(millis() - previousMillis >= 1000) {
numbers(i);
previousMillis = millis();
}
crosswalk_button = digitalRead(2);
if(crosswalk_button == false || i == 0) {
i = 0;
numbers(i);
greenhigh();
} else {
digitalWrite(RED_LED, HIGH);
digitalWrite(GREEN_LED, LOW):
}
}
}
I'm writing an Arduino program to control a minigun for a friend's cosplay. I need to write a program that when the button is pressed the motor ramps up from 0 to a given value (for now lets say "analogWrite(output_pin,200);") then loop at that RPM until the button is released, at which point it needs to ramp down back to zero.
When I try to put ramping into a loop it doesn't finish the code. I
I need something like this in c++ code for the Arduino button. (I've tried similar things using the "delay" function to no avail)
motorspeed = 0
if buttonpress == True:
buttonheld = True
for i in range (0,10):
delay(1)
motorspeed =+ 20
if buttonpress == False:
motorspeed = 0
if buttonheld == True:
motorspeed = 200
if buttonpress == False:
for i in range(0,10):
delay(1)
motorspeed =- 20
else:
#shut off motor
#Play error sound
Here is the current code that only runs the motor at one speed when the button is held down.
const int button1 =4;
int BUTTONstate1 = 0;
void setup()
{
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(9, OUTPUT);
pinMode(button1, INPUT);
}
void loop()
{
//set button to read
BUTTONstate1 = digitalRead(button1);
//if button is pressed
if (BUTTONstate1 == HIGH)
{
//run current through motor
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
//set speed
//Rampup
analogWrite(9,200);
}
else
{
digitalWrite(2, LOW);
digitalWrite(3, LOW);
}
}
Here is a virtual environment for the circuit
https://www.tinkercad.com/things/cLOBc9JJuTz-copy-of-dayloncircuitrefinecode/editel?sharecode=b6cqTLlNqUCCN09-mQ_zykp5sMnXx6KLt_KNqlXJmcs
I would recommend using interrupts, it's pretty well documented in Arduino reference:
Arduino reference - Interrupts
Most helpful is "Parameters" and "Example code" section. I think you should prepare two methods and attach interrupt to your input (button) pin on RISING and FALLING trigger.
Make sure that the button pin can be used for interrupt (on Arduino UNO / NANO only pin 2 and 3).
I think it should look similar to this (I haven't tested that):
#define buttonPin 2; // pin usable for interrupt
#define outputPin 9;
#define powerPinA 3;
#define powerPinB 4;
bool running = false;
int currentSpeed = 0;
void buttonUp()
{
digitalWrite(powerPinA, LOW);
digitalWrite(powerPinB, LOW);
running = false;
}
void buttonDown()
{
digitalWrite(powerPinA, LOW);
digitalWrite(powerPinB, HIGH);
running = true;
}
void setup()
{
pinMode(outputPin, OUTPUT);
pinMode(powerPinA, OUTPUT);
pinMode(powerPinB, OUTPUT);
pinMode(buttonPin, INPUT);
attachInterrupt(digitalPinToInterrupt(interruptPin), buttonUp, RAISING);
attachInterrupt(digitalPinToInterrupt(interruptPin), buttonDown, FALLING);
}
void loop()
{
delay(1);
if (running && currentSpeed < 200)
currentSpeed += 20;
else if (!running && currentSpeed > 0)
currentSpeed -= 20;
analogWrite(outputPin, currentSpeed);
}
Here's an example that doesn't rely on any interrupts. Just reading the button and remembering the state in a variable and remembering the time of the last step in a variable to check against millis() (Compiled but Untested)
int buttonPin = 5;
int lastButtonState = HIGH;
unsigned long lastRampStep;
unsigned int stepTime = 10; // 10ms steps. Set to whatever you want.
int analogPin = 3;
int analogLevel = 0;
int maxLevel = 200;
int analogStepSize = 10;
void setup() {
pinMode(buttonPin, INPUT_PULLUP); // will read LOW when pressed
}
void loop() {
int buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState) {
// The button just changed!
if (buttonState == LOW) {
// was just now pressed
analogLevel = 0; // start ramp up
lastRampStep = millis(); // note the time that we took this step
} else {
// Button just released.
// not sure what you want to happen here
}
}
else if (buttonState == LOW) {
// While button is held ramp until we reach the max level
if(analogLevel < maxLevel){
if(millis() - lastRampStep >= stepTime){
analogLevel += analogStepSize;
lastRampStep = millis();
}
}
} else {
// While button is released (HIGH) ramp back down:
if(analogLevel > 0){
if(millis() - lastRampStep >= stepTime){
analogLevel -= analogStepSize;
lastRampStep = millis();
}
}
}
analogWrite(analogPin, analogLevel);
lastButtonState = buttonState;
}
You could use interrupts to handle pressed/release, set a value, and handle the ramp up/down in the loop.
Keep in mind that you should also debounce the button.
Below is some pseudocode:
const byte maxLevel = 200; // some example values (max 255)
const int rampDelay = 5;
volatile byte targetLevel = 0;
byte currentLevel = 0;
// ...
ISR for Button { // Could be in one ore two ISR's
if pressed
targetLevel = maxLevel;
// possible other calls
if released
targetLevel = 0;
// possible other calls
}
void loop()
{
if (currentLevel < targetLevel) {
SetLevel(++currentLevel);
}
else if (currentLevel > targetLevel) {
SetLevel(--currentLevel);
}
// if currentLevel == targetLevel nothing changes
// if your device is battery powered you could even put the mcu
// to sleep when both levels are zero (and wake up from the ISR)
// no need for constantly polling the button when using ISR
delay(rampDelay);
}
This will also start ramping down immediately if the button is released before the motor is at full speed.
Thank you all for your time.
I Have my code that when I keep holding the button the laser turn on "HIGH"
when I release my finger from the button the laser turns off "LOW"
But I want when I double click the laser turn on and when I double click again the laser turn off.
Thank you and sorry for my bad English.
const int btn = 6;// The push button
int previousButtonStateLAZER = HIGH; // for btn6
void setup(){
pinMode(btn, INPUT);
pinMode (laserPin, OUTPUT);
}
void loop(){
int buttonStateLAZER = digitalRead(btn);
// if the button state has changed,
if (buttonStateLAZER != previousButtonStateLAZER){
if( buttonStateLAZER == HIGH ) {
digitalWrite (laserPin, HIGH);
}else{
digitalWrite (laserPin, LOW);
}
}
previousButtonStateLAZER = buttonStateLAZER;
}```
You need to store the last two times that a click was detected and then test to see if they occurred close enough together. Try this code out (edited from what you posted):
const int btn = 6;// The push button
int previousButtonStateLAZER = LOW; // for btn6
static long releaseTimes[2];
static int releaseIndex = 1;
static int laserStates[2];
static int laserIndex = 0;
void setup(){
pinMode(btn, INPUT);
pinMode (laserPin, OUTPUT);
releaseTimes[0] = 0;
releaseTimes[1] = 0;
laserStates[0] = LOW;
laserStates[1] = HIGH;
}
void loop(){
int buttonStateLAZER = digitalRead(btn);
// if the button state has changed,
if (buttonStateLAZER != previousButtonStateLAZER){
previousButtonStateLAZER = buttonStateLAZER;
if (buttonStateLAZER == HIGH) {
return;
}
releaseIndex = 1 - releaseIndex;
releaseTimes[releaseIndex] = millis();
if (abs(releaseTimes[0] - releaseTimes[1]) < 800) {
releaseTimes[0] = 0;
releaseTimes[1] = 0;
laserIndex = 1 - laserIndex;
digitalWrite(laserPin, laserStates[laserIndex]);
}
}
}
My question is very simple but I'm stuck on it for a while now.
I need to make a script that when pushed on a button loop a() is put on pause. And when you press it again it should go from where it ended. But I can't figure out a way to do it.
I hope someone can help me.
This is my code:
int Aan = 1;
int Uit = 0;
int analogPin = A3;
int LED1 = 13;
int LED2 = 12;
int LED3 = 11;
int LED4 = 10;
int val;
bool r = false;
void setup() {
pinMode(analogPin, INPUT_PULLUP);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
digitalWrite(LED1, Aan);
digitalWrite(LED2, Aan);
digitalWrite(LED3, Aan);
digitalWrite(LED4, Aan);
}
void loop() {
val = digitalRead(analogPin);
if (val == LOW)
{
if (r == true)
{
r = false;
}
if (r == false)
{
r = true;
}
}
if (r == true)
{
a();
}
}
void a() {
for (int i = 10; i <= 13; i++)
{
pinMode(i, OUTPUT);
digitalWrite(i, Uit);
delay(100);
digitalWrite(i, Aan);
}
for (int i = 13; i >= 10; i--)
{
pinMode(i, OUTPUT);
digitalWrite(i, Uit);
delay(100);
digitalWrite(i, Aan);
}
}
Just to explain whats happening.. Void a() makes 4 different leds light up and go out. The pattern thats used is Knight Rider (If you don't know the tv show just google the car of him)
I will assume you want the "Knight Rider" pattern to constantly run.
I've made a couple of changes. First, I added a function to run the led sequence one way. Second, I added a while loop that will always run once, and will continue to run while the button is pushed.
bool paused = false;
int buttonState = HIGH;
void loop() {
a();
}
// This only works if leds ports are consecutive
void runSequence(int ledStart, int ledEnd)
{
int direction = ledStart < ledEnd ? 1 : -1;
for (int i = ledStart; i != ledEnd + direction; i += direction) {
digitalWrite(i, Uit);
do {
delay(100);
} while (LOW == digitalRead(analogPin)); // Check button state
digitalWrite(i, Aan);
}
}
void a() {
runSequence(LED4, LED1);
runSequence(LED1, LED4);
}
EDIT Changes based on comment
bool paused = false;
int buttonState = HIGH;
int currentLED = LED1;
int currentDirection = -1;
void loop() {
checkButton();
if (!paused) {
// Flash the led
digitalWrite(currentLED, Uit);
delay(100);
digitalWrite(currentLED, Aan);
// Change direction?
if (LED1 == currentLED || LED4 == currentLED) {
currentDirection *= -1;
}
// Setup for next iteration
currentLED += currentDirection;
}
}
void checkButton() {
int state = digitalRead(analogPin);
// Check if button state has changed
if (state != buttonState) {
buttonState = state;
// Change paused state when button is released
if (state == HIGH) {
paused = !paused;
}
}
}
Inside your a() function put another while loop which becomes false when the button is Up.
Like:
while(digitalRead(analogPin) == LOW)
{
}
As long as the button is pressed down the loop will continue. When you release the button the program will exit the loop and continue execution of your code.