Arduino problems - c++

I have a problem. I'm working with Arduino but i'm not good with C++ and i take a error. I work on active security system. I used to motion sensor, RFID, embedded clock and wifi modele. But i say, i take a error Can you help me ?
#include <SoftwareSerial.h>
#include <MFRC522.h>
#include <SPI.h>
#include <virtuabotixRTC.h>
String agAdi = "";
String agSifresi = "";
int rxPin = 3;
int txPin = 5;
int RST_PIN = 9;
int SS_PIN = 10;
int CLK_PIN = 6;
int DAT_PIN = 7;
int PST_PIN = 4;
int buzzerPin = 2;
int pirPin = 8;
int hareket;
int tarih;
int saat;
String ip = "";
MFRC522 rfid(SS_PIN, RST_PIN);
byte ID[4] = {47, 219, 106, 81};
byte okunandeger[4];
virtuabotixRTC myRTC(CLK_PIN, DAT_PIN, RST_PIN);
SoftwareSerial esp(rxPin, txPin);
void setup() {
pinMode(pirPin, INPUT);
pinMode(buzzerPin, OUTPUT);
myRTC.setDS1302Time(00, 57, 22, 7, 23, 1, 2022);
Serial.begin(9600);
SPI.begin();
rfid.PCD_Init();
Serial.println("Started");
esp.begin(115200);
esp.println("AT");
Serial.println("AT Yollandı");
while (!esp.find("OK")) {
esp.println("AT");
Serial.println("ESP8266 Bulunamadı.");
}
Serial.println("OK Komutu Alındı");
esp.println("AT+CWMODE=1");
while (!esp.find("OK")) {
esp.println("AT+CWMODE=1");
Serial.println("Ayar Yapılıyor....");
}
Serial.println("Client olarak ayarlandı");
Serial.println("Aga Baglaniliyor...");
esp.println("AT+CWJAP=\"" + agAdi + "\",\"" + agSifresi + "\"");
while (!esp.find("OK"))
;
Serial.println("Aga Baglandi.");
delay(1000);
}
void loop() {
saatModulu();
rfidGiris();
byte okunankart[4] = rfid.uid.uidByte[];
int saat = String(myRTC.hours + ":" + myRTC.minutes + ":" + myRTC.seconds);
int tarih = String(myRTC.dayofmonth + ":" + myRTC.month + ":" + myRTC.year);
espFonk();
rfidCikis();
okunankart = rfid.uid.uidByte[];
int saat = String(myRTC.hours + ":" + myRTC.minutes + ":" + myRTC.seconds);
int tarih = String(myRTC.dayofmonth + ":" + myRTC.month + ":" + myRTC.year);
espFonk();
}
void espFonk() {
esp.println("AT+CIPSTART=\"TCP\",\"" + ip + "\",80");
if (esp.find("Error")) {
Serial.println("AT+CIPSTART Error");
}
String veri = "GET https://api.thingspeak.com/update?api_key=";
veri += "&field1=";
veri += String(okunankart);
veri += "&field2=";
veri += String(saat);
veri += "&field3=";
veri += String(tarih);
veri += "\r\n\r\n";
esp.print("AT+CIPSEND=");
esp.println(veri.length() + 2);
delay(2000);
if (esp.find(">")) {
esp.print(veri);
Serial.println(veri);
Serial.println("Veri gonderildi.");
delay(1000);
}
Serial.println("Baglantı Kapatildi.");
esp.println("AT+CIPCLOSE");
delay(1000);
}
void rfidGiris() {
if (!rfid.PICC_IsNewCardPresent()) return;
if (!rfid.PICC_ReadCardSerial()) return;
if (rfid.uid.uidByte[0] == ID[0] && rfid.uid.uidByte[1] == ID[1] &&
rfid.uid.uidByte[2] == ID[2] && rfid.uid.uidByte[3] == ID[3]) {
Serial.println("Güvenlik Sistemi Aktif!");
ekranaYazdir();
hareketSensor();
}
else {
Serial.print("Yetkisiz Kart");
ekranaYazdir();
melodi(500);
delay(500);
melodi(500);
}
rfid.PICC_HaltA();
}
void ekranaYazdir() {
Serial.print("ID Numarası : ");
for (int sayac = 0; sayac < 4; sayac++) {
Serial.print(rfid.uid.uidByte[sayac]);
Serial.print(" ");
}
}
void saatModulu() {
myRTC.updateTime();
Serial.print("Tarih / Saat: ");
Serial.print(myRTC.dayofmonth);
Serial.print("/");
Serial.print(myRTC.month);
Serial.print("/");
Serial.print(myRTC.year);
Serial.print(" ");
Serial.print(myRTC.hours);
Serial.print(":");
Serial.print(myRTC.minutes);
Serial.print(":");
Serial.print(myRTC.seconds);
delay(1000);
}
void hareketSensor() {
hareket = digitalRead(pirPin);
if (Hareket == HIGH) {
melodi(5000);
}
}
int melodi(int dly) {
tone(buzzerPin, 832);
delay(dly);
noTone(buzzerPin);
delay(dly);
}
void rfidCikis() {
if (!rfid.PICC_IsNewCardPresent()) return;
if (!rfid.PICC_ReadCardSerial()) return;
if (rfid.uid.uidByte[0] == ID[0] && rfid.uid.uidByte[1] == ID[1] &&
rfid.uid.uidByte[2] == ID[2] && rfid.uid.uidByte[3] == ID[3]) {
Serial.println("Güvenlik Sistemi Deaktif!");
ekranaYazdir();
hareket = LOW;
}
else {
Serial.print("Yetkisiz Kart");
ekranaYazdir();
melodi(500);
delay(500);
melodi(500);
}
rfid.PICC_HaltA();
}
This is error:
main.ino:166:8: note: suggested alternative: 'hareket'
exit status 1
expected primary-expression before ']' token
And Arduino IDE lighting
okunankart = rfid.uid.uidByte[];

For starters arrays do not have the assignment operator
okunankart = rfid.uid.uidByte[];
and in any case the expression with empty brackets is wrong.
The initializer in this declaration
byte okunankart[4] = rfid.uid.uidByte[];
is also wrong.
You need to copy elements from one array in another array for example using the standard algorithm std::copy or the C function memcpy.

Related

Arduino compiler failing with error code - invalid types 'int[int]' for array subscript

Quite a bit of untested code but the only thing really concerning me at the moment is my DISPLAY variable. I don't see how it is much different than my FONT array (which works fine) yet DISPLAY is the one that gets 'invalid types 'int[int]' for array subscript' I should be able to index an array of integers with integers (at least I have been with FONT).
#include <WiFiNINA.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include "font.h"
const int PIXEL_WIDTH = 30;
const int PIXEL_HEIGHT = 5;
int DISPLAY[PIXEL_HEIGHT][PIXEL_WIDTH] = {};
bool MILI_TIME = false;
String FORMATTING[2] = {"LONGS","SHORTH:LONGM"};
char ssid[] = "REMOVED"; // your network SSID (name) between the " "
char pass[] = "REMOVED"; // your network password between the " "
int keyIndex = 0; // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS; // connection status
WiFiServer server(80); // server socket
WiFiClient client = server.available();
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
int ledPin = LED_BUILTIN;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
while (!Serial); // dont do anything until there is a serial connection
enable_WiFi();
connect_WiFi();
server.begin();
printWifiStatus();
timeClient.begin();
timeClient.setTimeOffset(-14400);
}
void loop() {
timeClient.update();
client = server.available();
if (client) {
printWEB();
}
preRender();
}
void preRender(){
String FILLED_FORMATTING[2] = FORMATTING;
for(int i=0;i<2;i++){
FILLED_FORMATTING[i].replace("LONGH",leadWithZero(timeHours()));
FILLED_FORMATTING[i].replace("LONGM",leadWithZero(timeMinutes()));
FILLED_FORMATTING[i].replace("LONGS",leadWithZero(timeSeconds()));
FILLED_FORMATTING[i].replace("SHORTH",timeHours());
FILLED_FORMATTING[i].replace("SHORTM",timeMinutes());
FILLED_FORMATTING[i].replace("SHORTS",timeSeconds());
}
int x = 0;
for(int i=0;i<FILLED_FORMATTING[0].length();i++){
int c_ID = charID(FILLED_FORMATTING[0][i]);
if(c_ID < 38){
for(int j=0;j<5;j++){
DISPLAY[j][x] = FONT[c_ID][j][0];
x += 1;
DISPLAY[j][x] = FONT[c_ID][j][1];
x += 1;
DISPLAY[j][x] = FONT[c_ID][j][2];
x += 1;
if(i != FILLED_FORMATTING[0].length()){
DISPLAY[j][x] = 0;
x += 1;
}
}
}
}
x = PIXEL_WIDTH-1;
for(int i=FILLED_FORMATTING[1].length()-1;i>=0;i--){
int c_ID = charID(FILLED_FORMATTING[1][i]);
if(c_ID < 38){
for(int j=0;j<5;j++){
DISPLAY[j][x] = FONT[c_ID][j][0];
x -= 1;
DISPLAY[j][x] = FONT[c_ID][j][1];
x -= 1;
DISPLAY[j][x] = FONT[c_ID][j][2];
x -= 1;
if(i != 0){
DISPLAY[j][x] = 0; //<----------- compiler error here, and ofc all instances above
x -= 1;
}
}
}
}
}
int charID(char c){
for(int i=0;i<FONT_ID.length();i++){
if(FONT_ID[i] == c){
return i;
}
}
return 40;
}
String timeHours(){
if(MILI_TIME){
return String(timeClient.getHours());
}
else{
return convFromMili(timeClient.getHours());
}
}
String timeMinutes(){
return String(timeClient.getMinutes());
}
String timeSeconds(){
return String(timeClient.getSeconds());
}
String leadWithZero(String t){
if(t.length() < 2){
t = "0"+t;
return t;
}
}
String convFromMili(int t){
if(t > 12){
t -= 12;
}else if(t == 0){
t += 12;
}
String ts = String(t);
return ts;
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
Serial.print("To see this page in action, open a browser to http://");
Serial.println(ip);
}
void enable_WiFi() {
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
String fv = WiFi.firmwareVersion();
if (fv < "1.0.0") {
Serial.println("Please upgrade the firmware");
}
}
void connect_WiFi() {
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
delay(10000);
}
}
void printWEB() {
if (client) { // if you get a client,
Serial.println("new client"); // print a message out the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
//create the buttons
client.print(WiFi.getTime());
client.print("<br>");
client.print(timeClient.getEpochTime());
client.print("<br>");
client.print(timeClient.getFormattedTime());
client.print("<br>");
client.print(timeClient.getDay());
client.print("<br>");
client.print(timeClient.getHours());
client.print("<br>");
client.print(timeClient.getMinutes());
client.print("<br>");
client.print(timeClient.getSeconds());
client.print("<br>");
client.print("<div style=\"display:block;padding-left:calc(50% - 150px);margin-bottom:50px\"><div style=\"background-color:#e3e3e3;width:300px;height:120px;font-size:50px;text-align:center;padding-top:50px\">ON</div></div>");
client.print("<div style=\"display:block;padding-left:calc(50% - 150px)\"><div style=\"background-color:#e3e3e3;width:300px;height:120px;font-size:50px;text-align:center;padding-top:50px\">OFF</div></div>");
// The HTTP response ends with another blank line:
client.println();
// break out of the while loop:
break;
}
else { // if you got a newline, then clear currentLine:
currentLine = "";
}
}
else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
if (currentLine.endsWith("GET /H")) {
digitalWrite(ledPin, HIGH);
}
if (currentLine.endsWith("GET /L")) {
digitalWrite(ledPin, LOW);
}
if (currentLine.endsWith("%VAL")) {
// Trim 'GET /' and '%VAL'
currentLine.remove(0,5);
currentLine.remove(currentLine.indexOf("%"),4);
Serial.println(currentLine);
Serial.println();
}
}
}
// close the connection:
client.stop();
Serial.println("client disconnected");
}
}
font.h:
int FONT[37][5][3] = {{{1,1,1},{1,0,1},{1,0,1},{1,0,1},{1,1,1},},{{1,1,0},{0,1,0},{0,1,0},{0,1,0},{1,1,1},},{{1,1,1},{0,0,1},{1,1,1},{1,0,0},{1,1,1},},{{1,1,1},{0,0,1},{1,1,1},{0,0,1},{1,1,1},},{{1,0,1},{1,0,1},{1,1,1},{0,0,1},{0,0,1},},{{1,1,1},{1,0,0},{1,1,1},{0,0,1},{1,1,1},},{{1,1,1},{1,0,0},{1,1,1},{1,0,1},{1,1,1},},{{1,1,1},{0,0,1},{0,0,1},{0,0,1},{0,0,1},},{{1,1,1},{1,0,1},{1,1,1},{1,0,1},{1,1,1},},{{1,1,1},{1,0,1},{1,1,1},{0,0,1},{1,1,1},},{{1,1,1},{1,0,1},{1,1,1},{1,0,1},{1,0,1},},{{1,1,0},{1,0,1},{1,1,0},{1,0,1},{1,1,0},},{{1,1,1},{1,0,0},{1,0,0},{1,0,0},{1,1,1},},{{1,1,0},{1,0,1},{1,0,1},{1,0,1},{1,1,0},},{{1,1,1},{1,0,0},{1,1,1},{1,0,0},{1,1,1},},{{1,1,1},{1,0,0},{1,1,1},{1,0,0},{1,0,0},},{{1,1,1},{1,0,0},{1,0,1},{1,0,1},{1,1,1},},{{1,0,1},{1,0,1},{1,1,1},{1,0,1},{1,0,1},},{{1,1,1},{0,1,0},{0,1,0},{0,1,0},{1,1,1},},{{0,0,1},{0,0,1},{0,0,1},{1,0,1},{1,1,1},},{{1,0,1},{1,0,1},{1,1,0},{1,0,1},{1,0,1},},{{1,0,0},{1,0,0},{1,0,0},{1,0,0},{1,1,1},},{{1,1,1},{1,1,1},{1,0,1},{1,0,1},{1,0,1},},{{1,1,0},{1,0,1},{1,0,1},{1,0,1},{1,0,1},},{{1,1,1},{1,0,1},{1,0,1},{1,0,1},{1,1,1},},{{1,1,1},{1,0,1},{1,1,1},{1,0,0},{1,0,0},},{{1,1,1},{1,0,1},{1,0,1},{1,1,0},{0,0,1},},{{1,1,1},{1,0,1},{1,1,0},{1,0,1},{1,0,1},},{{0,1,1},{1,0,0},{0,1,0},{0,0,1},{1,1,0},},{{1,1,1},{0,1,0},{0,1,0},{0,1,0},{0,1,0},},{{1,0,1},{1,0,1},{1,0,1},{1,0,1},{1,1,1},},{{1,0,1},{1,0,1},{1,0,1},{0,1,0},{0,1,0},},{{1,0,1},{1,0,1},{1,0,1},{1,1,1},{1,1,1},},{{1,0,1},{1,0,1},{0,1,0},{1,0,1},{1,0,1},},{{1,0,1},{1,0,1},{1,1,1},{0,0,1},{1,1,1},},{{1,1,1},{0,0,1},{0,1,0},{1,0,0},{1,1,1},},{{0,0,0},{0,1,0},{0,0,0},{0,1,0},{0,0,0}}};
String FONT_ID = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ:";
did check -
int D[5][30];
void setup() {
}
void loop() {
D[0][0] = 0;
}
and of course its fine, so I'm just wondering where I went wrong in the mess above?
and yes there's a good bit of mess/debugging stuff
Simple solution... don't use DISPLAY as a variable it seems. Changing to DISPLAY_ fixed it, figured the variable was defined as a normal integer somewhere... just not in my code.

How would I optimize this code, so my arduino nodemcu won't crash after a while

I have this IOT project which controls my chameleon's terrarium. It has a relay connected to D5 and D6 pins to control lights. My Nodemcu 0.9 stops working after ~ 20 hours (lights are off at this time and won't turn back on, I always have to reset my nodemcu).
I've been reading stack and found that strings use a lot of heap memory, so I removed all of them but the problem still persists.
main.cpp
#include <FirebaseArduino.h>
#include <NTPClient.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiUdp.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include "credentials.h"
#define DHTPIN D7
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
double previousTemperature = 0.0;
double previousHumidity = 0.0;
boolean overRide = false;
boolean uvStatus;
boolean irStatus;
int baskingTime;
int startHour;
int startMinute;
int endTime;
int startTimeMinutes;
int endTimeMinutes;
const int uvPin = D5;
const int irPin = D6;
const char *ssid = "Waifas 2.4";
const char *password = "lsmustud";
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", 7200, 60000);
void setup()
{
pinMode(uvPin, OUTPUT);
pinMode(irPin, OUTPUT);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
}
timeClient.begin();
//initialize firebase
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
Firebase.setBool(OVERRIDE, false);
baskingTime = Firebase.getInt(BASKING_TIME);
startHour = Firebase.getInt(START_HOUR);
startMinute = Firebase.getInt(START_MINUTE);
uvStatus = Firebase.getBool(UV_STATUS);
irStatus = Firebase.getBool(IR_STATUS);
startTimeMinutes = startHour * 60 + startMinute;
endTimeMinutes = startHour * 60 + startMinute + baskingTime * 60;
Firebase.stream(DB_PATH);
dht.begin();
}
void loop()
{
timeClient.update();
int clientHour = timeClient.getHours();
int clientMinute = timeClient.getMinutes();
int currentTimeMinutes = clientHour * 60 + clientMinute;
if (Firebase.failed())
{
delay(1000);
Firebase.stream(DB_PATH);
return;
}
if (Firebase.available())
{
FirebaseObject event = Firebase.readEvent();
String eventPath = event.getString("path");
eventPath.toLowerCase();
if (eventPath == "/override")
{
overRide = Firebase.getBool(OVERRIDE);
}
if (eventPath == "/uvstatus")
{
uvStatus = Firebase.getBool(UV_STATUS);
}
if (eventPath == "/irstatus")
{
irStatus = Firebase.getBool(IR_STATUS);
}
if (eventPath == "/baskingtime" || eventPath == "/starthour" || eventPath == "/startminute")
{
baskingTime = Firebase.getInt(BASKING_TIME);
startHour = Firebase.getInt(START_HOUR);
startMinute = Firebase.getInt(START_MINUTE);
endTime = startHour + baskingTime;
if (endTime >= 24)
{
endTime = endTime - 24;
}
startTimeMinutes = startHour * 60 + startMinute;
endTimeMinutes = endTime * 60 + startMinute;
}
}
if (!overRide)
{
if (startTimeMinutes <= currentTimeMinutes && currentTimeMinutes <= endTimeMinutes)
{
if (!uvStatus)
{
Firebase.setBool(UV_STATUS, true);
uvStatus = true;
}
if (!irStatus)
{
Firebase.setBool(IR_STATUS, true);
irStatus = true;
}
}
else
{
if (uvStatus)
{
Firebase.setBool(UV_STATUS, false);
uvStatus = false;
}
if (irStatus)
{
Firebase.setBool(IR_STATUS, false);
irStatus = false;
}
}
}
if (uvStatus)
{
digitalWrite(uvPin, LOW);
}
else
{
digitalWrite(uvPin, HIGH);
}
if (irStatus)
{
digitalWrite(irPin, LOW);
}
else
{
digitalWrite(irPin, HIGH);
}
double t = dht.readTemperature();
double h = dht.readHumidity();
if (isnan(h) || isnan(t))
{
}
else
{
if (h != previousHumidity)
{
previousTemperature = h;
Firebase.setFloat(HUMIDITY, h);
}
if (t != previousTemperature)
{
previousTemperature = t;
Firebase.setFloat(TEMPERATURE, t);
}
}
delay(1000);
}
Credentials.h
//define firebase login
#define FIREBASE_HOST "someDatabase.someGoogleLink.com"
#define FIREBASE_AUTH "someSecret"
//db path
#define DB_PATH "/control/"
#define BASKING_TIME "/control/baskingTime"
#define IR_STATUS "/control/irStatus"
#define UV_STATUS "/control/uvStatus"
#define START_MINUTE "/control/startMinute"
#define START_HOUR "/control/startHour"
#define OVERRIDE "/control/overRide"
#define TEMPERATURE "/temperature"
#define HUMIDITY "/humidity"
Would you help me to fix this? I'm not a programmer, just a hobbyist, so it is really hard for me to figure this out.

WDT reset cause 4 after reinstalling Arduino

I try to control my LED strip with an Android app.
I reinstalled the Arduino IDE since I didn't like the Windows-store version. The serial monitor broke for whatever reason so I switched back to the Windows-store version.
But now I'm getting a WDT reset error, which I can't seem to overcome.
I'm also using Adafruit_NeoPixel and wherever I put "ledstrip.begin" or "ledstrip.show" seems to break it.
#include <ESP8266WiFi.h>
#include <Adafruit_NeoPixel.h>
byte RED = 14;
byte GREEN = 12;
byte BLUE = 13;
byte PIN_COUNT = 20;
Adafruit_NeoPixel ledstrip = Adafruit_NeoPixel(PIN_COUNT, 6, NEO_RGB + NEO_KHZ800); //(num of leds, )
WiFiServer server(port);
void setup()
{
Serial.begin(115200);
delay(10);
ledstrip.begin();
Serial.print("Connecting to: ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
byte tries;
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
tries++;
Serial.println(".");
if (tries == 5)
{
Serial.println("Connection failed");
return;
}
}
Serial.println("\nWiFi connected");
server.begin();
Serial.println("Server URL: ");
Serial.print(WiFi.localIP());
}
void print_tokens(char* strings)
{
byte index = sizeof(strings);
for (int n = 0; n < index; n++)
{
Serial.println(strings[n]);
}
}
char *tokenize_request(String request)
{
char requestArray[1024];
char *ptr = NULL;
byte index = 0;
char *strings[10];
strcpy(requestArray, request.c_str());
ptr = strtok(requestArray, ";");
while (ptr != NULL) {
strings[index] = ptr;
index++;
ptr = strtok(NULL, ";");
}
return *strings;
}
void loop()
{
WiFiClient responder = server.available();
if (!responder)
{
return;
}
while (!responder.available())
{
delay(1);
}
String request = responder.readStringUntil('#');
char *strings[10] = {tokenize_request(request)};
print_tokens(*strings);
int mode = atoi(strings[0]);
int R = atoi(strings[1]);
int G = atoi(strings[2]);
int B = atoi(strings[3]);
if (mode == 1)
{
ledstrip.setPixelColor(5, R, G, B);
}
if (mode == 0)
{
ledstrip.setPixelColor(5, 0, 0, 0);
}
ledstrip.show();
delay(1);
Serial.println("------------------------------------------------------------------");
}
Serial monitor output:

Serial.print uint64_t in Arduino

I use Arduino UNO (Arduino 1.8.3).
This is my code:
void setup() {
Serial.begin(115200);
}
void loop() {
uint64_t num = 9223372036854775807;
Serial.print(num);
delay(1000);
}
This is error message:
exit status 1
call of overloaded 'print(uint64_t&)' is ambiguous
How do I solve this error?
This is how I would do it. It's messy but it gets the job done.
Output from serial console: 9223372036854775807
void print_uint64_t(uint64_t num) {
char rev[128];
char *p = rev+1;
while (num > 0) {
*p++ = '0' + ( num % 10);
num/= 10;
}
p--;
/*Print the number which is now in reverse*/
while (p > rev) {
Serial.print(*p--);
}
}
A bit better than printing char by char as suggested somewhere else is to actually build the buffer in reverse and then print it at once.
#include <stdint.h>
char* str( uint64_t num ) {
static char buf[22];
char* p = &buf[sizeof(buf)-1];
*p = '\0';
do {
*--p = '0' + (num%10);
num /= 10;
} while ( num > 0 );
return p;
}
void setup() {
uint64_t num = 9223372036854775807;
Serial.print( str(num) );
}

Unexpected Program Stoppage on Arduino

I have an issue where my Arduino program hangs for no reason. I run my program, and at some undetermined point, the Serial Monitor stops printing output. Here's what I've tested so far:
In my "com.init_drone()" method, I've commented everything out except for the last line, which signals that the method returned. When I do this, my program hangs somewhere else but still it doesn't get to the infinite while loop.
I've been outputting memory usage, and I'm getting numbers no lower than 450 -- this tells me that I'm not using an absurd amount of memory.
I've tried removing the Timer1 instantiation, interrupt attach/detach, bu that has had no effect on the program.
My .ino file(and Command) is located here for anyone that wants a fuller picture and doesn't want to scroll through all this code I'm going to post below.
Here's my log output so far. Notice the truncation!:
AT&F
AT+NMAC=00:1d:c9:10:39:6f
AT+WM=0
AT+NDHCP=1
AT+WA=ardrone_279440
AT+NCUDP=192.168.1.1,5556
S0AT*CONFIG=1,"general:navdata_demo","TRUE"
EAT*CONFIG=1,"general:navdata_demo","TRUE"
638
S0AT*CONFIG=2,"control:altitude_max","2000"
EAT*CONFIG=2,"control:altitude_max","2000"
638
S0AT*CONFIG=3,"control:euler_angle_max","0.35"
EAT*CONFIG=3,"control:euler_angle_max","0.35"
586
S0AT*CONFIG=4,"control:outdoor","FALSE"
EAT*CONFIG=4,"control:outdoor","FALSE"
635
S0AT*CONFIG=5,"control:flight_without_shell","FALSE"
EAT*CONFIG=5,"control:flight_without_shell","FALSE"
574
S0AT*CTRL=6,4,0
EAT*CTRL=6,4,0
629
S0AT*CTRL=7,0,0
EAT*CTRL=7,0,0
629
S0AT*CTRL=8,4,0
EAT*CTRL=8,4,0
629
S0AT*COMWDG=9
EAT*COMWDG=9
629
S0AT*COMWDG=10
EAT*COMWDG=10
629
S0AT*COMWDG=11
EAT*COMWDG=11
629
S0AT*COMWDG=12
EAT*COMWDG=12
629
S0AT*COMWDG=13
EAT*COMWDG=13
629
S0AT*FTRIM=14
EAT*FTRIM=14
629
Here is my .ino file:
#include "Command.h"
#include "Streaming.h"
int debug = 1;
extern ring_buffer rx_buf;
extern resultint_ resultint;
Command com;
int sequenceNumber = 1;
String atcmd = "";
#include "TimerOne.h"
#define LEDpin 13
void setup()
{
PCsrl.begin(9600);
com.start_wifi_connection();
com.drone_is_init = com.init_drone();
Timer1.initialize(COMWDG_INTERVAL_USEC);
Timer1.attachInterrupt(watchdog_timer);
}
void watchdog_timer() {
com.sendwifi(com.makeComwdg());
}
void loop()
{
if (com.drone_is_init == 0) {
if (debug) {
// never use three ! together in arduino code
PCsrl.println("Drone wasn't initlized before loop() was called. Initalizing now.\r\n");
}
} else {
com.drone_takeoff();
com.drone_takeoff();
com.sendwifi(com.makePcmd(1,0,0,0,0));
com.sendwifi(com.makePcmd(1,0,0,0,0));
delay(5000);
com.moveForward(1);
com.moveRotate(180);
com.moveForward(1);
com.moveRotate(180);
delay(500);
com.drone_landing();
com.drone_landing();
delay(500);
//end of program
Timer1.detachInterrupt();
PCsrl.println("Program finished");
while (1){};
}
}
And my Command.cpp
#ifndef GAINSPAN
#define GAINSPAN
#include "Command.h"
extern int sequenceNumber;
extern int debug;
ring_buffer rx_buf= {{0}, 0, 0};
resultint_ resultint;
Command::Command()
{
at = "";
command = "";
s2ip_running = 0;
drone_is_init = 0;
drone_is_hover = 0;
emergency = 0;
}
void Command::sendwifi(String s) {
WIFIsrl.write(27); //esc
WIFIsrl.print("S0"); //choose connection CID 0
WIFIsrl.print(s);
WIFIsrl.write(27);
WIFIsrl.print("E");
if(debug) PCsrl.println(s);
WIFIsrl.println(memoryTest());
}
int Command::start_wifi_connection()
{
WIFIsrl.begin(9600);
WIFIsrl.println("");
WIFIsrl.println("AT&F");
//WIFIsrl.println("ATE0"); //turn off echo
WIFIsrl.print("AT+NMAC=00:1d:c9:10:39:6f\r"); //set MAC address
WIFIsrl.println("AT+WM=0");
WIFIsrl.println("AT+NDHCP=1");
/* drone's network profile, change if needed*/
WIFIsrl.println("AT+WA=ardrone_279440");
WIFIsrl.println("AT+NCUDP=192.168.1.1,5556");
readARsrl();
delay(3000); //need 3 seconds for connection to establish
return 0;
}
String Command::makeComwdg()
{
at = "AT*COMWDG=";
command = at + getSequenceNumber() + "\r\n";
return command;
}
void Command::sendComwdg_t(int msec)
{
for (int i = 0; i < msec; i+=20) {
sendwifi(makeComwdg());
delay(20);
}
}
void Command::sendFtrim()
{
at = "AT*FTRIM=";
command = at + getSequenceNumber() + "\r\n";
sendwifi(command);
}
void Command::sendConfig(String option, String value)
{
at = "AT*CONFIG=";
command = at + getSequenceNumber() + ",\"" + option + "\",\"" + value + "\"\r\n";
sendwifi(command);
}
void Command::sendRef(flying_status fs)
{
at = "AT*REF=";
if(fs == TAKEOFF){
command = at + getSequenceNumber() + ",290718208\r\n"; //takeoff
}
else if(fs == LANDING){
command = at + getSequenceNumber() + ",290717696\r\n"; //landing
} else if (fs == EMERGENCY_TOGGLE){
command = at + getSequenceNumber() + ",290717952\r\n"; //landing
}
// emergency -> 290717952
sendwifi(command);
}
void Command::send_control_commands(){
at = "AT*CTRL=";
sendwifi(at+getSequenceNumber()+",4,0\r\n");
sendwifi(at+getSequenceNumber()+",0,0\r\n");
sendwifi(at+getSequenceNumber()+",4,0\r\n");
}
void Command::drone_emergency_reset()
{
at = "AT*REF=";
command = at + getSequenceNumber() + ",290717952\r\n";
sendwifi(command);
}
/** Movement functions **/
int Command::moveForward(float distanceInMeters)
{
float i = 0;
String moveForward = makePcmd(1, 0, -.855, 0, 0);
delay(1000*distanceInMeters);
sendPcmd(moveForward);
return 1;
}
int Command::moveRotate(float yawInDegrees)
{
int i = 0;
while (i < yawInDegrees) {
String stayRotate = makePcmd(1, 0, 0, 0, 0.17);
sendPcmd(stayRotate);
delay(150);
i += 8;
}
return 1;
}
String Command::makePcmd(int enable, float roll, float pitch, float gaz, float yaw)
{
at = "AT*PCMD=";
command = at + getSequenceNumber() + "," + enable + "," + fl2int(roll) + "," + fl2int(pitch) + "," + fl2int(gaz) + "," + fl2int(yaw) + "\r";
return command;
}
void Command::sendPcmd(String command)
{
previousCommand = command;
sendwifi(command);
}
void Command::sendPcmd(int enable, float roll, float pitch, float gaz, float yaw)
{
at = "AT*PCMD=";
command = at + getSequenceNumber() + "," + enable + "," + fl2int(roll) + "," + fl2int(pitch) + "," + fl2int(gaz) + "," + fl2int(yaw) + "\r";
sendwifi(command);
}
String Command::makeAnim(anim_mayday_t anim, int time)
{
at = "AT*ANIM=";
command = at + getSequenceNumber() + "," + anim + "," + time + "\r\n";
return command;
}
void Command::doLEDAnim(int animseq, int duration)
{
PCsrl << "calling LEDAnim" << endl;
at = "AT*LED=";
command = at + getSequenceNumber() + "," + animseq + ",1073741824," + duration + "\r\n";
sendwifi(command);
}
int Command::start_s2ip()
{
char temp;
//delay(20000); //wait for drone to start
readARsrl();
if (debug) {
PCsrl << "trying to start s2ip" << endl;
}
ARsrl.print("\r\n");
delay(500);
ARsrl.print("\r\n");
delay(500);
ARsrl << "cd ~" << endl;
if (debug) {
readARsrl();
}
delay(500);
ARsrl << "cd data/video/apps/" << endl;
delay(500);
ARsrl << "./s2ip.arm" << endl;
while ((int) temp != 2) {
temp = ARsrl.read();
if (temp == 2) {
PCsrl << "s2ip is running" << endl;
ARsrl << "bullshit\r\n"; //to fix a delay bug
break;
}
//PCsrl << "s2ip not running" << endl;
}
if (debug) {
while (ARsrl.available()) {
PCsrl.write(ARsrl.read());
}
}
return 1;
}
void Command::quit_s2ip()
{
ARsrl.println("EXIT");
while (ARsrl.available()) {
PCsrl.write(ARsrl.read());
}
}
int Command::init_drone()
{
sendConfig("general:navdata_demo","TRUE");
sendConfig("control:altitude_max","2000");
sendConfig("control:euler_angle_max","0.35");
sendConfig("control:outdoor","FALSE");
sendConfig("control:flight_without_shell","FALSE");
send_control_commands();
sendComwdg_t(90);
sendFtrim();
drone_emergency_reset(); //clear emergency flag
return 1;
}
int Command::drone_takeoff()
{
sendRef(TAKEOFF);
int i = 0;
return 1;
}
int Command::drone_hover(int msec)
{
int i = 0;
while (i < msec) {
sendwifi(makePcmd(1, 0, 0, 0, 0));
delay(100);
i += 100;
}
return 1;
}
int Command::drone_landing()
{
sendRef(LANDING);
return 1;
}
int Command::drone_move_up(int centimeter)
{
int i = 0;
while (i < centimeter) {
ARsrl << makePcmd(1, 0, 0, 0.6, 0);
delay(100);
i += 10;
}
return 1;
}
int Command::drone_move_down(int centimeter)
{
int i = 0;
while (i < centimeter) {
sendwifi(makePcmd(1, 0, 0, -0.5, 0));
delay(100);
i += 10;
}
return 1;
}
long Command::fl2int(float value)
{
resultint.i = 0;
if (value < -1 || value > 1) {
resultint.f = 1;
} else {
resultint.f=value;
}
return resultint.i;
}
void Command::readARsrl()
{
while (ARsrl.available()) {
if (debug) {
PCsrl.write(ARsrl.read());
}
}
}
//Memory test code from : http://www.faludi.com/2007/04/18/arduino-available-memory-test/
int Command::memoryTest() {
int byteCounter = 0; // initialize a counter
byte *byteArray; // create a pointer to a byte array
// More on pointers here: http://en.wikipedia.org/wiki/Pointer#C_pointers
// use the malloc function to repeatedly attempt
// allocating a certain number of bytes to memory
// More on malloc here: http://en.wikipedia.org/wiki/Malloc
while ( (byteArray = (byte*) malloc (byteCounter * sizeof(byte))) != NULL ) {
byteCounter++; // if allocation was successful, then up the count for the next try
free(byteArray); // free memory after allocating it
}
free(byteArray); // also free memory after the function finishes
return byteCounter; // send back the highest number of bytes successfully allocated
}
int Command::getSequenceNumber(){
return sequenceNumber++;
}
// Volatile, since it is modified in an ISR.
volatile boolean inService = false;
void SrlRead()
{
if (inService) {
PCsrl.println("timer kicked too fast");
return;
}
interrupts();
inService = true;
while(ARsrl.available()) {
unsigned char k = ARsrl.read();
store_char(k, &rx_buf);
}
inService = false;
}
void read_rx_buf()
{
while (rx_buf.tail != rx_buf.head) {
if (debug) {
PCsrl.write(rx_buf.buffer[rx_buf.tail]);
}
rx_buf.tail = (unsigned int) (rx_buf.tail+ 1) % SERIAL_BUFFER_SIZE;
}
}
inline void store_char(unsigned char c, ring_buffer *buffer)
{
int i = (unsigned int)(buffer->head + 1) % SERIAL_BUFFER_SIZE;
// if we should be storing the received character into the location
// just before the tail (meaning that the head would advance to the
// current location of the tail), we're about to overflow the buffer
// and so we don't write the character or advance the head.
if (i != buffer->tail) {
buffer->buffer[buffer->head] = c;
buffer->head = i;
}
else {
Serial.println("ring buffer is too small");
}
}
#endif
I know it sounds weird but, sometimes this happens when arduino is not getting enough power supply. Try connecting the arduino to a power source different from USB.
As soon as I started to put things in prog memory my program started to clear up it's hiccups. It seems it was a memory issue.
I had the same problem, but the issue was with Timer1.initialize(). Try this:
com.drone_is_init = com.init_drone();
Serial.println("One");
Serial.println("Two");
Serial.println("Three");
Timer1.initialize(COMWDG_INTERVAL_USEC);
Timer1.attachInterrupt(watchdog_timer);
Open serial monitor and see... it will show until "Two", and then Arduino will hang.
The issue was I calling some functions of the LiquidCrystal_I2C library, that need interrupt routines. Check if your timer ISR is using some interrupts. If so, you should move this code to another place in your project.