I Am working with arduino mega 2560....
Here I want to toggle two led simultaneously using threading.....
but there is problem that it is running one by one...
here is the code...
it would b your pleasure...
#include <SPI.h>
#include <Thread.h>
#include <ThreadController.h>
Thread Led1=Thread();
Thread Led2=Thread();
ThreadController controll=ThreadController();
void Led1run()
{
Serial.println("Led1");
Led2.run();
for(int i=0;i<5;i++)
{
digitalWrite(13,HIGH);
delay(1000);
digitalWrite(13,LOW);
delay(1000);
}
}
void Led2run()
{
Serial.println("Led2");
for(int i=0;i<5;i++)
{
digitalWrite(53,HIGH);
delay(1000);
digitalWrite(53,LOW);
delay(1000);
}
}
void setup()
{
Serial.begin(9600);
Led1.onRun(Led1run);
Led2.onRun(Led2run);
controll.add(&Led1);
pinMode(13,OUTPUT);
pinMode(53,OUTPUT);
}
void loop()
{
if(Serial.available())
{
Serial.println("loop");
Led1.run();
}
}
Related
I'm trying to connect to a Bluetooth device on my Windows 10 machine, but nothing is showing up. Not with BT classic or LE. Has anyone managed to get this working?
void __fastcall TForm1::DiscoverClick(TObject *Sender)
{
Bluetooth1->DiscoverDevices(5000);
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//void __fastcall TForm1::ListBox1Click(TObject *Sender)
//{
// BluetoothLE1->DiscoverServices(BluetoothLE1->DiscoveredDevices->Items[ListBox1->ItemIndex]);
//}
//---------------------------------------------------------------------------
//void __fastcall TForm1::BluetoothLE1EndDiscoverServices(TObject * const Sender, TBluetoothGattServiceList * const AServiceList)
//{
// int i;
// for (i = 0; i < 10; i++) {
// ListBox2->Items->Add(AServiceList->Items[i]->UUIDName);
// }
//}
//---------------------------------------------------------------------------
void __fastcall TForm1::Bluetooth1DiscoveryEnd(TObject * const Sender, TBluetoothDeviceList * const ADeviceList)
{
int i;
for (i = 0; i < ADeviceList->Count; i++) {
ListBox1->Items->Add(ADeviceList->Items[i]->DeviceName);
}
}
//---------------------------------------------------------------------------
I want to use timer interrupter combined with BLE to transmit in esp32.
I found that an error occurs when the transmission cycle is too fast.
I don't know what caused this error.
Help me plz.............................................................................................
The error
This is my code
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
////////////////////////////////////////////////////////////
#include <BLE2902.h>
#define PERIOD_MS 5
#define BAUD_RATE 115200
#define BLE_WR_EN 0x5A
#define BLE_WR_DIS 0x55
#define SERVICE_UUID "0000ffe0-0000-1000-8000-00805f9b34fb"
#define CHARACTERISTIC_UUID_RX "0000ffe2-0000-1000-8000-00805f9b34fb"
#define CHARACTERISTIC_UUID_TX "0000ffe1-0000-1000-8000-00805f9b34fb"
int PERIOD_US=PERIOD_MS*1000;
/* create a hardware timer */
hw_timer_t * timer = NULL;
/* LED pin */
int led = 2;
/* LED state */
volatile byte state = LOW;
int i=0;
BLEServer *pServer;
BLEService *pService;
BLECharacteristic *pCharacteristic;
void IRAM_ATTR onTimer(){
//state = !state;
if(i<700){
i++;
}
else{
i=0;
}
//Serial.println(state);
char txString[8];
dtostrf(i, 1, 2, txString);
pCharacteristic->setValue(txString);
pCharacteristic->notify();
std::string txValue=pCharacteristic->getValue();
Serial.print("txValue:");
Serial.println(txValue.c_str());
//digitalWrite(led, state);
}
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
Serial.println("********************************");
Serial.println("Connected");
Serial.println("********************************");
timerAlarmEnable(timer);
};
void onDisconnect(BLEServer* pServer) {
Serial.println("********************************");
Serial.println("Disconnected");
Serial.println("********************************");
timerAlarmDisable(timer);
//delay(1000);
///*
// Start the service
//pService->start();
// Start advertising
//pServer->getAdvertising()->start();
pServer->startAdvertising();
//*/
//ESP.restart();
}
};
class MyCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string rxValue = pCharacteristic->getValue();
std::string EN="Z";
std::string DIS="U";
Serial.print("rxValue:");
Serial.println(rxValue.c_str());
/*
//Serial.println(getvalue);
if (rxValue==EN ) {
//g_Timer.enable(g_TimerId);
timerAlarmEnable(timer);
digitalWrite(LED, HIGH);
}
else if (rxValue==DIS) {
//g_Timer.disable(g_TimerId);
timerAlarmDisable(timer);
//data_count = 0;
digitalWrite(LED, LOW);
}
*/
}
};
void setup() {
Serial.begin(115200);
Serial.begin(BAUD_RATE);
////////////////////////////////////////
Serial.println("Starting BLE Server!");
BLEDevice::init("ESP32-INTERRUT-Server");
/////////////////////////////////////////////////////////////////////////////////////
BLEServer *pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// Create the BLE Service
BLEService *pService = pServer->createService(SERVICE_UUID);
// Create a BLE Characteristic
pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID_TX,
BLECharacteristic::PROPERTY_READ|
BLECharacteristic::PROPERTY_NOTIFY
);
pCharacteristic->addDescriptor(new BLE2902());
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID_RX,
BLECharacteristic::PROPERTY_WRITE
);
pCharacteristic->setCallbacks(new MyCallbacks());
// Start the service
pService->start();
// Start advertising
pServer->getAdvertising()->start();
////////////////////////////////////////////////////////////////////
timer = timerBegin(0, 80, true);
timerAttachInterrupt(timer, &onTimer, true);
timerAlarmWrite(timer, PERIOD_US, true);
timerAlarmDisable(timer);
}
void loop() {
}
I have a serial barcode scanner and Arduino Uno.
I need to configuration..one Correct barcode scanning led on.
my code is
void setup ()
{
Serial.begin (9600);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
}
void loop ()
{
if(Serial.available())
{
String command =Serial.readStringUntil('\n');
Serial.println(command);
if(command =="1010007")
{
digitalWrite(3, HIGH);
digitalWrite(4,LOW);}
else
if(command !="1010007")
{digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
digitalWrite(3,LOW);}
}}
im scanning barcode 1010007 it showing serial monitor g⸮⸮⸮⸮ like this
how to fix it
plz help
and i try another code it showing correctly barcode,,,,,i scan 1010007 serial monitor also same 1010007
but led not on...code is below,,,
#include <SoftwareSerial.h>
SoftwareSerial barcode = SoftwareSerial(2, 3, true); // RX, TX
void setup()
{
Serial.begin(9600);
pinMode(3, OUTPUT);
barcode.begin(9600);
delay(1000);
}
void loop()
{
if(barcode.available())
{
char data = barcode.read();
Serial.write(data);
if(data=="101007")
{
digitalWrite(3, HIGH);
}
}}
How can I measure the bluetooth connection force with ESP32? I'm using the available example of BLE to detect the possibility of connection, but I need to measure its strength. Thank you.
I'm using:
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
int scanTime = 30; //In seconds
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());
}
};
void setup() {
Serial.begin(115200);
Serial.println("Scanning...");
BLEDevice::init("");
BLEScan* pBLEScan = BLEDevice::getScan(); //create new scan
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
BLEScanResults foundDevices = pBLEScan->start(scanTime);
Serial.print("Devices found: ");
Serial.println(foundDevices.getCount());
Serial.println("Scan done!");
}
void loop() {
// put your main code here, to run repeatedly:
delay(2000);
}`
Just a few lines added to your MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks :
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str
int rssi = advertisedDevice.getRSSI();
Serial.print("Rssi: ");
Serial.println(rssi);
}
};
in wifi i am using this function:
// Take a number of measurements of the WiFi strength and return the average result.
int getStrength(int points){
long rssi = 0;
long averageRSSI=0;
for (int i=0;i < points;i++){
rssi += WiFi.RSSI(); // put your BLE function here
delay(20);
}
averageRSSI=rssi/points;
return averageRSSI;
}
and you have the same function for BLE than WiFi.RSSI(): (see in github source code)
int BLEAdvertisedDevice::getRSSI() {
return m_rssi;
} // getRSSI
I just merely want to emit 2 signal and a Qtimer timeout on button click from a GUI.
Although 2 SIGNAL/SLOT WORKS the QTimer's SIGNAL/SLOT is not working, the whentimeout SLOT never works.
There is no error even.
GUI(DIALOG.CPP)
#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
fduthread = new fdustatus(this);
connect(fduthread,SIGNAL(NumberChanged(int)),this,SLOT(onNumberChanged(int)));
connect(fduthread,SIGNAL(nameChange(QString)),this,SLOT(onNameChanged(QString)));
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::onNumberChanged(int number)
{
ui->label->setText(QString::number(number));
}
void Dialog::onNameChanged(QString s)
{
ui->label_2->setText(s);
}
void Dialog::on_pushButton_clicked()
{
fduthread->start();
fduthread->stop=false;
}
void Dialog::on_pushButton_2_clicked()
{
ui->label_2->setText("");
fduthread->stop=true;
}
THAT IS MY THREAD
#include "fdustatus.h"
#include<QMutex>
#include<QTimer>
#include<QDebug>
fdustatus::fdustatus(QObject *parent):QThread(parent)
{
}
void fdustatus::run()
{
mytimer = new QTimer();
mytimer->setInterval(10);
connect( mytimer,SIGNAL(timeout()),this,SLOT(whentimeout()));
mytimer->start();
for(int i =0;i<100;i++)
{
QMutex mutex;
mutex.lock();
if(this->stop)break;
mutex.unlock();
emit NumberChanged(i*10);
emit nameChange(getstring());
this->msleep(100);
}
}
QString fdustatus::getstring()
{
QString networkport;
networkport.append("Alarm Active");
return networkport;
}
void fdustatus::whentimeout()
{
qDebug() << "timer ended from thread..";
}