Arduino - How to control piezo with button? - arduino

What I am trying to achieve is a button that acts as an on/off switch for playing a tune on a piezo. Initially I want the piezo not to make any sound until the user presses a button, and then they can turn the piezo off by pressing the same button. I'm wondering if anyone can help me figure out what code I need to add to this to get this kind of button functionality.
This is a modified version of the toneMelody sketch that comes with Arduino IDE, I have a button attached to Pin 12, and a Piezo to Pin 8.
#include "pitches.h"
// notes in the melody:
int melody[] ={
NE5,NF5,NG5,ND6,0,NC6,NC6,NB5,NG5,NF5,0,NF5,NF5,NE5,NC5,NF5,NE5,ND5,NC5,NE5,ND5,0,NE5,NF5,NG5,ND6,0,NC6,NC6,NB5,NG5,NF5,0,NF5,NF5,NE5,NC5,NF5,NE5,ND5,NC5,NE5,ND5,0,
0,NC5,NE5,NG5,NG5,NE5,ND5,NE5,ND5,0,NC5,NE5,NG5,NG5,NE5,ND5,0,NE5,0,NC5,NE5,NG5,NG5,NE5,ND5,NE5,ND5,0,NC5,NE5,NG5,NG5,NA5,NE5,0,NE5,
0,NC5,NE5,NG5,NG5,NE5,ND5,NE5,ND5,0,NC5,NE5,NG5,NG5,NE5,ND5,0,NE5,0,NC5,NE5,NG5,NG5,NE5,ND5,NE5,ND5,NF5,0,NE5,0,NC5,NF5,0,NE5,0,NC5,
NE5,NE5,NE5,0,NE5,0,ND5,NE5,NE5,NE5,0,NE5,0,ND5,NE5,NE5,NE5,0,NE5,0,ND5,ND5,NB4,NB4,0,NC5,0,NG5,
NE5,NE5,NE5,0,NE5,0,ND5,NE5,NE5,NE5,0,NE5,0,ND5,NE5,NE5,NE5,0,NE5,0,ND5,ND5,
NE5,NF5,NG5,ND6,0,NC6,NC6,NB5,NG5,NF5,0,NF5,NF5,NE5,NC5,NF5,NE5,ND5,NC5,NE5,ND5,0,NE5,NF5,NG5,ND6,0,NC6,NC6,NB5,NG5,NF5,0,NF5,NF5,NE5,NC5,NF5,NE5,ND5,NC5,NE5,ND5,0,
NE5,NF5,NE5,ND5,ND5,NC5,NC5,NC5,ND5,ND5,NE5,0,NE5,NF5,NE5,ND5,ND5,NC5,NC5,NC5,NG5,ND5,0,
NE5,NF5,NE5,ND5,ND5,NC5
};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
8,8,8,4,8,4,8,8,8,4,8,4,8,8,8,4,4,8,8,8,2,8,8,8,8,4,8,4,8,8,8,4,8,4,8,8,8,4,4,8,8,8,2,8,
8,8,8,8,8,16,8,16,8,8,8,8,8,8,16,8,16,8,8,8,8,8,8,16,8,16,8,8,8,8,8,8,16,8,16,8,
8,8,8,8,8,16,8,16,8,8,8,8,8,8,16,8,16,8,8,8,8,8,8,16,8,16,8,8,16,8,16,8,8,16,8,16,8,
4,4,8,16,8,16,8,4,4,8,16,8,16,8,4,4,8,16,8,16,8,4,4,8,16,8,16,8,
4,4,8,16,8,16,8,4,4,8,16,8,16,8,4,4,8,16,8,16,8,2,
8,8,8,4,8,4,8,8,8,4,8,4,8,8,8,4,4,8,8,8,2,8,8,8,8,4,8,4,8,8,8,4,8,4,8,8,8,4,4,8,8,8,2,8,
8,8,8,8,4,4,8,8,8,4,2,8,8,8,8,8,4,4,8,8,8,2,8,
8,8,8,8,4,1
};
void setup() {
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 240; thisNote++) {
// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000/noteDurations[thisNote];
tone(8, melody[thisNote],noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(8);
}
}
void loop() {
// no need to repeat the melody.
}

The way I would use a button would be like this. I added comments above and below were I added stuff. The out come of this will be that when you press the button the whole song will play then stop until you press the button again. You can see what kind of button I am taking about here http://arduino.cc/en/Tutorial/Button it also gives you some example code, for you to look at just incase I messed up, if I did mess up
just message me and I will show you how to fix.
#include "pitches.h"
// notes in the melody:
int melody[] ={
NE5,NF5,NG5,ND6,0,NC6,NC6,NB5,NG5,NF5,0,NF5,NF5,NE5,NC5,NF5,NE5,ND5,NC5,NE5,ND5,0,NE5,NF5,
NG5,ND6,0,NC6,NC6,NB5,NG5,NF5,0,NF5,NF5,NE5,NC5,NF5,NE5,ND5,NC5,NE5,ND5,0,
0,NC5,NE5,NG5,NG5,NE5,ND5,NE5,ND5,0,NC5,NE5,NG5,NG5,NE5,ND5,0,NE5,0,NC5,NE5,NG5,NG5,NE5,ND
5,NE5,ND5,0,NC5,NE5,NG5,NG5,NA5,NE5,0,NE5,
0,NC5,NE5,NG5,NG5,NE5,ND5,NE5,ND5,0,NC5,NE5,NG5,NG5,NE5,ND5,0,NE5,0,NC5,NE5,NG5,NG5,NE5,ND
5,NE5,ND5,NF5,0,NE5,0,NC5,NF5,0,NE5,0,NC5,
NE5,NE5,NE5,0,NE5,0,ND5,NE5,NE5,NE5,0,NE5,0,ND5,NE5,NE5,NE5,0,NE5,0,ND5,ND5,NB4,NB4,0,NC5,
0,NG5,
NE5,NE5,NE5,0,NE5,0,ND5,NE5,NE5,NE5,0,NE5,0,ND5,NE5,NE5,NE5,0,NE5,0,ND5,ND5,
NE5,NF5,NG5,ND6,0,NC6,NC6,NB5,NG5,NF5,0,NF5,NF5,NE5,NC5,NF5,NE5,ND5,NC5,NE5,ND5,0,NE5,NF5,
NG5,ND6,0,NC6,NC6,NB5,NG5,NF5,0,NF5,NF5,NE5,NC5,NF5,NE5,ND5,NC5,NE5,ND5,0,
NE5,NF5,NE5,ND5,ND5,NC5,NC5,NC5,ND5,ND5,NE5,0,NE5,NF5,NE5,ND5,ND5,NC5,NC5,NC5,NG5,ND5,0,
NE5,NF5,NE5,ND5,ND5,NC5
};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
8,8,8,4,8,4,8,8,8,4,8,4,8,8,8,4,4,8,8,8,2,8,8,8,8,4,8,4,8,8,8,4,8,4,8,8,8,4,4,8,8,8,2,8,
8,8,8,8,8,16,8,16,8,8,8,8,8,8,16,8,16,8,8,8,8,8,8,16,8,16,8,8,8,8,8,8,16,8,16,8,
8,8,8,8,8,16,8,16,8,8,8,8,8,8,16,8,16,8,8,8,8,8,8,16,8,16,8,8,16,8,16,8,8,16,8,16,8,
4,4,8,16,8,16,8,4,4,8,16,8,16,8,4,4,8,16,8,16,8,4,4,8,16,8,16,8,
4,4,8,16,8,16,8,4,4,8,16,8,16,8,4,4,8,16,8,16,8,2,
8,8,8,4,8,4,8,8,8,4,8,4,8,8,8,4,4,8,8,8,2,8,8,8,8,4,8,4,8,8,8,4,8,4,8,8,8,4,4,8,8,8,2,8,
8,8,8,8,4,4,8,8,8,4,2,8,8,8,8,8,4,4,8,8,8,2,8,
8,8,8,8,4,1
};
//int to identify what digital pin the button is hooked up to
// can Chang 2 if put button on a different pin
int buttonPin = 2;
void setup() {
//says button pin is a input
pinMode(buttonPin, INPUT);
// I might say make a int = to the pin you have
// your sound maker on, name it soundMakerPin, and the uncomment the line below
// pinMode(soundMakerPin, OUTPUT);
}
void loop(){
// read the button
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// play song
song();
}
}
//function named song, call this and your song should play
void song(){
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 240; thisNote++) {
// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000/noteDurations[thisNote];
tone(8, melody[thisNote],noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(8);
}
}

Related

What is rosidl_runtime_c__double__Sequence type?

I'm trying to use a teensy 4.1 as an interface between an encoder and ROS thanks to micro-ros (arduino version).
I would like to publish position of a wheel to the /jointState topic with the teensy but there is no example on the micro-ros arduino Github repo.
I've tried to inspect the sensormsgs/msg/jointState message struct but everything is a bit fuzzy and I don't understand how to make it works. I can't understand what is rosidl_runtime_c__double__Sequence type.
I've tried several things but I always get an error about operand types
no match for 'operator=' (operand types are 'rosidl_runtime_c__String' and 'const char [18]')
msg.name.data[0] = "drivewhl_1g_joint";
Here is my arduino code
#include <micro_ros_arduino.h>
#include <stdio.h>
#include <rcl/rcl.h>
#include <rcl/error_handling.h>
#include <rclc/rclc.h>
#include <rclc/executor.h>
#include <sensor_msgs/msg/joint_state.h>
rcl_publisher_t publisher;
sensor_msgs__msg__JointState msg;
rclc_executor_t executor;
rclc_support_t support;
rcl_allocator_t allocator;
rcl_node_t node;
rcl_timer_t timer;
#define LED_PIN 13
#define RCCHECK(fn) { rcl_ret_t temp_rc = fn; if((temp_rc != RCL_RET_OK)){error_loop();}}
#define RCSOFTCHECK(fn) { rcl_ret_t temp_rc = fn; if((temp_rc != RCL_RET_OK)){}}
void error_loop(){
while(1){
digitalWrite(LED_PIN, !digitalRead(LED_PIN));
delay(100);
}
}
void timer_callback(rcl_timer_t * timer, int64_t last_call_time)
{
RCLC_UNUSED(last_call_time);
if (timer != NULL) {
RCSOFTCHECK(rcl_publish(&publisher, &msg, NULL));
//
//Do not work
//msg.name=["drivewhl_1g_joint","drivewhl_1d_joint","drivewhl_2g_joint","drivewhl_2d_joint"];
//msg.position=["1.3","0.2", "0","0"];
msg.name.size = 1;
msg.name.data[0] = "drivewhl_1g_joint";
msg.position.size = 1;
msg.position.data[0] = 1.85;
}
}
void setup() {
set_microros_transports();
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, HIGH);
delay(2000);
allocator = rcl_get_default_allocator();
//create init_options
RCCHECK(rclc_support_init(&support, 0, NULL, &allocator));
// create node
RCCHECK(rclc_node_init_default(&node, "micro_ros_arduino_node", "", &support));
// create publisher
RCCHECK(rclc_publisher_init_default(
&publisher,
&node,
ROSIDL_GET_MSG_TYPE_SUPPORT(sensor_msgs, msg, JointState),
"JointState"));
// create timer,
const unsigned int timer_timeout = 1000;
RCCHECK(rclc_timer_init_default(
&timer,
&support,
RCL_MS_TO_NS(timer_timeout),
timer_callback));
// create executor
RCCHECK(rclc_executor_init(&executor, &support.context, 1, &allocator));
RCCHECK(rclc_executor_add_timer(&executor, &timer));
}
void loop() {
delay(100);
RCSOFTCHECK(rclc_executor_spin_some(&executor, RCL_MS_TO_NS(100)));
}
I'm a beginner with Ros and C, it may be a very dumb question but I don't know how to solve it. Thanks for your help !
rosidl_runtime_c__String__Sequence is a structure used to old string data that is to be transmitted. Specifically it is a sequence of rosidl_runtime_c__String data. You're running into an error because rosidl_runtime_c__String is also a struct itself with no custom operators defined. Thus, your assignment fails since the types are not directly convertible. What you need to do instead is use the rosidl_runtime_c__String.data field. You can see slightly more info here
void timer_callback(rcl_timer_t * timer, int64_t last_call_time)
{
RCLC_UNUSED(last_call_time);
if (timer != NULL) {
//msg.name=["drivewhl_1g_joint","drivewhl_1d_joint","drivewhl_2g_joint","drivewhl_2d_joint"];
//msg.position=["1.3","0.2", "0","0"];
msg.name.size = 1;
msg.name.data[0].data = "drivewhl_1g_joint";
msg.name.data[0].size = 17; //Size in bytes excluding null terminator
msg.position.size = 1;
msg.position.data[0] = 1.85;
RCSOFTCHECK(rcl_publish(&publisher, &msg, NULL));
}
}
I also spent quite some time trying to get publishing JointState message from my esp32 running microros, and also couldn't find working example. Finally, i was successful, maybe it will help someone.
In simple words:
.capacity contains max number of elements
.size contains actual number of elements (strlen in case of string)
.data should be allocated as using malloc as .capacity * sizeof()
each string within sequence should be allocated separately
This is my code that allocates memory for 12 joints, named j0-j11. Good luck!
...
// Declarations
rcl_publisher_t pub_joint;
sensor_msgs__msg__JointState joint_state_msg;
...
// Create publisher
RCCHECK(rclc_publisher_init_default(&pub_joint, &node,
ROSIDL_GET_MSG_TYPE_SUPPORT(sensor_msgs, msg, JointState),
"/hexapod/joint_state"));
//Allocate memory
joint_state_msg.name.capacity = 12;
joint_state_msg.name.size = 12;
joint_state_msg.name.data = (std_msgs__msg__String*) malloc(joint_state_msg.name.capacity*sizeof(std_msgs__msg__String));
for(int i=0;i<12;i++) {
joint_state_msg.name.data[i].data = malloc(5);
joint_state_msg.name.data[i].capacity = 5;
sprintf(joint_state_msg.name.data[i].data,"j%d",i);
joint_state_msg.name.data[i].size = strlen(joint_state_msg.name.data[i].data);
}
joint_state_msg.position.size=12;
joint_state_msg.position.capacity=12;
joint_state_msg.position.data = malloc(joint_state_msg.position.capacity*sizeof(double));
joint_state_msg.velocity.size=12;
joint_state_msg.velocity.capacity=12;
joint_state_msg.velocity.data = malloc(joint_state_msg.velocity.capacity*sizeof(double));
joint_state_msg.effort.size=12;
joint_state_msg.effort.capacity=12;
joint_state_msg.effort.data = malloc(joint_state_msg.effort.capacity*sizeof(double));
for(int i=0;i<12;i++) {
joint_state_msg.position.data[i]=0.0;
joint_state_msg.velocity.data[i]=0.0;
joint_state_msg.effort.data[i]=0.0;
}
....
//Publish
RCSOFTCHECK(rcl_publish(&pub_joint, &joint_state_msg, NULL));

Button counter with interrupt

I want to increment a variable every second when an interrupt is triggered.
Code on esp32, esp-idf.
I have connected a button, when the button is pressed I want to count the number of seconds.
I did this using polling function, but I want to learn how to do it with interrupt, so counting and polling only when the button is pressed and not checking every second for a button pushed.
#include <stdio.h>
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#define ESP_INTR_FLAG_DEFAULT 0
#define BLINK_LED 13
#define GPIO_INPUT_IO_0 33
int buttonCount = 0;
int i = 0;
SemaphoreHandle_t xSemaphore = NULL;
TaskHandle_t printVariableTask = NULL;
void printVariable(void *pvParameter) {
int a = (int) pvParameter;
while (1) {
printf("A is a: %d \n", a++);
vTaskDelay(1000 / portTICK_RATE_MS);
}
}
// interrupt service routine, called when the button is pressed
void IRAM_ATTR button_isr_handler(void* arg) {
// notify the button task
xSemaphoreGiveFromISR(xSemaphore, NULL);
}
// task that will react to button clicks
void button_task(void* arg) {
// infinite loop
for(;;) {
// wait for the notification from the ISR
if(xSemaphoreTake(xSemaphore,portMAX_DELAY) == pdTRUE) {
int buttonState = gpio_get_level(GPIO_INPUT_IO_0);
while(buttonState == 1){ //code stucks here!!!!
buttonCount++;
printf("GPIO_INPUT_IO_0 %d\n", buttonState);
printf("Button pressed! %d \n", i++);
gpio_set_level(BLINK_LED, buttonState);
vTaskDelay(1000 / portTICK_RATE_MS);
}
}
}
}
void app_main()
{
// create the binary semaphore
xSemaphore = xSemaphoreCreateBinary();
// configure button and led pins as GPIO pins
gpio_pad_select_gpio(GPIO_INPUT_IO_0);
gpio_pad_select_gpio(BLINK_LED);
// set the correct direction
gpio_set_direction(GPIO_INPUT_IO_0, GPIO_MODE_INPUT);
gpio_set_direction(BLINK_LED, GPIO_MODE_OUTPUT);
// enable interrupt on falling (1->0) edge for button pin
gpio_set_intr_type(GPIO_INPUT_IO_0, GPIO_INTR_POSEDGE);
// start the task that will handle the button
xTaskCreate(button_task, "button_task", 2048, NULL, 10, NULL);
// install ISR service with default configuration
gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT);
// attach the interrupt service routine
gpio_isr_handler_add(GPIO_INPUT_IO_0, button_isr_handler, NULL);
int pass = 25;
xTaskCreate(&printVariable, "printVariable", 2048, (void*) pass, 5, &printVariableTask);
}
It works, but when the code enter in the while(buttonState == 1) the loop never ends.
What am I doing wrong?
not sure if this is still and issue for you but try making a new global variable as a flag and set it to 1 in the interrupt routine when you want to start counting. In your loop look for that flag to be set to 1 and start incrementing. When you detect the button is no longer being pressed set the flag to 0.
Also you're never resetting your button state within the button task while loop. That's why your Button State is always coming back as 1.
Actually looking at it further I think you might just need to sample your input level level outside of your if statement within the for(;;) loop. I think this because (I believe) your if in the button task wont be called on a falling edge?

How to make animation in *.gif run only once in Qt? [duplicate]

I'm using QMovie and gif to create explosion after collision. The problem is that my gif is looping over and over, I've checked loopcount status and it returns -1 (infinite). How to display my gif just one time?
#include "Bullet.h"
#include <QTimer>
#include <QGraphicsScene>
#include <QList>
#include "Enemy.h"
#include "Game.h"
#include <typeinfo>
#include "levels.h"
extern Game * game; // there is an external global object called game
int Bullet::killed = 0;
int Bullet::missed = 0;
double Bullet::accurancy = 0;
Bullet::Bullet(QGraphicsItem *parent): QGraphicsPixmapItem(parent){
// draw graphics
setPixmap(QPixmap(":/images/res/images/bullets/bullet.png"));
missed++; // increse missed when bullet is created
movie = new QMovie(":/images/res/images/effects/explosion/64x48.gif");
processLabel = new QLabel;
processLabel->setMovie(movie);
// make/connect a timer to move() the bullet every so often
QTimer * timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(move()));
// start the timer
timer->start(2);
}
void Bullet::move(){
// get a list of all the items currently colliding with this bullet
QList<QGraphicsItem *> colliding_items = collidingItems();
// if one of the colliding items is an Enemy, destroy both the bullet and the enemy
for (int i = 0, n = colliding_items.size(); i < n; ++i){
if (typeid(*(colliding_items[i])) == typeid(Enemy)){
// increase the score
game->score->increase();
//play explosion animation
movie->start();
movie->setSpeed(180);
processLabel->setAttribute(Qt::WA_NoSystemBackground);
processLabel->setGeometry(QRect(x()-15,y()-15,64,48));
scene()->addWidget(processLabel);
qDebug() << movie->loopCount();
//connect(movie,SIGNAL(finished()),movie,SLOT(stop()));
// remove them from the scene (still on the heap)
scene()->removeItem(colliding_items[i]);
scene()->removeItem(this);
// delete them from the heap to save memory
delete colliding_items[i];
delete this;
killed++;
missed--; // decrese missed if bullet colide with enemy
if((killed+1) % 9 == 0)
{
game->level->Levels::incrementLevels();
game->score->Score::addToSum(); /// TODO
}
//qDebug() << "Already killed: " << killed;
//qDebug() << "Already missed: " << missed;
// return (all code below refers to a non existint bullet)
return;
}
}
// if there was no collision with an Enemy, move the bullet forward
setPos(x(),y()-1);
// if the bullet is off the screen, destroy it
if (pos().y() < 0){
scene()->removeItem(this);
delete this;
}
}
I had the same question and didn't find anything, so here's my solution:
connect the signal "frameChanged(int)" to:
void Bullet::frameChanged_Handler(int frameNumber) {
if(frameNumber == (movie->frameCount()-1)) {
movie->stop();
}
}
If you want to run X times the loop you just have to add a static counter to know how many times you've passed the last frame:
void Bullet::frameChanged_Handler(int frameNumber) {
static int loopCount = 0;
if(frameNumber == (movie->frameCount()-1)) {
loopCount++;
if(loopCount >= MAX_LOOP_TIMES)
movie->stop();
}
}
and of course, connect with this:
connect(movie, SIGNAL(frameChanged(int)), this, SLOT(frameChanged_Handler(int)));
That's it... Hope it can help you.

How to implement long press key event for stm32f429i discovery board using keil?

I interfaced a keypad to stm32f429i board and am able to display respective button of keypad on LCD screen of board. Short press is working fine but I have no idea how to implement long press key event. Can someone please help me?
Edit: As suggested by PeterJ_01, using a Timer interrupt is the most elegant way. Do not use the GPIO interrupt for buttons!
If you are using the Hardware Abstraction Layer (HAL) Library, you can do something like this inside your GPIO interrupt:
static int timestamp_pressed = -1; // -1 means not pressed
if (timestamp_pressed == -1) {
// user just pressed the button
timestamp_pressed = HAL_GetTick(); // milliseconds since startup
} else if (HAL_GetTick() - timestamp_pressed > 2000) {
// 2000 milliseconds = 2 seconds are over
// << your code
timestamp_pressed = -1;
}
If you are not using HAL and don't want to use a regular timer interrupt (from TIM1, TIM2, TIM3, ...) you can consider using the SysTick timer (with an interrupt or without). There is plenty information about it in the internet ;)
Yes - you need to use the timer interrupt. Here is the link and the library:
https://www.diymat.co.uk/arm-three-function-click-double-and-long-click-button-library-timer-interrupt-driven/
An implementation inspired from STM32F746G discovery board example.
if(HAL_GPIO_ReadPin(Push_GPIO_Port,Push_Pin) != RESET)
{
HAL_Delay(10);//debounce
timestamp_butpressed = HAL_GetTick();
while (HAL_GPIO_ReadPin(Push_GPIO_Port,Push_Pin) != RESET);
timestamp_butreleased = HAL_GetTick();
//Is button pressed enouph to run the program?
if(timestamp_butreleased>timestamp_butpressed?//This condition prevent max value to cause problem
((timestamp_butreleased-timestamp_butpressed)>2000)://normal condition
((4294967295-timestamp_butpressed)+timestamp_butreleased)>2000)//glitchy conditioin handle
{
//application runs here or flag handling heppens here
}
}
Another triky implementation (long press triggers as it reaches required time):
if(HAL_GPIO_ReadPin(Push_GPIO_Port,Push_Pin) != RESET)
{
HAL_Delay(10);
while(HAL_GPIO_ReadPin(Push_GPIO_Port,Push_Pin) == RESET)
{
HAL_Delay(5);
i++
if(i==100)break;
}
if(i<100)
{
short pressed
}
else
{
long pressed
}
}
A functional implementation:
uint8_t pushbot(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
{
if(HAL_GPIO_ReadPin(GPIOx,GPIO_Pin) != RESET)
{
uint32_t timestamp_butpressed=0;
uint32_t timestamp_butreleased=0;
HAL_Delay(10);//debounce
timestamp_butpressed = HAL_GetTick();
while (HAL_GPIO_ReadPin(GPIOx, GPIO_Pin) != RESET);
timestamp_butreleased = HAL_GetTick();
//Is button pressed enouph to run the program?
if(timestamp_butreleased>timestamp_butpressed?//This condition prevent max value to cause problem
((timestamp_butreleased-timestamp_butpressed)>1000)://normal condition
((4294967295-timestamp_butpressed)+timestamp_butreleased)>1000)//glitchy conditioin handle
{
return 2;// long pressed
}
else
{
return 1;// short pressed
}
}
else
{
return 0;// not pressed
}
}

processing + bitWrite + arduino

I am working with an Arduino and Processing with the Arduino library.
I get the error "The function bitWrite(byte, int, int) does not exist.";
it seams that processing + Arduino bitWrite function are not working together.
its raised due to this line:
arduino.bitWrite(data,desiredPin,desiredState);
my goal in this project is modifying a music reactive sketch to work with shift registers.
Here is my full code:
Arduino_Shift_display
import ddf.minim.*;
import ddf.minim.analysis.*;
import processing.serial.*;
import cc.arduino.*;
int displayNum = 8;
Arduino arduino;
//Set these in the order of frequency - 0th pin is the lowest frequency,
//while the final pin is the highest frequency
int[] lastFired = new int[displayNum];
int datapin = 2;
int clockpin = 3;
int latchpin = 4;
int switchpin = 7;
byte data = 0;
//Change these to mess with the flashing rates
//Sensitivity is the shortest possible interval between beats
//minTimeOn is the minimum time an LED can be on
int sensitivity = 75;
int minTimeOn = 50;
String mode;
String source;
Minim minim;
AudioInput in;
AudioPlayer song;
BeatDetect beat;
//Used to stop flashing if the only signal on the line is random noise
boolean hasInput = false;
float tol = 0.005;
void setup(){
// shift register setup
arduino.pinMode(datapin, arduino.OUTPUT);
arduino.pinMode(clockpin, arduino.OUTPUT);
arduino.pinMode(latchpin, arduino.OUTPUT);
arduino.digitalWrite(switchpin, arduino.HIGH);
//Uncomment the mode/source pair for the desired input
//Shoutcast radio stream
//mode = "radio";
//source = "http://scfire-ntc-aa05.stream.aol.com:80/stream/1018";
//mode = "file";
//source = "/path/to/mp3";
mode = "mic";
source = "";
size(512, 200, P2D);
minim = new Minim(this);
arduino = new Arduino(this, Arduino.list()[1]);
minim = new Minim(this);
if (mode == "file" || mode == "radio"){
song = minim.loadFile(source, 2048);
song.play();
beat = new BeatDetect(song.bufferSize(), song.sampleRate());
beat.setSensitivity(sensitivity);
} else if (mode == "mic"){
in = minim.getLineIn(Minim.STEREO, 2048);
beat = new BeatDetect(in.bufferSize(), in.sampleRate());
beat.setSensitivity(sensitivity);
}
}
void shiftWrite(int desiredPin, int desiredState)
// This function lets you make the shift register outputs
// HIGH or LOW in exactly the same way that you use digitalWrite().
// Like digitalWrite(), this function takes two parameters:
// "desiredPin" is the shift register output pin
// you want to affect (0-7)
// "desiredState" is whether you want that output
// to be HIGH or LOW
// Inside the Arduino, numbers are stored as arrays of "bits",
// each of which is a single 1 or 0 value. Because a "byte" type
// is also eight bits, we'll use a byte (which we named "data"
// at the top of this sketch) to send data to the shift register.
// If a bit in the byte is "1", the output will be HIGH. If the bit
// is "0", the output will be LOW.
// To turn the individual bits in "data" on and off, we'll use
// a new Arduino commands called bitWrite(), which can make
// individual bits in a number 1 or 0.
{
// First we'll alter the global variable "data", changing the
// desired bit to 1 or 0:
arduino.bitWrite(data,desiredPin,desiredState);
// Now we'll actually send that data to the shift register.
// The shiftOut() function does all the hard work of
// manipulating the data and clock pins to move the data
// into the shift register:
arduino.shiftOut(datapin, clockpin, MSBFIRST, data);
// Once the data is in the shift register, we still need to
// make it appear at the outputs. We'll toggle the state of
// the latchPin, which will signal the shift register to "latch"
// the data to the outputs. (Latch activates on the high-to
// -low transition).
arduino.digitalWrite(latchpin, arduino.HIGH);
arduino.digitalWrite(latchpin, arduino.LOW);
}
void draw(){
if (mode == "file" || mode == "radio"){
beat.detect(song.mix);
drawWaveForm((AudioSource)song);
} else if (mode == "mic"){
beat.detect(in.mix);
drawWaveForm((AudioSource)in);
}
if (hasInput){ //hasInput is set within drawWaveForm
for (int i=0; i<displayNum-1; i++){
if ( beat.isRange( i+1, i+1, 1) ){
shiftWrite(i, 1);
lastFired[i] = millis();
} else {
if ((millis() - lastFired[i]) > minTimeOn){
shiftWrite(i, 0);
}
}
}
}
} //End draw method
//Display the input waveform
//This method sets 'hasInput' - if any sample in the signal has a value
//larger than 'tol,' there is a signal and the lights should flash.
//Otherwise, only noise is present and the lights should stay off.
void drawWaveForm(AudioSource src){
background(0);
stroke(255);
hasInput = false;
for(int i = 0; i < src.bufferSize() - 1; i++)
{
line(i, 50 + src.left.get(i)*50, i+1, 50 + src.left.get(i+1)*50);
line(i, 150 + src.right.get(i)*50, i+1, 150 + src.right.get(i+1)*50);
if (!hasInput && (abs(src.left.get(i)) > tol || abs(src.right.get(i)) > tol)){
hasInput = true;
}
}
}
void resetPins(){
for (int i=0; i<ledPins.length; i++){
arduino.digitalWrite(ledPins[i], Arduino.LOW);
}
}
void stop(){
resetPins();
if (mode == "mic"){
in.close();
}
minim.stop();
super.stop();
}
BeatListener
class BeatListener implements AudioListener
{
private BeatDetect beat;
private AudioPlayer source;
BeatListener(BeatDetect beat, AudioPlayer source)
{
this.source = source;
this.source.addListener(this);
this.beat = beat;
}
void samples(float[] samps)
{
beat.detect(source.mix);
}
void samples(float[] sampsL, float[] sampsR)
{
beat.detect(source.mix);
}
}
You can achieve the same thing using standard bitwise operators. To turn a bit on:
data |= 1 << bitNumber;
The right-hand side (1 << bitNumber) is a bit-shift operation to create a suitable bit-mask. It takes the single '1' bit and moves it left until it reaches the desired position. The bitwise-or assignment (|=) combines that new bit-mask with the existing bits in data. This turns the desired bit on, but leaves the rest untouched.
The code to turn a bit off is slightly different:
data &= ~(1 << bitNumber);
You can see the same bit-shift operation here. However, it's preceded by the unary negation operator (~). This swaps all the 1's for 0's, and all the 0's for 1's. The result is the exact opposite of the bit-mask we used before. You can't do a bitwise-or operation this time though, or else you'll turn all the other bits on. The bitwise-and assignment (&=) is used instead to combine this mask with the data variable. This ensures the desired bit is turned off, and the rest are untouched.
In your code, desiredPin is the equivalent of bitNumber.
A full explanation of how bitwise operations work can be quite lengthy. I'd recommend looking for a good tutorial online if you need more help with that.
There are also the bitSet and bitClear Arduino macros that make the code a little more readable than bit shifting and using AND and OR. The format is either bitSet(what_to_modify,bit_number) and bitClear(what_to_modify,bit_number). These translate into very efficient code and can be used to manipulate both, variables and hardware registers. So for example, if you wanted to turn on pin 13 on the Arduino UNO, you would first need to look up that Arduino pin 13 is actually pin 5 on PORTB of the Atmel atmega328 chip. So the command would be:
bitSet(PORTB,5);

Resources