Wire.endTransmisson crashing when building Arduino Library - arduino

I am trying to have a constructor set up the pins on the external chip I am using to be outputs. To do so I need to do an I2C transaction.
Below is code that is currently in the constructor. With the Wire.endTransmissonline in the code, it crashes. With it omitted it works. But I feel that it should be in there and something else is going on. Is there something I am missing here?
HID::HID(){
Wire.begin();
I2Cflag = 1;
Wire.beginTransmission(0x41);
Wire.write(0x03);
Wire.write(0xF3);
Wire.endTransmission(); //When omitted, it works!
I2Cflag = 0;}

Related

trouble triggering relay with Bluetooth on an Arduini Mega

I'm planning to make a Bluetooth controlled relay. but i keep having problems triggering the relay with my Bluetooth entries.The data i write on my phone is displayed in the serial monitor but i just cant get the relay to turn off or on.i am using a single channel relay module and the HC-06 Bluetooth module.
can someone please tell me what the problem is?? this is driving me crazy. my Bluetooth module is connected to TX and RX respectively and my relay is connected to pin 13 of my arduino mega.
Thank You in advance!
#include <SoftwareSerial.h>
SoftwareSerial bt(1,0);
int RelayStateOn =0;
void setup() {
bt.begin(9600);
pinMode(13,OUTPUT);
}
char val=bt.read();
void loop() {
if(bt.available()) {
bt.println(val);
}
if(val==1) {
digitalWrite(13,HIGH);
}
else if(val==0) {
digitalWrite(13,LOW);
}
}
Firstly, you don't have a call to read from the bt in the loop anywhere. You have a call to bt.read() at global scope, so val gets a value that was read before anything was ready to read. So it most likely gets a -1.
You also have a call to avaialable before you have a call to print which implies that you don't understand what available is for. It tells if there is more available to read. So that should be around the line in loop that you're going to add that reads from bt not the one that prints to it.
To start with, where you have the bt.println(val), change that to val = bt.read().

How to use Serial as an interrupt for other functions

I'm making an LED control program (using FastLED, of course) and using Serial (with the Serial monitor) to control it. I just connected it via USB and for the most part it works just fine. However, I noticed with the long, flashing routines that I couldn't stop them and make the LEDs do something else. The flash routine in my code is very simple:
void flash(CRGB color, int count, int del){
for(int i = 0; i < count; i++){
if(pause){
break;
}
fillLeds(color.r, color.g, color.b);
milliDelay(del);
fillLeds(0,0,0);
milliDelay(del);
}
}
With fillLeds(r,g,b) being a for loop, looping through and setting all LEDs to a certain color, and milliDelay is just delay() using millis and not the delay() function.
I need to be able to pause not just this, but other functions as well (probably using break;) and then execute other code. It seems easy, right? Well, I've noticed that when I send a byte over Serial, it goes into this "queue," if you will, and then is sequentially read.
I can’t have this happen. I need the next byte entering Serial to activate some kind of event that pauses the other flash() function running, and then be used. I have implemented this like:
void loop()
{
if (Serial.available() > 0)
{
int x = Serial.read();
Serial.print(x);
handleRequest(x);
}
FastLED.show();
FastLED.delay(1000 / UPDATES_PER_SECOND);
}
Where handleRequest(x); is just a long switch statement with calls to the flash method, with different colors being used, etc.
How can I make the Arduino pause other loops whenever a new byte is received, instead of adding it to this "queue" to be acted upon later? If this is not possible thanks for reading anyway. I've tried using serialEvent() which doesn't appear to work.
I think you need two loops. You have one, which is your main loop, and you can add another (like a multi threading) with the TimerOne library. Like this:
Timer1.initialize(your desired delay in this loop);
Timer1.attachInterrupt(your desired function in this loop);
So maybe you can add an if statement with a variable in your second loop to prevent some function and update the variable in your first loop or something like that.
Presuming you want interrupt-like functionality when a new byte arrives:
Unfortunately, serialEvent() is not a true interrupt. It only runs at the end of loop(), if there is serial data available.
However, serialEvent() is just a function, and there isn't any reason why you can't call it in your code as often as you like. This is effectively polling for new serial data as often as possible. So, while your loops are running, call serialEvent() during your delays and handle the serial data there.
You may need to restructure your code to avoid recursion though. If flash calls serialEvent(), which calls flash, which calls serialEvent, etc... then you may end up overflowing the stack.

Problems on ESP8266 programming based on Arduino IDE for ESP8266

I decided to use Arduino IDE for ESP8266 to program on my ESP8266, targeting to read data (bytes array from a TTL camera). ESP8266-01 (8Mbits Flash ROM) has 8 pins so I decided to use GPIO16 (TXD) and GPIO2 (RXD) as SoftwareSerial pins to achieve this. But the ESP8266 printed an exception and I am not sure what happened to it.
So I have a few question about this crash.
I want to know whether I can run SoftwareSerial on ESP8266 (ESP8266 has 2 UARTs, Serial0 for Serial print and we could not send data through Serial1), so I decided to use SoftwareSerial.
I don't know what the exception info means for I can't understand assembly language.
I read documents on Github for Arduino IDE for ESP8266, but I don't understand well about the pin definitions on ESP8266 when programming with Arduino IDE. For example, when using GPIO16 (TXD) and GPIO2 (RXD) as SoftwareSerial, we might use Constructor SoftwareSerial Camera(int Pin1, int Pin2). I want to know what the corresponding Pin1 and Pin2 for GPIO2 and GPIO16) is. The document really confused me.
Here is my key code.
#include <Arduino.h>
#include "camera_VC0706.h"
#include <SoftwareSerial.h>
HTTPClient httpClient;
//define camera and bytes array, I am not sure whether the pins are correct or not
int rxPin=2;
int txPin=16;
SoftwareSerial cameraSerial(rxPin,txPin); //RX,TX
camera_VC0706 Camera = camera_VC0706(&cameraSerial);
//take photo
bool takePhoto() {
byte time=0;
writeRawSerial(F("<STATUS>WAITING</STATUS>"),true);
while(!Camera.begin(115200)) {
if(+time<=5){//try 5 times
writeRawSerial(F("."),false);
} else {
writeRawSerial("",true);
writeSerial(F("<STATUS>NO_CAMERA</STATUS>"));
return false;
}
}
writeRawSerial(F("<STATUS>CAMERA_ON</STATUS>"), false);
writeRawSerial(F("<VERSION>"), false);
writeRawSerial(Camera.getVersion(), false);
writeSerial(F("</VERSION>"));
Camera.setImageSize(VC0706_320x240);
if (!Camera.takePicture()) {
writeSerial(F("<STATUS>TAKE_PHOTO_FAIL</STATUS>"));
return false;
} else {
byte imgSize = Camera.frameLength();
writeSerial(F("<STATUS>TAKE_PHOTO_SUCCESS</STATUS>"));
writeRawSerial(F("<IMAGE_SIZE>"),false);
writeRawSerial(String(imgSize,DEC),false);
writeSerial(F("</IMAGE_SIZE>"));
freeHeap();//It was defined, but not key function, only for showing free heap of esp8266
imgBuffer=Camera.readPicture(imgSize);
freeHeap();
Camera.resumeVideo();
writeSerial(F("<STATUS>SAVE_PHOTO_SUCCESS</STATUS>"));
return true;
}
}
Thank you for reading my questions.
You need to add the standard Arduino functions of setup() and loop() or it won't know where to start.
If you have a look at the example sketches you should be able to get something running, and then you can start adding your takePhoto code.
So, the stack trace shown is pretty standard for any system, and it gives you pretty much everything you need to track it down - It is not assembly code, it is hexadecimal addresses of your binary.
First, you have an Exception 28 - If we look at the ESP8266 Reference, you can see 28 means you either have a bad pointer, or you're trying to access non-cached data from an interrupt/cache is turned off.
Next, you have an address, epc1. This is where the code crashes, which is a hexadecimal address to the binary you uploaded. Also, ctx: cont is helpful, as it indicates the program crashed in your code and not the system code.
You also have excvaddr, which is the addressed you tried to access, 0x10.
Given that, you almost certainly have a NULL pointer somewhere in your code that you are dereferencing. You should use the ESP Exception Decoder to determine what line has the offending crash and bad pointer usage.
In regards to using the software serial - You'd just use the pin numbers on the datasheet. GPIO 14 would be number 14 in the constructor. I found it worked, but caused very bizarre crashes in the production products I worked with after 1+ days of usage, so do not recommend it at all. We ended up using Serial1 for debug printing, and freed up Serial0 for general UART communications - Way more reliable.

Not able to upload to my Arduino Micro board

I uploaded the following program to my Arduino micro:
#include <Mouse.h>
int buttonPin;
void setup() {
pinMode(buttonPin, INPUT);
Mouse.begin();
}
void loop() {
if(digitalRead(buttonPin) == HIGH) {
Mouse.click(MOUSE_LEFT);
}
}
First of all, yes I already know that I haven't defined buttonPin (I realized after the fact) but, this is the code as I uploaded it. Now when ever I plug my Arduino in to try to upload a program it spam clicks, causing the Arduino IDE to overload and not upload my program. It should also be noted that one time it also overloaded my computer forcing me to unplug the Arduino and reset my computer. My question is: #1: is there any way to actually fix the Arduino (I also have an Arduino Uno if I need to hook it up to that for some reason) and #2: when or if I get the Arduino working again, how would I fix my code. (I'm guessing the answer to #2 would be changing int buttonPin; to int buttonPin = 2)
Connect pull-down resistor to pin 0 because global variables are initialized to theirs default values. This should stop spam from mouse and it should be possible to upload code.
Otherwise you need another Arduino as Arduino ISP and upload new code over 6pin serial interface.

How can you check for already stored variables on an Arduino Uno?

I have a program I want to make which will ask to see whether a variable already exists. If it does, it displays it, if it does not, it creates it and stores it in the Arduino using the PROGMEM command. Can someone explain more about PROGMEM and how to make the program I'm talking about?
Generally speaking if you are creating any variables in functions they are existing only there when function is closed all variables are deleted. If you want to keep them alive try to create global variables or use static before it;
like here
static int myvariable;
And here is answer for your question
if (myvariable!=NULL)
{
printfucntion(myvariable);
}
solution for eeprom
EEPROM Read
Reads the value of each byte of the EEPROM and prints it to the computer.
#include <EEPROM.h>
// start reading from the first byte (address 0) of the EEPROM
int address = 0;
byte value;
void setup()
{
Serial.begin(9600);
}
void loop()
{
// read a byte from the current address of the EEPROM
value = EEPROM.read(address);
Serial.print(address);
Serial.print("\t");
Serial.print(value, DEC);
Serial.println();
//move to next address of the EEPROM
address = address + 1;
// there are only 512 bytes of EEPROM, from 0 to 511, so if you are
// on address 512, wrap around to address 0
// if you have arduinoMega probably there is more eeprom space
if (address == 512)
address = 0;
delay(500);
}
I hope I helped.
This is a pretty stale question, and one that's not so popular. BUT is a valid question. In php, I am all the time using isset() to test for variables' existences. So, perhaps the OP is coming to embedded / C programming from the make-love-not-war world of php, where anything goes and isn't accustomed to the extremely literal and formal country of C.
As pointed out here, C language has #ifdef and #ifndef conditional defines that are often used for the exact purpose of testing if something is defined. To better understand the nuance of this usage, one should probably visit Programmers.SE and inquire about professional philosophy about conditional defines.
Me? I'm researching permanent variable storage on an Arduino via the EEPROM. Here are two different excellent articles. And about #ifdef's? I am just a lowly software engineer and save that for software architects. ;-) I have never intentionally implemented them, just see those a lot.
And a literal answer to the OP's question is: query the variable and try to use it. The Arduino's IDE compiler will scream if it is not defined.
Its simple , Just you need to Declare a variable.just compare with array of elements,you wanna compare with. If array element and enter element are present display using Serial.print() statement else you store it in array of buffer accumulating it. Display it.
As you are doing single link list

Resources