I searched around much to glow the LED on pressing PUSH Button in Proteus . But LED glows on its on and do not turn Off on pressing Push Button in Proteus. I am working on Simulation with PIC18F2455 and here is the MicroC code.
void main() {
TRISB.B0=1; //input button
TRISB.B7=0; //output
while (1)
{
if( PORTB.B0 == 0 )
{
PORTB.B7=1;
}
else
{
PORTB.B7=0;
}
}
}
Nabeel:
Based on your post it looks like you might not have hardware design properly which changes the status of Push Button Pin when you press it. You mention that LED glows right away then there can be wrong connection either with switch or with LED.
Please refer this circuit diagram:-
This is for AT89S51 but you can see how pull-up is connected with Push button Input pins S1 and S2.
Related
For a project I'm trying to get out of the Arduino loop() function and 'loop' in another function. I'm trying to make a sort of menu where the user can loop through and press an OK-button to confirm.
My setup is as follows;
Up button, OK button, Down button, set temperature button
Whenever the user pushes the temperature button, I'd like to give the user 2 options (using a LCD); change the minimum or the maximum temperature. I'd like to cycle through these options using the up and down button and give the user the option to confirm either of these temperature changes with the OK button. For some reason the Arduino doesn't listen to any button presses after I press the temperature button anymore. See code example below.
const int UpButton = 3;
const int OKButton = 4;
const int DownButton = 5;
const int ChangeTemperatureButton = 6;
void setup() {
pinMode(UpButton, INPUT_PULLUP);
pinMode(OKButton, INPUT_PULLUP);
pinMode(DownButton, INPUT_PULLUP);
pinMode(ChangeTemperatureButton, INPUT_PULLUP);
}
void loop() {
if (!digitalRead(ChangeTemperatureButton)) {
changeTemperature();
}
if (!digitalRead(OKButton)) {
// set LCD to 'not good'
}
}
void changeTemperature() {
// set LCD to 'Changing temperature'
if (!digitalRead(OKButton)) {
// set LCD to 'Set max temperature'
}
}
Whenever I press the button to change the temperature it does set the LCD to 'Changing temperature'. After that I press the OK button and instead of it changing to 'Set max temperature' it changes to 'not good', which is in the loop. I think I fundamentally understand something wrong here, can anyone help me out with this one?
TL;DR: I'm trying to get and stay out of the loop when I press a certain button so my button can do something else than is defined in the loop. After everything in the changeTemperature function is set and done, I'd like to return to the loop function.
When you press the ChangeTemperatureButton, and your program enters the changeTemperature() function, it immediately sets the LCD to 'Changing temperature' as you said.
The next thing it does is evaluate the condition of the if statement.
if (!digitalRead(OKButton)) {
If the condition of an if statement is false, then it is skipped over.
So if you are not simultaneously holding down the OKButton when you press the ChangeTemperatureButton, then that code will be skipped, and hence you are not seeing the 'Set max temperature' on your LCD.
I think I see though what you after, which is basically having a sub-menu, which is a very common feature of menus in general.
You might consider looking at some example code for LCD menus that use submenus to get an idea of how to implement your code.
You've gotten a good suggestion from YouJeng. To expand on that answer...
In your loop you'll do your initial button detection and debouncing work. Once the user presses the ChangeTemperatureButton your loop should call another function say "changeTemp()". You'll keep yourself in this function until you hit "OK".
There are tons of ways to accomplish this, but here's some simple pseudo-code:
void loop() {
//Draw your main menu buttons
//Handle button presses
if(ChangeTemperatureButton) {
changeTemp()
}
//Anything else your sketch needs to do
}
void changeTemp() {
bool okPressed = false;
//Display your text for the buttons in this mode
//Increase, decrease, OK (for example)
if(increase) {
//do something
}
if(decrease) {
//do something
}
if(OK) {
okPressed = true;
}
if(!okPressed) { //This essentially keeps you in this mode until you hit the OK button
changeTemp();
}
}
There are certainly cleaner ways to do this, but this is exactly how I've done it on other projects and it works great for a quick prototype.
Good Day,
I'm using Arduino IoT Cloud in a personal project.
Within the project there are two modes, "auto" and "manual". When the auto mode is selected, I need a switch widget to be inactive(greyed out) so the user cannot tap on it.
I have found a workaround in the interim, i.e. when the user is in auto mode and taps the switch, it will change its state on the widget, but then I reverse its state to initial before the tap.
eg. If you're in auto mode and the switch is on, and is then pressed (i.e. goes from on to off) then I change the switch widget state back to on. Below is the workaround.
void onSwitchStateChange() {
if (modeState == true) { // when the mode is auto
if (switchState == true) { // if switch state changes to "On" by tapping on the widget
switchState = false; // set the switch state back to its original state before the tap
}
else if (switchState == false) { // if switch state changes to "Off" by tapping on the widget
switchState = true; // set the switch state back to its original state before the tap
}
}
}
Is there a way "grey-out" the switch ?
I’m try to get it to work but it turns when I press the button and goes back when I let go of the button at the moment
//sketch created by Akshay Joseph follow me on Instagra: five_volt_player
include<Servo.h>
Servo Myservo;
int pos=0;
void setup()
{
pinMode(2,INPUT);
Myservo.attach(3);
}
void loop()
{
if(digitalRead(2)==LOW){
Myservo.write(180);
}
else
Myservo.write(0);
}
Many times a second it is "checking" to see what it should be doing. Right now your code says, "If at this exact moment I am pushing the button, then go to 180. Otherwise (If I'm not pushing the button) go to 0."
There are a few different ways to do what you're trying to do. Keep experimenting!
I would probably create a variable at the top of your code before void loop(), something like this:
int motorState = 0;
Then have the Myservo.write(motorState) each time just inside void loop().
For the if else statement in, I would say (unformatted) something like: if the button is pressed and motorState==180, then motorState==0.
Else if button pressed and motorState=180, then motorState==0.
I am creating an application that should work on desktop and some mobile platforms.
The following example creates and connects my portrait/landscape buttons, in a group, to a slot, on the release signal.
m_landscapeRadio = new QRadioButton(QObject::tr("Landscape "));
m_portraitRadio = new QRadioButton(QObject::tr("Portrait "));
m_orientationGroup.addButton(m_landscapeRadio, 0);
m_orientationGroup.addButton(m_portraitRadio, 1);
m_orientationGroup.setExclusive(true);
m_landscapeRadio->setChecked(true);
connect(&m_orientationGroup, SIGNAL(buttonReleased(int)), this, SLOT(orientationSlot(int)));
But I found a weird situation:
Assume landscape button is checked. If I press and drag away from the portrait radio button, the slot action is performed (for the portrait option) but the portrait button is not checked.
I would like the action not to be performed.
For now...
In the orientationSlot I test the argument and set the checked value myself... Though I really expected the buttons to know to do that themselves.
But I think it is more expected by users that, if the press a button and change their mind, to be able to drag away from the button and not have the action be performed.
I can handle verifying if the check really happened in the action slot, and either check or discard the action depending on how I will think the user experience is better...
If I want the buttons to be checked and to perform the action as well:
void MyWidget::orientationSlot(int checked)
{
if(checked) m_portraitRadio->setChecked(true);
else m_landscapeRadio->setChecked(true);
.... actual actions
}
If I want the action not to be performed when the user drags away from the button (my preferred option):
void MyWidget::orientationSlot(int checked)
{
if(m_orientationGroup.checkedId() != checked) return;
.... actual actions
}
I use QRadioButton and handle mouse button being released event for reacting
on radio button being switched. It causes problems altogether with dragging event. I would like to either get the button
to be checked, or the action not to be performed.
http://doc.qt.io/qt-5/qradiobutton.html
Whenever a button is switched on or off, it emits the toggled()
signal. Connect to this signal if you want to trigger an action each
time the button changes state. Use isChecked() to see if a particular
button is selected.
Either you connect the radio button to the handler explicitly or the whole group: http://doc.qt.io/qt-5/qbuttongroup.html#buttonToggled
void QButtonGroup::buttonToggled(QAbstractButton *button, bool
checked)
This signal is emitted when the given button is toggled. checked is
true if the button is checked, or false if the button is unchecked.
Note: Signal buttonToggled is overloaded in this class. To connect to
this one using the function pointer syntax, you must specify the
signal type in a static cast, as shown in this example:
connect(buttonGroup, static_cast<void(QButtonGroup::*)
(QAbstractButton *, bool)>(&QButtonGroup::buttonToggled),
[=](QAbstractButton *button, bool checked) {
if (button == m_portraitRadio) {
// Portrait (un)checked
if (checked)
{
// checked!
}
}
/* ... */ });
I am connecting Processing and an Arduino pushbutton. It's successfully connected with Standard Firmata. What I want to happen is that when the button is pressed, an image will show up in Processing but also stay on the screen, just like the LED, and then when the button is pressed again, the image will disappear from the screen. I'm just testing it with shapes for now. I have tested it with an LED and that works fine. Any ideas what I'm doing wrong? This is the code I have:
void draw()
{
buttonState = arduino.digitalRead(buttonPin);
if (buttonState == arduino.HIGH && buttonPressed == 0)
{
buttonPressed = 1;
rect(10, 10, 10, 10);
text("hello", 10, 10);
}
if (buttonState == arduino.LOW && buttonPressed == 1)
{
buttonPressed = 0;
rect(50, 50, 10, 10);
}
}
I'm not sure how you've wired up your button. I'm assuming it goes HIGH when you press it.
Currently, your first if statement will get triggered when the button is pressed, but only if it wasn't pressed last time you checked (i.e. it's effectively rising edge triggered).
Your second if statement will get triggered when the button is released, but only if it wasn't released last time you checked (i.e. it's effectively falling edge triggered).
If you want the button to toggle something each time it's pressed, then you will probably need to put most of your logic into the first if statement (except the buttonPressed stuff). You will need to store some kind of value which says if the image is currently visible. If it's visible when the button is pressed, then hide it (and vice versa).
For example:
boolean imageVisible = false;
void draw()
{
buttonState = arduino.digitalRead(buttonPin);
if (buttonState == arduino.HIGH && buttonPressed == 0)
{
buttonPressed = 1;
if (imageVisible) {
// Hide image here...
} else {
// Show image here...
}
imageVisible = !imageVisible;
}
if (buttonState == arduino.LOW)
{
buttonPressed = 0;
}
}
Note: I removed the buttonPressed check from the second if statement. It's only necessary if you actually need to respond to a falling-edge event. For a simple toggle, it's not important.