Reading input pin of pic microcontroller with MIKROC - microcontroller

I need to write a program that will check if an input pin of PIC has a voltage. If a voltage exists then it will give voltage to a selected output pin like PORTB.RB1=1;. Else it will give voltage to other selected output pin like PORTC.RC1=1;.
Is it possible? I have tried to do this, but it does not work .
void main() {
TRISB=0;
TRISA=1;
TRISC=0;
while(1){
delay_ms(500);
// PORTB=0;
if(PORTA==1){
PORTB.RB1 =1;
}
else{
PORTC.RC1 =1;
}
}
}

You have not turned off the other port output, and you have not isolated the input pin of PORTA. If it's bit 0 the mask is 1, if it's bit 1 the mask is 2, etc.
void main() {
TRISB=0;
TRISA=1;
TRISC=0;
while(1){
delay_ms(500);
if(PORTA & 1){
PORTB.RB1 =1;
PORTC.RC1 =0;
}
else{
PORTB.RB1 =0;
PORTC.RC1 =1;
}
}
}

The PORTA and the PORTE are analog ports. If you want to use them as Digital Input, you must Prevent PIC to use them as analog inputs.
You must add this instruction:
ADCON1=0x06;
before you set the PORTA or PORTE as input.
This code works successfully:
void main()
{
ADCON1=0x06;
TrisA=1;
TrisE=1;
TrisC=0;
PortC=0;
while (1)
{
if (PortA.B0==1)
PortC.B0=1;
else
PortC.B0=0;
if (PortA.B1==1)
PortC.B1=1;
else
PortC.B1=0;
if (PortA.B2==1)
PortC.B2=1;
else
PortC.B2=0;
if (PortA.B3==1)
PortC.B3=1;
else
PortC.B3=0;
if (PortA.B5==1)
PortC.B4=1;
else
PortC.B4=0;
if (PortE.B0==1)
PortC.B5=1;
else
PortC.B5=0;
if (PortE.B1==1)
PortC.B6=1;
else
PortC.B6=0;
if (PortE.B2==1)
PortC.B7=1;
else
PortC.B7=0;
}
}

hardware connection : wire input port's bit with 5v supply and switch.
after this connect it with pull down resistors.
void main(){
TRISB = 1; //set portB as input
TRISC = 0; //set portC as output
while(1){
if(PORTB.B0 == 0){ //if RB0 == 0 ?
PORTC.F0 = 1; //set RC0 = 1 ,(high)
}else PORTC.F0 = 0; //set RC0 = 0 ,(low)
if(PORTB.B1 == 0){ //if RB1 == 0 ?
PORTC.F1 = 1; //set RC1 = 1 ,(high)
}else PORTC.F1 = 0; //set RC1 = 0 ,(low)
//set if else block for numbers of bit as you want.....
}
}
note that pic port A is anlalog input default and if you want to use this port as digital port change the ADCON registers and follow datasheet.

Related

How do i send data from my esp8266 light sensor to blynk?

I am trying to know whether the lights have tripped using a light sensor to detect light intensity. However, i do not know how to upload my data onto blynk, as i am new to blynk and new to arduino as well. Below is my code. I tried using a telegram bot to get the data but somehow it stopped working and i am finding alternatives to get the data remotely. If there are any other methods other than Blynk please suggest too.
#include <ESP8266WiFi.h> // WIFI LIBRARY
#include <WiFiClient.h> //CLIENT LIBRARY
#include <ESP8266WebServer.h> //WEBSERVICER LIBRARY
#include <ESP8266HTTPClient.h> //HTTP CLIENT LIBRARY
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "ArchiEngStudio";
char pass[] = "a12345678";
int CNT = 0, sent, i, j;
float count = 0;
int limit = 800; // SET LIMIT HERE
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
void loop() {
float reading = analogRead(A0);
Serial.println(reading);
delay(3000);
/* NO LIGHT CONDITION */
if (reading < limit)
{
if (CNT < 1) // CHECK LOOP ONLY RUNS ONCE
{
count = 0;
for (i=0; i<10; i++) //TAKING 10 READINGS TO CHECK IF LIGHTS ARE REALLY OFF
{
delay(1000);
int read2 = analogRead(A0);
Serial.println(read2);
if (read2 < limit)
{
Serial.println("(1) No Light ...");
}
else
{
Serial.println("(1) Light on...");
i= i+20;
}
}
if (i < 11) // IF ALL 10 READINGS SHOW THAT LIGHTS ARE OFF, SEND MESSAGE
{
if (sent == 0)
{
Serial.println("Tripped!");
sent = 1;
}
}
}
CNT++;
}
else
{
CNT = 0;
}
/* LIGHTS ON CONDITION */
if (sent == 1) //ONLY ALLOWED IF LIGHTS WERE OFF
{
if (reading >= limit)
{
for (j=0; j<10; j++) //TAKING 10 READINGS TO CHECK IF LIGHTS ARE REALLY ON
{
delay(1000);
int read3 = analogRead(A0);
Serial.println(read3);
if (read3 >= limit)
{
Serial.println("(2) Light on...");
}
else
{
Serial.println("(2) No light...");
j= i+10;
}
}
if (j < 11) //IF ALL 10 READINGS SHOW THAT LIGHTS ARE ON, SEND MESSAGE
{
Serial.println("Light on!");
sent = 0;
}
}
}
}
Blynk provides a decent way to display your data and is very easy to use once you are familiar with the concept.
You can store your reading variable in a virtual pin 'Vx' on the Blynk server by adding this line to your code:
Blynk.virtualWrite(Vx, reading);
If you want to display it on the app just use "value display" widget and configure it to display your virtual pin 'Vx'.

Arduino code for multiple buttons and LED matrix

I'm trying to build a system using Arduino Uno, 8x8 LED matrix and 3 push buttons. The goal of the system is to display 3 different characters upon pushing the 3 buttons correspondingly. For instance, I've chosen the letters A,B,C. When the button corresponding to A is pressed, the letter A must be displayed and similar for B and C too. I'm kinda stuck in this code, where it seems logically correct for me but I have no idea why it isn't working. Thanks in advance.
#include "LedControlMS.h"
#define NBR_MTX 1
LedControl lc=LedControl(4,3,2, NBR_MTX);//
const int buttonPinA = 8;
const int buttonPinB = 9;
const int buttonPinC = 10;
char ip2;
void setup()
{
Serial.begin(9600);
for (int i=0; i< NBR_MTX; i++)
{
lc.shutdown(i,false);
lc.setIntensity(i,8);
lc.clearDisplay(i);
delay(100);
}
}
void Fun1()
{
lc.writeString(0,"A");
delay(500);
lc.clearAll();
}
void Fun2{
lc.writeString(0,"B");
delay(500);
lc.clearAll();
}
void Fun3()
{
lc.writeString(0,"C");
delay(500);
lc.clearAll();
}
void loop(){
if( digitalRead(buttonPinA) == HIGH){
ip2 = 1;}
else if(digitalRead(buttonPinB) == HIGH){
ip2 = 2;}
else if(digitalRead(buttonPinC) == HIGH){
ip2 = 3;
}
if(ip2 == '1'){
for(int i=1;i<=6;i++){
Fun1();
}
}
else if(ip2 == '2')
{
for(int i=1;i<=6;i++){
Fun2();}
}
else if(ip2 == '3'){
for(int i=1;i<=6;i++){
Fun3();}
}}
You're setting your char ip2 variable as an integer and then checking it as a character. You will see in this ASCII Table that '1' is equal to 31 as in integer, '2' is equal to 32 and so on.
Replacing the first few lines of you main loop with the code below should fix your problem. If not, I would check the documentation of the led library you're using and make sure you're implementing it correctly.
if( digitalRead(buttonPinA) == HIGH)
{
ip2 = '1';
}
else if(digitalRead(buttonPinB) == HIGH)
{
ip2 = '2';
}
else if(digitalRead(buttonPinC) == HIGH)
{
ip2 = '3';
}

How to stop multiple reads of an RFID card

I'm using an 125Khz RFID module RDM6300 with arduino nano.
While the card is near the RFID reader the loop will read the card multiple times. I want it to read only once while the card is near the reader then read it again if a new connection is being made.
*This code is not writen by me, this is the source:
https://github.com/Wookai/arduino-rfid
// define constants for pins
//int SUCCESS = 10;
//int ERROR = 13;
// variables to keep state
int readVal = 0; // individual character read from serial
unsigned int readData[10]; // data read from serial
int counter = -1; // counter to keep position in the buffer
char tagId[11]; // final tag ID converted to a string
char* authorizedTags[4]; // array to hold the list of authorized tags
// fills the list of authorzied tags
void initAuthorizedTags() {
// add your own tag IDs here
authorizedTags[0] = "0400680B85";
authorizedTags[1] = "0400063EB9";
authorizedTags[2] = "040004F3F5";
authorizedTags[3] = "04006813AB";
}
void setup() {
Serial.begin(9600);
// pinMode(SUCCESS, OUTPUT);
//pinMode(ERROR, OUTPUT);
initAuthorizedTags();
}
// check if the tag ID we just read is any of the authorized tags
int checkTag() {
int i;
for (i = 0; i < 4; ++i) {
if (strcmp(authorizedTags[i], tagId) == 0) {
return 1;
}
}
return 0;
}
// convert the int values read from serial to ASCII chars
void parseTag() {
int i;
for (i = 0; i < 10; ++i) {
tagId[i] = readData[i];
}
tagId[10] = 0;
}
// once a whole tag is read, process it
void processTag() {
// convert id to a string
parseTag();
// print it
printTag();
// check if the tag is authorized
if (checkTag() == 1) {
tagSuccess(); // if so, perform an action (blink a led, open a door, etc...)
} else {
tagFailed(); // otherwise, inform user of failure
}
}
void printTag() {
Serial.print("Tag value: ");
Serial.println(tagId);
}
// perform an action when an authorized tag was read
void tagSuccess() {
Serial.println("Tag authorized.");
// here, we simply turn on the success LED for 2s
// digitalWrite(SUCCESS, HIGH);
//digitalWrite(ERROR, LOW);
// delay(2000);
}
// inform the user that the tag is not authorized
void tagFailed() {
Serial.println("Unauthorized access!");
//digitalWrite(SUCCESS, LOW);
// digitalWrite(ERROR, HIGH);
// delay(2000);
}
// this function clears the rest of data on the serial, to prevent multiple scans
void clearSerial() {
while (Serial.read() >= 0) {
; // do nothing
}
}
void loop() {
// turn LEDs off
// digitalWrite(SUCCESS, LOW);
// digitalWrite(ERROR, LOW);
if (Serial.available() > 0) {
// read the incoming byte:
readVal = Serial.read();
// a "2" signals the beginning of a tag
if (readVal == 2) {
counter = 0; // start reading
}
// a "3" signals the end of a tag
else if (readVal == 3) {
// process the tag we just read
processTag();
// clear serial to prevent multiple reads
clearSerial();
// reset reading state
counter = -1;
}
// if we are in the middle of reading a tag
else if (counter >= 0) {
// save valuee
readData[counter] = readVal;
// increment counter
++counter;
}
}
}
Thank you.
Thank you for your answers. I tried to accept the multiple reads and print only one but it keeps printing "already read" instead of reading the card first time, this is the code:
void printTag() {
if(strcmp(tagId,previous)==1){
strcpy(previous, tagId);
Serial.print("Tag value: ");
Serial.println(tagId);
}
else
{
Serial.print("already read");
}
}
I also tried to put the delay after end of tag but it still reads the card multiple times.
I tried another code, it still reads the tag multiple times.
#include <SoftwareSerial.h>
// RFID | Nano
// Pin 1 | D2
// Pin 2 | D3
SoftwareSerial Rfid = SoftwareSerial(2,3);
int timer=0;
int reference = 1000;
int card_status = 0;
void setup() {
// Serial Monitor to see results on the computer
Serial.begin(9600);
// Communication to the RFID reader
Rfid.begin(9600);
}
void read() {
// check, if any data is available
// as long as there is data available...
while(Rfid.available() > 0 ){
// read a byte
int r = Rfid.read();
// print it to the serial monitor
Serial.print(r, DEC);
Serial.print(" ");
}
// linebreak
Serial.println();
timer=0;
}
void loop()
{
if((Rfid.available() > 0 ) && (card_status == 0) )
{
read();
}
if((!Rfid.available() > 0 ) && (card_status == 1) )
{
card_status=0;
}
}
I'm sorry for the late response. I forgot about this topic.
I solved the problem by making the arduino wait for a response after writing the RFID code for the frist time.
I was able to do that because my arduino was sending the code to a C# application via serial port.
Here is how it works: the arduino prints the RFID code on the serial, from there it is picked up by the C# application which searches a database to see if the code is stored there. Depending on the result, the application prints a character('y' or 'n') which is picked up by the arduino. Depending on the character recieved, the arduino lights up a led ( green or red) and makes a noise. Now a new RFID reading can be made.
Here is the code:
#include <SoftwareSerial.h>
#include "RDM6300.h"
SoftwareSerial rdm_serial(8, 9);
RDM6300<SoftwareSerial> rdm(&rdm_serial);
String comanda;
char c="";
int led_verde = 2;
int led_rosu = 7;
int buzzer = 12;
int i;
void buzz(int n = 1)
{
for (int i = 0; i < n; i++) {
digitalWrite(buzzer, LOW);
delay(200);
digitalWrite(buzzer, HIGH);
delay(200);
}
}
void ledVerde()
{
digitalWrite(led_verde, HIGH);
buzz(1);
delay(1000);
digitalWrite(led_verde, LOW);
}
void ledRosu()
{
digitalWrite(led_rosu, HIGH);
buzz(3);
delay(1000);
digitalWrite(led_rosu, LOW);
}
void setup()
{
pinMode(led_verde, OUTPUT);
pinMode(led_rosu, OUTPUT);
pinMode(buzzer, OUTPUT);
digitalWrite(led_verde, LOW);
digitalWrite(led_rosu, LOW);
digitalWrite(buzzer, HIGH);
Serial.begin(9600);
}
void loop()
{
static unsigned long long last_id = 0;
last_id = rdm.read();
rdm.print_int64(last_id);
Serial.println();
rdm_serial.end();
Serial.flush();
while(!Serial.available());
c=Serial.read();
if(c=='y')
{
ledVerde();
c="";
}
if(c=='n')
{
ledRosu();
}
Serial.flush();
last_id="";
c="";
rdm_serial.begin(9600);
}
You can find the RDM6300 library here: https://github.com/arliones/RDM6300-Arduino
Long time passed the original question, but maybe my answer would be useful for future visitors.
The RDM6300 works by:
automatically reading the data, and then
your code transfers the read data to the buffer for further processing.
Imagine it as a Baggage carousel in the airport. There are multiple luggage (data) on the carousel (reader), and you pick them one by one (transferring to buffer).
So, the problem of multiple reads, is that you have got read data in the reader (luggage on the carousel), that your code is gradually transferring them to the buffer (picking the luggage up).
In our example, if you don't like to collect all luggage, you can ask someone to take some of them, before they reach to you.
The below code does this. While you have data in the reader (the card is near to the reader), it transfers data from reader to buffer and then zeros all of them in the buffer:
First, place this code before "void setup()":
boolean multipleRead = false;
This defines a false/true variable to tell if this is the first time you are reading the tag (false), or it's being read multiple times (true).
Then, put this one at the end of the code block that shows the tag is fully read. If you are using Michael Schoeffler's library for your RDM6300/630 RFID, put it after "else if (ssvalue == 3) {":
multipleRead = true;
It change the variable to true, when your tag is read. That tells the program that your first read is done and the next upcoming read(s) would be "multiple read" and you don't want them.
Then put this at the end of the RFID reader code (if your RFID code is under void loop (), put the below code just after "void loop (){":
if (multipleRead) {
while (ssrfid.available() > 0) {
int ssvalue = ssrfid.read(); // read
if (ssvalue == -1) { // no data was read
break;
}
}
for (int x = 0; x < 14; x++)
{
buffer[x] = 0;
}
multipleRead = false;
}
It empties the reader and then zeros the buffer, while there is a card nearby. When you move the card away, multipleRead value would turn to false, which let another RFID reading loop to initiate.
Hope that helped :)
I guess what you want is a edge trigger instead of level trigger with time limit.
For example, you may prefer to read a RFID card when it firstly comes near to the antenna, once only; when it keeps to contact the antenna, still take no more actions. When you remove the card and place it again near to the antenna, the MCU starts to read the card again.
If so, the following code could be for your reference. What I have done is just to add 1 more flag to keep checking the card_status before checking if a RFID card comes near to the antenna.
int card_status = 0; //0:readable; 1:not-readable
if ( (Serial.available() > 0) && (card_status == 0) ) {
card_status = 1; //disable to read card after exit this loop
//your code, a card is near to the antenna, try to read it.
readVal = Serial.read();
if (readVal == 2) {
counter = 0; // start reading
} else if (readVal == 3) {
processTag();
clearSerial();
counter = -1;
} else if (counter >= 0) {
readData[counter] = readVal;
++counter;
}
}
reset the card status when no RFID signal comes in && card status is true,
if ( (!(Serial.available() > 0)) && (card_status == 1) ) {
card_status = 0; //enable to read card again
//no signal, no card is near to the antenna.
}
You can set a delay (e.g. delay(2000)) after reading the (end of tag). A delay of (say) 2 seconds will allow the user to move the card far enough away from the reader. Note that delay is a blocking command which might not suit your purposes, in which case you could be looking at using the millis count to activate/deactivate the Serial.read.
Another option is to accept the multiple reads, but keep a state of which card has been read. If the new card number is the same as the old card number (within a reasonable timeframe) then just ignore.

Cannot receive data more than once with 433Mhz on Arduino

I am currently building myself some lighting for my apartment using Arduinos and strips of WS2812b LEDs. My plan was to control them with an app from my smartphone and a Raspberry Pi. The phone sends data (like color, mode etc.) to the Pi and the Pi transmits that via a 433MHz transmitter to three Arduino Nanos I wanted to place across the room.
Sending data from my phone through the Pi to the Arduinos works without a problem, the data is received correctly and the LEDs light up, but as soon as I do this once no more data can be received. There is no issue when I just receive and don't do anything else.
Here's my code:
#include <RCSwitch.h>
#include <Adafruit_NeoPixel.h>
#define LEDPIN 9
#define NUMPIXELS 75
#define STARTBIT 4194305
#define STOPBIT 8388609
#define SETBIT 2097153
RCSwitch rc = RCSwitch();
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, LEDPIN, NEO_GRB + NEO_KHZ800);
uint32_t data[] = {0,0,0};
bool written[] = {false, false, false};
bool led_set = false;
uint32_t wait = 0;
uint16_t j = 0;
int index = 0;
bool recording = false;
int led = 0;
unsigned long time;
static unsigned long received = 0;
void setup() {
Serial.begin(9600);
time = millis();
pinMode(LEDPIN, INPUT);
rc.enableReceive(0); // Receiver on interrupt 0 => that is pin #2
strip.begin();
for (int i = 0; i < 75; i++) {
strip.setPixelColor(i, strip.Color(255,255,255));
}
strip.show();
}
void loop() {
if (millis()-time >= 1000) {
Serial.println(*((unsigned int*)rc.nReceivedValue));
Serial.println(rc.nReceivedValue);
Serial.println(millis());
time = millis();
}
if (rc.available()) {
received = rc.getReceivedValue();
rc.resetAvailable();
Serial.print("received: ");
Serial.print(received);
Serial.println();
}
if ((received == STOPBIT) && recording) {
Serial.println("stopped");
recording = false;
}
else if ((received == STARTBIT) && not recording) {
recording = true;
led_set = false;
Serial.println("started");
}
if (recording) {
if(received == SETBIT && written[index] && index < 3) {
index++;
Serial.println("setbit");
}
else if(received != STARTBIT && received != STOPBIT && received != SETBIT && received != 0 && not written[index] && index < 3) {
data[index] = received & 0xFFFFFFFF;
written[index] = true;
//Serial.println(received);
Serial.println(data[index]);
Serial.println("written");
}
}
else if (data[0] != 0 && data[1] != 0 && data[2] != 0 && not recording && not led_set) {
Serial.println("setting LED");
for (int i = 0; i < NUMPIXELS; i++) {
strip.setPixelColor(i, data[2]);
}
strip.show();
rc.resetAvailable();
}
if (not recording) {
for (int k = 0; k < 3; k++) {
written[k] = false;
}
index = 0;
}
}
(I pretty much ripped this code to pieces and this is what's left.)
These are the transmitter/receivers and I use RC-Switch and Adafruit NeoPixel as libraries, other LED-libraries (FastLED, light_ws2812) had the same error though.
What I found out so far is that it works with less than 5 LEDs and sometimes with more if I set the colors to low values. Then it's more or less chance if I get another receive or not. If I do not set the LEDs here
else if (data[0] != 0 && data[1] != 0 && data[2] != 0 && not recording && not led_set) {
Serial.println("setting LED");
for (int i = 0; i < NUMPIXELS; i++) {
strip.setPixelColor(i, data[2]);
}
strip.show();
I can receive more data, if I hardcode the values for the colors it works only for bright white (so every color to 255) or for color values below 14.
If I comment out the last four lines in setup() the problem can't even be solved by the restart button (unplugging helps though). Switching the receiver does not work either.
All three of the Arduinos I use have the same issue.
I use a 6A power supply (at least for the strip with 75 LEDs, the other two have 30 LEDs each with a 2.4A supply) so power should be no issue.
When I hooked up my Arduino Uno to the second data pin of the receiver it couldn't receive anything either, using the Uno with another receiver next to it was fine. Using the Uno as oscilloscope and displaying the voltage on the data pin I noticed a lot of noise after first transmitting. It looked like it increased with the color values. The noise was gone after I restarted the Arduino with the LED-setting in setup() and it doesn't occur if I don't do anything with the LEDs at all. There is a 5 MOhm pull-down resistor between data and GND on the receiver module. I don't actually know if this noise has anything to do with this, because receiving was sometimes fine even with noise.
I tried disabling the receive pin before setting the LEDs and enabling it afterwards but that didn't help. Enabling interrupts manually doesn't do anything either. I changed the rc-switch interrupt handling so it prints something every time an interrupt occurs. That worked even after receiving the first data, The Arduino somehow just doesn't recognize what's been sent.
I really would like to know if there's anything I could try to solve this, besides switching to WiFi-modules or actual wire.

ASCII reading with arduino

This is my first post, I know this theme could be very simple or obvious but I can't figure out how to solve it.
I have a capacitance meter from jyetech wiuch claims to have a 8-N-1 serial output, here is the link to the manual. I just want to read the output with my Arduino Uno, can anyone help me? Here is the code I have done, I'm getting some real data but also some strange characters.
#include <stdio.h>
void setup() {
Serial.begin(38400);
Serial.println("OK");
}
char command[1024];
char commandBuffer[128];
int commandBufferSize = 0;
void readCommandBuffer(int bytesToRead) {
int i = 0;
char c = 0;
while (i < 128 && (i < bytesToRead || bytesToRead <= 0)) {
while (!Serial.available())
;
c = Serial.read();
if (c == '\r' || c == '\n') {
break;
}
commandBuffer[i] = c;
i++;
}
commandBufferSize = i;
}
void readCommand() {
command[0] = '\0';
readCommandBuffer(0);
if (strncmp(commandBuffer, "RCV", 3) == 0) {
commandBuffer[commandBufferSize] = '\0';
int expectedSize = atoi(commandBuffer + 4);
if (expectedSize <= 0 || expectedSize > 1024) {
return;
}
Serial.println("RDY");
int bytesRead = 0;
while (bytesRead < expectedSize) {
readCommandBuffer(expectedSize - bytesRead);
memcpy(command + bytesRead, commandBuffer, commandBufferSize);
bytesRead += commandBufferSize;
Serial.print("ACK ");
Serial.println(commandBufferSize);
}
command[bytesRead] = '\0';
} else {
memcpy(command, commandBuffer, commandBufferSize);
command[commandBufferSize] = '\0';
}
}
void loop() {
if (Serial.available()) {
readCommand();
// "command" now contains the full command
Serial.println(command);
}}
You have to use two serial ports, one to talk to the PC (the usual Serial object), and the other to talk to the instrument.
If you have an Arduino UNO, which has only one hardware serial port, you have to use the software serial library. Mega boards have instead 4 hardware serial ports, which are available through the bulit-in objects Serial1, Serial2, etc.
BTW, just noticed a very similar question was asked awhile ago:
Serial Data communication Arduino

Resources