PIC microcontroller automatically pressed - button

I'm trying to turn on a LED on a pic24FV16KA301 microcontroller, through a button press. The problem is the LED automatically goes on. After some altering it looks like the PIC is automatically pressed. The button is connected with a pull up resistor.
Here is part of the code(since some of the code is irrelavent to the problem).
#include <xc.h>
#include "Header_School_Project.h"
#include <stdlib.h>
#include <stdio.h>
#include <libpic30.h>
#define _XTAL_FREQ 20000000
#define LED_LOW LATAbits.LATA4
#define BUTTON_LOW PORTAbits.RA1
void main(void)
{
TRISAbits.TRISA4 = 0;
TRISAbits.TRISA1 = 1;
while(1)
{
if(!BUTTON_LOW)
{
__delay_ms(100);
if(!BUTTON_LOW)
{
LED_LOW = 1;
}
}
else if(BUTTON_LOW)
{
LED_LOW = 0;
}
return;
}
If anyone can help me with this, that would be much appreciated.
EDIT: After changing the __delay_ms(100) to __delay_ms(1000) I see that the LED is flickering on and off really fast

First configure the pins of porta as digital pins as mentioned in the datasheet of corresponding microcontroller using ANSEL register.
One more thing is increase de-bouncing delay to about 300ms, this much can solve your problem.

As mentioned by Kozmotronik you need to set the pin to digital first. PICs default to analog inputs... this "defualt" has wasted enormous number of man hours.

Related

my serial port did not show my sensor data properly

Hey i got a bit problem with my Arduino and sensor
Here is what i tried ;
#define USE_ARDUINO_INTERRUPTS true // Set-up low-level interrupts for most acurate BPM math.
#include <PulseSensorPlayground.h> // Includes the PulseSensorPlayground Library.
#include <SoftwareSerial.h>
SoftwareSerial blue(0,1);
const int PulseWire = 0; // PulseSensor PURPLE WIRE connected to ANALOG PIN 0
const int LED13 = 13; // The on-board Arduino LED, close to PIN 13.
int Threshold = 550;
PulseSensorPlayground pulseSensor;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
blue.begin(9600);
pulseSensor.analogInput(PulseWire);
pulseSensor.blinkOnPulse(LED13); //auto-magically blink Arduino's LED with heartbeat.
pulseSensor.setThreshold(Threshold);
pulseSensor.begin();
}
void loop() {
// put your main code here, to run repeatedly:
int myBPM = pulseSensor.getBeatsPerMinute();
if(myBPM>200){
myBPM-100;
}
if (pulseSensor.sawStartOfBeat()) {
Serial.println(myBPM);
blue.println(myBPM);
}
delay(10);
}
this code I got from the example library and modified it.
so i want to send data to my android using Bluetooth but this sensor kinda ticked me off because whenever i use it with my HC-06 Bluetooth module it suddenly got a hearth beat without i even touching it and it just sends so much data ignoring the delay I set.
I just need to slowly sending data just like a second but the data didn't show up
so anyone can help?
I read your code and I noticed this piece of code
if(myBPM > 200){ myBPM - 100; }
that is poorly written if (I understand correctly) you want to check the size of myBPM and if it is larger than 200 then it should be subtracted 100.
it should be:
myBPM = myBPM - 100; not myBPM - 100;
I hope my answer will help you. Have a nice day!

WS2812 LED light strips behaving strange

I had a long strip of WS2812 lights (300 LEDs) and had some code that was working completely fine. I decided to cut the strip so it would be the length of my desk but now the lights are acting strange and not following my code at all. I modified the code so it would work with the new length but now it doesn't work. I'm not sure if this is a hardware or software issue so I'll share the code I used and we can narrow it down from there. I simplified the code to light up just one LED but all the LEDs light up and are random colors which I have not had happen before. Here is the code:
#include <FastLED.h>
#define NUM_LEDS 10
#define LED_PIN 6
CRGB led[NUM_LEDS];
void setup() {
delay(100);
FastLED.addLeds<WS2812, LED_PIN>(led, NUM_LEDS);
}
void loop() {
led[0] = CHSV(100, 255, 255);
FastLED.show();
}
Let me know if there is more information you need and I will be happy to provide, thanks!

'Keyboard' was not declared in this scope - Arduino coding

I'm writing a simple program for an Arduino that will take the input of 2 buttons and output simulated keys for 2 different functions to use in Clone Hero.
Arduino editor (both online and local versions) spit out
'Keyboard' was not declared in this scope
The offline editor asks if Keyboard.h is included... Which it obviously is.
Any ideas why?
// Keyboard - Version: Latest
#include <Keyboard.h>
//btnWhammy is the button to replace whammy bar function
//btnSP is the button to replace star power activation
//Set Clone Hero to register j for whammy and k for star power
//declaring constant integers for the pins on the Arduino
const int btnWhammy = 2;
const int btnSP = 13;
//Declaring integers for the state of the button press
int btnWhammyState = 0;
int btnSPState = 0;
void setup() {
//Initialisation of the pins as inputs
pinMode(btnWhammy, INPUT);
pinMode(btnSP, INPUT);
}
void loop() {
//Setting the button states to the read of the digital pin (LOW is off, HIGH is on)
btnWhammyState = digitalRead(btnWhammy);
btnSPState = digitalRead(btnSP);
//If the whammy button is pressed, send 'j' to the keyboard, wait 100ms then release all keys
if (btnWhammyState == HIGH) {
Keyboard.press('j');
delay(100);
Keyboard.releaseAll();
}
//If the Star Power button is pressed, send 'k' to the keyboard, wait 100ms then release all keys
if (btnSPState == HIGH) {
Keyboard.press('k');
delay(100);
Keyboard.releaseAll();
}
}
This is a classic mistake -- you are probably compiling for a non-Leonardo board, like a Uno. The Keyboard.h library is not included because it is not present for the board you are compiling with.
I took your code and compiled it for Leonardo -- no issues. For Uno, I get the same error as you...
keyboard key was not declared in this scope i found a very simple and working trick of this problem just go to your main file where you declare all other keys that you are facing not declare
#include <DigiKeyboard.h>
#define KEY_UP_ARROW 0x52
#define KEY_DOWN_ARROW 0x51
#define KEY_LEFT_ARROW 0x50
#define KEY_RIGHT_ARROW 0x4F
#define KEY_TAB 0x2B

Saving two values into arduino uno EEPROM

I am looking to save pulse and SPO2 readings from the cooking hacks e-health sensor platform (link below) onto my Arduino Uno's EEPROM then remove the health shield and use an Ethernet shield to move the data. A piece of my code is as below:
#include <PinChangeInt.h>
#include <eHealth.h>
#include <EEPROM.h>
int cont =0;
int addr =0;
int BPM;
int SPO2;
void loop() {
Serial.print("PRbpm:");
Serial.print(e.health.getBPM);
BPM =eHealth.getBPM();
Serial.print("%SPo2 :);
Serial.print(eHealth.getOxygenSaturation());
SPO2 =eHealth.getOxygenSaturation();
Serial.print("\n");
Serial.println("===========");
EEPROM.write(0,eHealth.getBPM());
delay(500);
}
The two readings I am trying to save and then load onto another sketch are BPM and SPO2, does the line:
EEPROM.write(0,eHealth.getBPM());
make sense and how would I also save and send the second reading SPO2?
Any help appreciated or any knowledge of why my idea will not work also appreciated, thanks.
Cooking-hacks sensor platform
EEPROM.write(0,BPM);
EEPROM.write(1,SPO2);

Using the SD card with the Seeed Studio TFT Touch Shield 2.0

I just purchased Seeed's TFT Touch Shield 2.0 for Arduino, but I cannot seem to figure out how to access the SD card while maintaining the ability to draw to the screen. The tutorials and documentation are quite insubstantial (for me), and most questions on the product site seem to be directed to the same wiki page, which doesn't explain anything about the SD interface, other than what example file draws bitmaps from the card.
I've used the SD interface with the Ethernet Shield before, but it's been a long time since then, so I can't quite remember the ins and outs. From my old code, it seems that, for normal usage of the SD library, you simply do:
#include <SD.h>
void setup()
{
pinMode(4, OUTPUT);
if (!SD.begin(4))
{
//Fail
}
... //Open file, read, etc.
}
To use the TFT screen normally (with the exception of drawing bitmaps), you do as such:
#include <SD.h>
#include <TFTv2.h>
#include <SPI.h>
void setup()
{
TFT_BL_ON; //Enable Backlight
Tft.TFTinit(); //Initialize TFT Screen
Tft.drawCircle(100, 100, 30,YELLOW); //Draw
}
In the provided example program on the wiki page for drawing bitmaps from the SD card, the setup code looks like this:
#include <SD.h>
#include <TFTv2.h>
#include <SPI.h>
#define chipSelect 4
Sd2Card card;
void setup()
{
pinMode(11,INPUT);
pinMode(12,INPUT);
pinMode(13,INPUT);
TFT_CS_HIGH;
pinMode(chipSelect,OUTPUT);
digitalWrite(chipSelect,HIGH);
Serial.begin(38400);
SPI.begin();
Tft.TFTinit();
//SPI.setClockDivider(SPI_CLOCK_DIV4);
//SDcard_info();
/**/
DDRB |= 0x04;
card.init(SPI_FULL_SPEED,chipSelect);
if(!SD.begin(chipSelect))//SPI_QUARTER_SPEED,
{ //53 is used as chip select pin
Serial.println("failed!");
while(1);
}
Serial.println("SD OK!");
Tft.setCol(0,239);
Tft.setPage(0,319);
Tft.sendCMD(0x2c);//start to write to display ram
TFT_BL_ON;
}
In loop() bitmaps are sequentially opened with SD.open(), drawn, and then closed with SD.close().
What I assume is happening is that pins 11 through 13 are set to input for some SPI-related reason, the TFT chip select 'enabled' mode is set to HIGH, and then the screen is subsequently enabled. Serial moniter is started, followed by SPI, and then the TFT. After those things happen, it does something unknown to me, starts the card, and then uses the standard card initialization method. It finishes up by preparing to draw the bitmaps and sends this 'command 0x2c', which is used frequently in the underlying libraries to "start to write to display ram".
The problem is that I have tried initializing the TFT and SD card using this code, and then attempted to draw graphics as shown in my second example, but this did not work. I need to be able to read bytes from the SD card, and then be able to draw simple graphics on-screen, and repeat.
So my question is: Is anyone who has used this shield before or has experience with this able to explain how one should go about writing the code to allow usage of both the SD card and screen or how the initialization and SPI processes work to make this possible?
Thanks for your answers in advance!
(Also, if this is not the correct SE site for this question, please feel free to migrate it accordingly.)
The solution to this problem is quite simple actually, and I must have been doing something wrong when I had combined the source files before.
The initialization code looks like this:
#include <SD.h>
#include <TFTv2.h>
#include <SPI.h>
Sd2Card card;
void setup()
{
pinMode(11, INPUT); //Pin mode changes; not sure what for
pinMode(12, INPUT);
pinMode(13, INPUT);
TFT_CS_HIGH; //Something with chipselect and the TFT
pinMode(4, OUTPUT); //Set chipselect pin to OUTPUT
digitalWrite(4, HIGH); //Set chipselect mode
SPI.begin(); //Start SPI
Tft.TFTinit(); //Initialize the TFT
TFT_BL_ON; //Turn on the TFT Backlight
Serial.begin(9600); //Start serial output
DDRB |= 0x04; //Some sort of processor IO port?
if(!SD.begin(4)) //Start the SD card
{
while(true) { } //Fail
}
}
It is basically the bitmap initialization code, with the extra TFT commands at the end left out. After this, both the screen and the SD card are usable, as was desired.

Resources