RE: Arduino with neo 6m GPS and push button - c++

I am an aeronautical student, new to the coding environment.
I'm currently working on a GPS neo 6m module with Arduino mega 2560, where I wanted to save the current location upon pressing the push button. Which function is to be used to save the location by pressing the push button.
Here is what I have done so far. Any help would be much appreciated. Thanks in advance.
#include <SoftwareSerial.h>
// The TinyGPS++ object
TinyGPSPlus gps;
static const int RXPin = 4, TXPin = 3; //gps module connections
static const uint32_t GPSBaud = 9600;
// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);
const int PUSH_BUTTON = 2;
void setup(){
pinMode(PUSH_BUTTON, INPUT_PULLUP); //push button input
Serial.begin(9600);
ss.begin(GPSBaud);
}
void loop(){
unsigned char i;
static const double homeLat = 12.334455, homeLon = 05.112233;
while (ss.available() > 0){
gps.encode(ss.read());
if (gps.location.isUpdated()){
Serial.print("Latitude= ");
Serial.print(gps.location.lat(), 6);
Serial.print(" Longitude= ");
Serial.println(gps.location.lng(), 6);
}
status = digitalRead(PUSH_BUTTON);
if (status== HIGH){
*missing code/confused*
}
delay(1000);
}
}```

It is simple just store the values in 2 variables.
#include <SoftwareSerial.h>
// The TinyGPS++ object
TinyGPSPlus gps;
static const int RXPin = 4, TXPin = 3; //gps module connections
static const uint32_t GPSBaud = 9600;
// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);
const int PUSH_BUTTON = 2;
double save_lat, save_lang;
void setup(){
pinMode(PUSH_BUTTON, INPUT_PULLUP); //push button input
Serial.begin(9600);
ss.begin(GPSBaud);
}
void loop(){
unsigned char i;
static const double homeLat = 12.334455, homeLon = 05.112233;
while (ss.available() > 0){
gps.encode(ss.read());
if (gps.location.isUpdated()){
Serial.print("Latitude= ");
Serial.print(gps.location.lat(), 6);
Serial.print(" Longitude= ");
Serial.println(gps.location.lng(), 6);
}
status = digitalRead(PUSH_BUTTON);
if (status== HIGH){
if (gps.location.isUpdated()){
save_lat = gps.location.lat();
save_lang = gps.location.lng();
}
}
delay(1000);
}
}

Related

Having trouble sending a string via I2C

I have an arduino with 3 sensors connected and need to send the information I'm getting from the said sensors to another arduino and have it displayed on a connected lcd. I tried to go about this by changing the float values I'm getting into a string, sending the string to the other arduino and displaying that on the lcd. But when I try to execute this, I get an error saying :
" In function 'void receiveEvent(int)':
16:24: error: incompatible types in assignment of int to char [7] "
#include <Wire.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
int sensePin = A0;
int sensorInput;
int lightPin = A1;
int lightInput;
int gasPin = A2;
int gasInput;
int buzzer = 13;
int b_state = 3;
float temp;
float light;
char t_buffer[7];
int state = 0;
void setup()
{
Wire.begin();
pinMode(A0, INPUT);
pinMode(A1,INPUT);
pinMode(buzzer, OUTPUT);
pinMode(A2, INPUT);
pinMode(buzzer, OUTPUT);
pinMode(b_state, INPUT_PULLUP);
Serial.begin(9600);
lcd.begin(16, 2);
attachInterrupt(digitalPinToInterrupt(3), buttons, FALLING);
}
void loop()
{
gasInput = analogRead(gasPin);
sensorInput = analogRead(sensePin);
lightInput = analogRead(lightPin);
temp = (float)sensorInput / 1024;
temp = temp * 5;
temp = temp - 0.498;
temp = temp * 100;
light = ((float)lightInput - 26) / 8.97;
dtostrf(temp, 6, 2, t_buffer);
Serial.print("Current Temperature: ");
Serial.println(temp);
Serial.print("Current Light: ");
Serial.println(light);
Serial.print("Gas (conc.?): ");
Serial.println(gasInput);
Wire.beginTransmission(9);
Wire.write(t_buffer, 7);
Wire.endTransmission();
delay(100);
if (state == 0){
lcd.setCursor(0, 0);
lcd.print("Temp (C): ");
lcd.setCursor(0,1);
lcd.print(t_buffer);
}
else {
lcd.setCursor(0,0);
lcd.print("Light Intensity: ");
lcd.setCursor (0,1);
lcd.print(light);
}
if (gasInput > 800)
digitalWrite(buzzer, HIGH);
else
digitalWrite(buzzer, LOW);
}
void buttons()
{
lcd.clear();
state = !state;
}
#include <Wire.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
float temp;
char t_buffer[7];
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
Wire.begin(9);
Wire.onReceive(receiveEvent);
}
void receiveEvent(int bytes) {
t_buffer = Wire.read();
//temp = t_buffer.toFloat();
}
void loop() {
lcd.setCursor(0, 0);
lcd.print("Temp (C): ");
lcd.setCursor(0,1);
lcd.print(t_buffer);
}
Also I have 2 separate float values that I need to send and I'm thinking I could time the transmissions properly and send them both one after the other. Is this a good way of doing this or is there an alternative way?
The circuit
This may not be a direct answer to your question but it may help you figure it out.
t_buffer is a pointer, while t_buffer[0] is the first character in your buffer. The Wire.read returns a char and not a pointer.
If the explanation above does not make sense you may want to read on arrays.
Having said that, since you main loop is almost empty you can use the suggestion by Juraj and use Wire.readBytes(t_buffer , bytes) this way you do not have to worry about indexing through the buffer.
I would strongly suggest that you put text data in t_buffer and test your application before trying to send a float.

Connecting an Arduino ESP32 microcontroller to javafx via PacketSender

We have built a drone controller using C++ and an Arduino ESP32 module. We have achieved connection to a Tello Drone and can control it successfully. However, now we want to connect our controller to a javafx program that receives input and then draws on a canvas in scene builder.
Meanwhile the problem is that we can't seem to connect our controller through PacketSender. We have attached our code, including the previous built that enabled connection with a drone.
The big question is how to send the messages between two programs and initiate our virtual depiction of our drone on a canvas - instead of the actual physical one.
The issue seems to be in case 1, line 190, where we connected controller to actual drone, but now have to write up a new connection somehow.
#include <Arduino.h>
#include "WiFi.h"
#include "AsyncUDP.h"
const char * ssid = "OnePlus 7 Pro";
const char * password = "hej12345";
//Connect button variables (Command)
int inPinLand = 18;
int valLand = 0;
//Ultra sonic sensor variables
#define trigPin 2
#define echoPin 21
//Land button variables (Land)
int inPin = 25;
int val = 0;
//Instantiate specific drone
const char * networkName = "TELLO-59F484";
const char * networkPswd = "";
//IP address to send UDP data to:
// either use the ip address of the server or
// a network broadcast address
const char * udpAddress = "10.60.0.227";
const int udpPort = 7000;
boolean connected = false;
char fromTello[256];
unsigned long timer;
//static const byte glyph[] = { B00010000, B00110100, B00110000, B00110100, B00010000 };
//static PCD8544 lcd;
uint8_t state = 0;
//Controller movement variables
int pitch = 0;
int roll = 0;
int yaw = 0;
int throttle = 0;
char cmd[256];
AsyncUDP udp;
void setup() {
Serial.begin(9600);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("WiFi Failed");
while (1) {
delay(1000);
}
}
pinMode(5, OUTPUT);
digitalWrite(5, HIGH);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(inPin, INPUT);
pinMode(inPinLand, INPUT);
//LCD screen initialization
//lcd.begin(84, 48);
Serial.begin(9600);
//pinMode(trigPin, OUTPUT);
//pinMode(echoPin, INPUT);
//pinMode(BL, OUTPUT);
//digitalWrite(BL, HIGH);
if (udp.listen(7000)) {
Serial.print("UDP Listening on IP: ");
Serial.println(WiFi.localIP());
udp.onPacket([](AsyncUDPPacket packet) {
Serial.print("UDP Packet Type: ");
Serial.print(packet.isBroadcast()
? "Broadcast"
: packet.isMulticast() ? "Multicast" : "Unicast");
Serial.print(", From: ");
Serial.print(packet.remoteIP());
Serial.print(":");
Serial.print(packet.remotePort());
Serial.print(", To: ");
Serial.print(packet.localIP());
Serial.print(":");
Serial.print(packet.localPort());
Serial.print(", Length: ");
Serial.print(packet.length());
Serial.print(", Data: ");
Serial.write(packet.data(), packet.length());
Serial.println();
// reply to the client/sender
packet.printf("Got %u bytes of data", packet.length());
});
}
// Send unicast
// udp.print("Hello Server!");
// udp.
}
//WiFi connection function
void connectToWiFi(const char * ssid, const char * pwd) {
Serial.println("Connecting to WiFi network: " + String(ssid));
// delete old config
WiFi.disconnect(true);
//Initiate connection
WiFi.begin(ssid, pwd);
Serial.println("Waiting for WIFI connection...");
}
//Drone connection function
void TelloCommand(char *cmd) {
//only send data when connected
if (connected) {
//Send a packet
//udp.beginPacket(udpAddress, udpPort); OUTDATED has new name with ASync
udp.printf(cmd);
//udp.endPacket(); OUTDATED has new name with ASync
Serial.printf("Send [%s] to Tello.\n", cmd);
}
}
void sendMessage(String msg){
udp.writeTo((const uint8_t *)msg.c_str(), msg.length(),
IPAddress(169, 254, 107, 16), 4000);
}
void loop() {
delay(5000);
// Send broadcast on port 4000
udp.broadcastTo("Anyone here?", 4000);
// Serial.println("waiting for udp message...");
int x = 100;
int y = 100;
sendMessage("init " + String(x) + " " + String(y));
}
void loop() {
long duration, distance;
val = digitalRead(inPin); // read input value
//Ultra sonic sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
//LCD screen line 2
// lcd.setCursor(0, 1);
// lcd.print(distance, DEC);
//State machine that connects the drone to WiFi and the controller
switch (state)
{
case 0: //Idle not connected
//LCD screen line 1
//lcd.setCursor(0, 0);
//lcd.print("Controller");
if (val == HIGH)
{
state = 1;
connectToWiFi(networkName, networkPswd);
timer = millis() + 5000;
}
break;
case 1: //Trying to connect
if (WiFi.status() == WL_CONNECTED)
{
Serial.print("Connected to: ");
Serial.println(networkName);
udp.begin(WiFi.localIP(), udpPort);
connected = true;
TelloCommand("command");
timer = millis() + 2000;
state = 2;
}
if (millis() > timer)
{
state = 0;
}
break;
case 2: //Connected on ground
//lcd.setCursor(0, 0);
//lcd.print("Connected ");
if (WiFi.status() != WL_CONNECTED)
{
WiFi.disconnect(true);
Serial.println("Disconnected");
state = 0;
}
if (distance < 10)
{
TelloCommand("takeoff");
timer = millis() + 1000;
state = 3;
Serial.println("takeoff");
}
break;
case 3: //In air
//lcd.setCursor(0, 0);
//lcd.print("In air ");
if (millis() > timer)
{
timer = millis() + 20;
pitch = map(analogRead(34) - 1890, -2000, 2000, -100, 100);
roll = map(analogRead(35) - 1910, -2000, 2000, -100, 100);
throttle = map(analogRead(33) - 1910, -2000, 2000, -100, 100);
yaw = map(analogRead(32) - 1910, -2000, 2000, -100, 100);
sprintf(cmd, "rc %d %d %d %d", roll, pitch, throttle, yaw);
TelloCommand(cmd);
}
if (val == HIGH) {
TelloCommand("land");
timer = millis() + 1000;
state = 0;
Serial.println("land");
}
break;
}
delay(200);
}

using arduino tone function on two pin at the same time

I'm trying to make an smart car with Arduino Mega, and I need to turn both of the back wheels on for an specific time sometimes. I've been told that I can set a "digital HIGH" time using tone, But as I need them to work in a same time, Is there a way to set tone for two pins in one line or something to do instead?
Thanks for your help.
#include <Servo.h>
/////////////////////
Servo servo;
/////////////////////
int trig = 12;
int echo = 13;
long duration;
int distance;
int dist_right;
int dist_left;
int ang = 90;
unsigned int value = 255;
unsigned long tone_time = 3000;
float forward_time;
/////////////////////
int ena = 35;
int in1 = 7;
int in2 = 6;
int in3 = 5;
int in4 = 4;
int enb = 47;
/////////////////////
void setup()
{
Serial.begin(9600);
servo.attach(22);
pinMode(trig, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(ena, OUTPUT);
pinMode(enb, OUTPUT);
pinMode(echo, INPUT);
digitalWrite(ena, HIGH);
digitalWrite(enb, HIGH);
}
void loop() {
servo.write(90);
distance = dist();
if(distance<=15)
{
for(ang;ang>=0;ang-=2)
{
servo.write(ang);
delay(30);
}
dist_right = dist();
Serial.println(dist_right);
for(ang;ang<=180;ang+=2)
{
servo.write(ang);
delay(30);
}
dist_left = dist();
Serial.println(dist_left);
for(ang;ang>=90;ang-=2)
{
servo.write(ang);
delay(30);
}
if(dist_right>=dist_left)
{
tone(in3, value, tone_time);
}
else if(dist_right<dist_left)
{
tone(in1, value, tone_time);
}
servo.write(90);
ang=90;
}
else{
forward_time=distance/25;
tone((in1,in3), value, forward_time);
}
}
int dist(){
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
duration = pulseIn(echo, HIGH);
distance= duration*0.034/2;
return distance;
}
I'm 99.99% sure, that your motors will not feel the time differencce if you turn them one by one. Try simplest case and you will see.
// Define your wheel control pins (use same as in your mega)
const int motor1Pin = 5;
const int motor1Pin = 6;
// somewhere in setup method
outputMode(motor1Pin, OUTPUT);
outputMode(motor2Pin, OUTPUT);
// Create function to turn motors and remember the time
unsigned long turnMotorsOn(int seconds) {
// turn motors ant return time when they should be stopped
return millis() + seconds * 1000;
}
// In you code check if it is time to turn off
if (millis() > timeWhenTurnMotorsOff) {
// turn them off
}

Ardunio HM-10 BLE Scanner development

I'm using an Ardunio uno with a HM-10 module. I am trying to scan for all beacons around me and then store the beacon names
#include <SPI.h>
#include <SoftwareSerial.h>
String inputTXT;
SoftwareSerial mySerial(10, 11); // RX, TX
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
//setup
mySerial.write("AT");
delay(100);
mySerial.write("AT+ROLE1"); // Master mode
delay(100);
mySerial.write("AT+IMME1"); //wait for a connection command before connecting
delay(100);
mySerial.write("AT+RESET");
delay(50);
}
void loop() {
delay(3000);
mySerial.write("AT+DISI?");
if (mySerial.available()) {
inputTXT = mySerial.readString();
Serial.println(inputTXT);
inputTXT = "";
}
}
I get this output repeated at every loop
OK+DISISOK+DISC:00000000:00000000000000000000000000000000:00000OK+DISISOK+DISCEOK+DISC:00000000:00000000000000000000000000000000:0000000000:38F9D379C9E5:-065OK+DISC:00000000:00000000000000000000000000000000:0000000000:4CAA0DE091B7:-066OK+DISC:00000000:00000000000000000000000000000000:0000000000:72363EC2C661:-084OK+DISC:4C000C0E:008D37DBECB6B76115D006C9B3FA1005:1B1C1E7B5B:76854777DBD7:-072OK+DISC:00000000:00000000000000000000000000000000:0000000000:38F55DEDE396:-079OK+DISC:00000000:00000000000000000000000000000000:0000000000:4D023B8ED54D:-083OK+DISC:00000000:00000000000000000000000000000000:0000000000:6B5DB3EB2
So i now want to save the 12 digit string before the final colon of each iteration such as '4CAA0DE091B7' can someone show me or advice me how i would go about doing that please?
Unfortunately, there is no regular expression for Arduino, so you need to do it manually.
#include <SPI.h>
#include <SoftwareSerial.h>
String inputTXT;
SoftwareSerial mySerial(10, 11); // RX, TX
void setup()
{
//setup
Serial.begin(9600);
mySerial.begin(9600);
mySerial.write("AT");
delay(100);
mySerial.write("AT+ROLE1"); // Master mode
delay(100);
mySerial.write("AT+IMME1"); //wait for a connection command before connecting
delay(100);
mySerial.write("AT+RESET");
delay(50);
}
void loop()
{
delay(3000);
mySerial.write("AT+DISI?");
if (mySerial.available())
{
inputTXT = mySerial.readString();
int pos = 0;
String result = "";
const String regx = "00000000:00000000000000000000000000000000:0000000000:";
const int regx_len = regx.length();
while ((pos = inputTXT.indexOf(regx, pos)) != -1)
{
// substring from <starting point> to >starting point + 12>
result = inputTXT.substring(pos + regx_len, pos + regx_len + 12);
Serial.println(result);
// move starter point to end of last result
pos = pos + regx_len + 12;
}
}
}

Teachable Robotic Arm coding error

I am working on a robotic arm that has six servos which is being controlled by an arduino uno. The servos are analog servos from adafruit and they are special in the sense that you can get feedback from the servos, i.e their actual position after you told it where to go. What I am working on is trying to adapt this example code to include six servos instead of one. https://github.com/adafruit/Feedback-Servo-Record-and-Play/blob/master/servo_recordplay.ino
Here is my code so far and its errors
// Example code for recording and playing back servo motion with a
// analog feedback servo
// http://www.adafruit.com/products/1404
#include <Servo.h>
#include <EEPROM.h>
#define CALIB_MAX 512
#define CALIB_MIN 100
#define SAMPLE_DELAY 25 // in ms, 50ms seems good
uint8_t recordButtonPin = 12;
uint8_t playButtonPin = 7;
uint8_t servo1Pin = 9;
uint8_t servo2Pin = 10;
uint8_t servo1FeedbackPin = A0;
uint8_t servo2FeedbackPin = A1;
uint8_t servo3FeedbackPin = A2;
uint8_t servo4FeedbackPin = A3;
uint8_t servo5FeedbackPin = A4;
uint8_t servo6FeedbackPin = A5;
uint8_t ledPin = 13;
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;
Servo servo6;
void setup() {
Serial.begin(9600);
pinMode(recordButtonPin, INPUT);
digitalWrite(recordButtonPin, HIGH);
pinMode(playButtonPin, INPUT);
digitalWrite(playButtonPin, HIGH);
pinMode(ledPin, OUTPUT);
Serial.println("Servo RecordPlay");
}
void loop() {
if (! digitalRead(recordButtonPin)) {
delay(10);
// wait for released
while (! digitalRead(recordButtonPin));
delay(20);
// OK released!
recordAllServos(servo1Pin, servo1FeedbackPin, recordButtonPin);
}
if (! digitalRead(playButtonPin)) {
delay(10);
// wait for released
while (! digitalRead(playButtonPin));
delay(20);
// OK released!
playAllServo(servo1Pin, playButtonPin);
}
}
void playAllServo(uint8_t servoPin, uint8_t buttonPin) {
uint16_t addr = 0;
Serial.println("Playing");
servo1.attach(servo1Pin);
while (digitalRead(buttonPin)) {
uint8_t x = EEPROM.read(addr);
Serial.print("Read EE: "); Serial.print(x);
if (x == 255) break;
// map to 0-180 degrees
x = map(x, 0, 254, 0, 180);
Serial.print(" -> "); Serial.println(x);
servo1.write(x);
delay(SAMPLE_DELAY);
addr++;
if (addr == 512) break;
}
Serial.println("Done");
servo1.detach();
delay(250);
}
void recordAllServos(uint8_t servoPin, uint8_t buttonPin) {
uint16_t addr = 0;
Serial.println("Recording");
digitalWrite(ledPin, HIGH);
pinMode(servo1FeedbackPin, INPUT);
pinMode(servo2FeedbackPin, INPUT);
pinMode(servo3FeedbackPin, INPUT);
pinMode(servo4FeedbackPin, INPUT);
pinMode(servo5FeedbackPin, INPUT);
pinMode(servo6FeedbackPin, INPUT);
while (digitalRead(buttonPin))
{
readServo(servo1FeedbackPin);
readServo(servo2FeedbackPin);
readServo(servo3FeedbackPin);
readServo(servo4FeedbackPin);
readServo(servo5FeedbackPin);
readServo(servo6FeedbackPin);
if (addr > 506) break;
delay(SAMPLE_DELAY);
}
if (addr != 1024) EEPROM.write(addr, 255);
digitalWrite(ledPin, LOW);
Serial.println("Done");
delay(250);
}
void readAllServo(uint8_t analogPin)
{
uint16_t a = analogRead(analogPin);
Serial.print("Read analog pin "); Serial.print(analogPin); Serial.print(": ");
Serial.print(a);
if (a < CALIB_MIN) a = CALIB_MIN;
if (a > CALIB_MAX) a = CALIB_MAX;
a = map(a, CALIB_MIN, CALIB_MAX, 0, 254);
Serial.print(" -> "); Serial.println(a);
EEPROM.write(addr, a);
addr++;
}
The errors that I am getting from the arduino ide are
Teachable_Arm_Mark.cpp: In function 'void loop()':
Teachable_Arm_Mark:10: error: too many arguments to function 'void recordAllServos(uint8_t, uint8_t)'
Teachable_Arm_Mark:49: error: at this point in file
Teachable_Arm_Mark.cpp: In function 'void recordAllServos(uint8_t, uint8_t)':
Teachable_Arm_Mark:100: error: 'readServo' was not declared in this scope
Teachable_Arm_Mark.cpp: In function 'void readAllServo(uint8_t)':
Teachable_Arm_Mark:127: error: 'addr' was not declared in this scope
Any help much appreciated
Thanks
In void loop you have
recordAllServos(servo1Pin, servo1FeedbackPin, recordButtonPin);
but recordAllServos is declared as
void recordAllServos(uint8_t servoPin, uint8_t buttonPin)
Hence too many arguments 3 vs 2...