Digispark replacement Wire library - arduino

I have a dilemma:
I am trying to get a Digispark to read a BH1750 I2C light sensor and then send the readout via USB. Currently I'm using the DigiUSB library to send to the PC but the sketch is very big (can't implement any of the features I want, using 94% of the flash mem). At the moment it's just sending the value every 0.5 seconds, doing no calculations on it and I have to re-plug the device at boot. I know how to implement these features, it's just that there isn't enough space on the digispark, using both Wire and DigiUSB.
So in order to reduce the size I want to replace Digistump's big Wire library with SoftI2CMaster&SoftWire OR USIWire, both half the size.
I tried two BH1750 I2C libraries (both work fine with Digistump's Wire) but for all four combinations I get the following error:
In file included from C:\Users\xyz\AppData\Local\Temp\arduino_modified_sketch_192160\Echo.ino:8:0:
C:\Users\xyz\Documents\Arduino\libraries\SoftI2CMaster-master/SoftWire.h:101:10: error: conflicting return type specified for 'virtual size_t SoftWire::write(const uint8_t*, size_t)'
size_t write(const uint8_t *data, size_t quantity) {
^
In file included from C:\Users\xyz\AppData\Local\Arduino15\packages\digistump\hardware\avr\1.6.7\cores\tiny/Stream.h:24:0,
from C:\Users\xyz\AppData\Local\Arduino15\packages\digistump\hardware\avr\1.6.7\cores\tiny/TinyDebugSerial.h:31,
from C:\Users\xyz\AppData\Local\Arduino15\packages\digistump\hardware\avr\1.6.7\cores\tiny/WProgram.h:18,
from C:\Users\xyz\AppData\Local\Arduino15\packages\digistump\hardware\avr\1.6.7\cores\tiny/Arduino.h:4,
from sketch\Echo.ino.cpp:1:
C:\Users\xyz\AppData\Local\Arduino15\packages\digistump\hardware\avr\1.6.7\cores\tiny/Print.h:75:18: error: overriding 'virtual void Print::write(const uint8_t*, size_t)'
virtual void write(const uint8_t *buffer, size_t size);
^
exit status 1
I tried to figure out how to fix this "conflicting return type" error but I couldn't figure out how to do that. Google is often reliable in finding answers but not today.
Any help would be appreciated!

Related

How do I control servos using Processing code and an Arduino UNO Board?

To the stackoverflow community,
First, let me begin by saying that I'm not a code writer (though I'm quite familiar with LabVIEW). My background is in Laser and optical system design and development. Currently, I'm trying to integrate servos into an optical component used in a long range atmosphere mapping Lidar. The optical component, refereed to as a TR-Swich, is critical in maintaining the alignment between the Transmitted Laser pulse stream and back-scattered Return light. In order to ensure long-term alignment and compensate for optical mount thermal and shock drift, the servos - connected to optical mount pitch and yaw actuators - will allow our customers to adjust the TR-Swich as needed and thereby maintain optimum signal returns. Due to a variety of constraints (time, space, ease of integration into existing hardware, etc.), I'm hoping to use servos, which are small and reliable and can be easily controlled using an Arduino UNO board. I've already proven that they work quite well to move and set the position of the actuators; now I'm trying to get the GUI interface working...which hopefully is where all of you come in...
Since I'm new to Arduino board code (and the related Processing code), I snooped around and found code published on hackster.io (back in 2020) - which is close to what I'm looking for. The code was written by engineerkid (his hackster user name). I copied his code for both the Arduino board and the Processing GUI, but haven't been able to get it to work. I reached out to him (through hackster) but haven't received a reply. The message I sent him was, " Hi Enginnerkid, First, I want to thank you for posting the example! It's very close to what I ultimately need for adjusting optical mounts using servos. I copied your code for both the Arduino and Processing sketches and it's close to working, but something isn't quite right. The servo (practically identical to what you're using in your example) moves to the center position and the display based on the mouse position using the Processing code work just fine. Unfortunately, the servo doesn't respond to the mouse movements. As a heads up, currently I'm only using one servo - are two servos required in order for the code to work? Going on this premise, I deleted the code related to one of the servos, but still wasn't able to achieve any movement. I did some browsing regarding communication problems between Arduino and Processing code and found the following comment (which might be relevant), " The serial monitor in Arduino is like a separate terminal program so it and your Processing sketch are competing for the same serial connection to the Arduino." Any help you can provide would be greatly appreciated! Thanks! Steve"
His Arduino code is:
#include <Servo.h>
char tiltChannel=0, panChannel=1;
Servo servoTilt, servoPan;
char serialChar=0;
void setup()
{
servoTilt.attach(9); //The Tilt servo is attached to pin 9.
servoPan.attach(10); //The Pan servo is attached to pin 10.
servoTilt.write(90); //Initially put the servos both
servoPan.write(90); //at 90 degress.
Serial.begin(57600); //Set up a serial connection for 57600 bps.
}
void loop()
{
while(Serial.available() <=0); //Wait for a character on the serial port.
serialChar = Serial.read(); //Copy the character from the serial port to the variable
if(serialChar == tiltChannel){ //Check to see if the character is the servo ID for the tilt servo
while(Serial.available() <=0); //Wait for the second command byte from the serial port.
servoTilt.write(Serial.read()); //Set the tilt servo position to the value of the second command byte received on the serial port
}
else if(serialChar == panChannel){ //Check to see if the initial serial character was the servo ID for the pan servo.
while(Serial.available() <= 0); //Wait for the second command byte from the serial port.
servoPan.write(Serial.read()); //Set the pan servo position to the value of the second command byte received from the serial port.
}
//If the character is not the pan or tilt servo ID, it is ignored.
}
His Processing code is:
import processing.serial.*;
Serial port; // The serial port we will be using
int xpos=90; // set x servo's value to mid point (0-180)
int ypos=90; // set y servo's value to mid point (0-180)
void setup()
{
size(360, 360);
frameRate(100);
String arduinoPort = Serial.list()[0];
port = new Serial(this, arduinoPort, 57600);
}
void draw()
{
fill(175);
rect(0,0,360,360);
fill(255,0,0); //rgb value so RED
rect(180, 175, mouseX-180, 10); //xpos, ypos, width, height
fill(0,255,0); // and GREEN
rect(175, 180, 10, mouseY-180);
update(mouseX, mouseY);
}
void update(int x, int y)
{
//Calculate servo postion from mouseX
xpos= x/2;
ypos = y/2;
//Output the servo position ( from 0 to 180)
port.write(xpos+"x");
port.write(ypos+"y");
}
If you would like to look at his code, and the really cool example he set up, the site link is:
https://www.hackster.io/engineerkid/servo-motor-control-using-arduino-and-processing-af8225#team
As mentioned above, I simplified the code by removing the pan servo control, but still wasn't able to get any response from the servo using the tilt portion of the code. It does appear to be related to a communication bottleneck, I'm just not sure what small snip-it of code needs to be added to prevent the conflict. By the way, one indication that the communication is at fault is the LED serial com lights on the Arduino board. Prior to using (or trying to use the Processing GUI) I copied published code for moving a servo just with the Arduino code and was able to position the servo at any desired angle (form 0 to 180 degrees). I noticed that with each upload of the code (which would set the servo to any chosen hard coded angle value), the LEDs would flicker and toggle back and forth as the new code uploaded. Now with the GUI, only 1 LED stays on continuously... which is probably not a good sign...
As I mentioned to Engineerkid, any help you can provide would be greatly appreciated!
Thanks for your time.
Steve
The java code does not send what your arduino sketch expects (or vice versa)
You're reading a single control character and compare it with one of two allowed values:
serialChar = Serial.read();
if(serialChar == tiltChannel){
I suggest to change at the Arduino side
char tiltChannel='x', panChannel='y';
And to modify the java side to
port.write("x");
port.write(xpos);
Same for the other servo, too.
And make sure each java port.write sends a single byte only.

How to integrate attiny85 with source files? Specifically changing Timer0 to Timer1 to avoid conflicting use of timers

I am trying to control a servo motor on a timer using buttons and the ATtiny85. I am able to use the ATtiny to make an LED blink at the push of a button but once I include the servo library my code does not work.
I have tried using the Servo.h library and the Servo8Bit.h library but neither work. I thought that the issue might be coming from one timer being used for two different things so I went into the Servo8Bit.h and Servo8Bit.cpp source files and changed all Timer0 to Timer1 and all TCCR0B to TCCR1B.
I receive these error messages when I try to upload my code:
/private/var/folders/kd/6b3mdhb90xl1rm2j9_dvn7vr0000gn/T/AppTranslocation/EDE8B1E7-9D65-436D-87B1-4534CFB3B4CF/d/Arduino.app/Contents/Java/libraries/Servo8Bit-master/Servo8Bit.cpp: In static member function 'static void ServoSequencer::setupTimerPrescaler()':
/private/var/folders/kd/6b3mdhb90xl1rm2j9_dvn7vr0000gn/T/AppTranslocation/EDE8B1E7-9D65-436D-87B1-4534CFB3B4CF/d/Arduino.app/Contents/Java/libraries/Servo8Bit-master/Servo8Bit.cpp:493:9: error: 'TCCR1A' was not declared in this scope
TCCR1A = 0;
^
/private/var/folders/kd/6b3mdhb90xl1rm2j9_dvn7vr0000gn/T/AppTranslocation/EDE8B1E7-9D65-436D-87B1-4534CFB3B4CF/d/Arduino.app/Contents/Java/libraries/Servo8Bit-master/Servo8Bit.cpp:498:13: error: 'TCCR1B' was not declared in this scope
TCCR1B &= ~(1<< CS02); //clear
^
Using library Servo8Bit-master in folder: /private/var/folders/kd/6b3mdhb90xl1rm2j9_dvn7vr0000gn/T/AppTranslocation/EDE8B1E7-9D65-436D-87B1-4534CFB3B4CF/d/Arduino.app/Contents/Java/libraries/Servo8Bit-master (legacy)
exit status 1
Error compiling for board ATtiny25/45/85.
The expected result is that after uploading the code I will press a button. After a specified amount of time, 10 minutes in this case, the motor should move but the code doesn't even upload.
All you have to do is comment/uncomment the right choice in the header file Servo8Bit.h:
//Options
//pick one, comment out the other one out:
//#define USE_TIMER0
#define USE_TIMER1
and it'll be compiled with correct settings and so on.
You've probably tried to change 0 to 1 everywhere and failed because the timer0 and timer1 aren't the same. The timer1 doesn't have two control registers A and B unlike timer0.

Having trouble programming wifi on esp8266

So I think I'm doing fairly well so far, have the arduino IDE installed, add the ESP8266 module, manage to get an LED blinking on and off. Time to connect to the WIFI and do some stuff.
Unfortunately, it all falls apart then. I get errors about undefined references to wifi_set_opmode, wifi_station_set_config and wifi_station_connect. Now, this isn't my first rodeo with C so I started looking for documentation on these functions and how they are included but can find nothing except people apparently quite happily using them. I've tried adding all sorts of #includes but nothing helps so far. So time to ask for a little help.
Here is the code I am trying to run (pretty uneventful)
#include <user_interface.h>
#define LED_1 13
void setup() {
// put your setup code here, to run once:
pinMode(LED_1, OUTPUT);
const char ssid[32] = "qqqq";
const char password[32] = "wwww";
struct station_config stationConf;
wifi_set_opmode( STATION_MODE );
os_memcpy(&stationConf.ssid, ssid, 32);
os_memcpy(&stationConf.password, password, 32);
wifi_station_set_config(&stationConf);
wifi_station_connect();
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(LED_1, HIGH);
delay(500);
digitalWrite(LED_1, LOW);
delay(500);
}
My board selection is "Generic ESP8266 module"
Here are the errors.
sketch\blinktest.ino.cpp.o:(.text.setup+0x8): undefined reference to `wifi_set_opmode(unsigned char)'
sketch\blinktest.ino.cpp.o:(.text.setup+0x10): undefined reference to `wifi_station_set_config(station_config*)'
sketch\blinktest.ino.cpp.o:(.text.setup+0x14): undefined reference to `wifi_station_connect()'
You need to notice that Arduino is not C, it's based on C++. So add your inclusion like below to use C SDK based functions.
extern "C" {
#include "user_interface.h"
}
Arduino IDE has code examples on menu
File > Examples
There is also example called WiFiWebClient where you need to change only your wifi ssid and password.
Save it to a new folder and you can make http requests to any site on Internet by modifying the address on the example.
Start from the examples of the manufacturer so you don't need to rely on outdated / partial tutorials.

Reading a long text from GPRS Shield with Arduino

I am having hell with this and I know it is probably really simple. I am trying to read a text message from my Seeed GPRS shield. I have the shield setup as a software serial and I am displaying the information received from the GPRS to the serial monitor. I am currently sending all AT commands over serial while I work on my code. To display the data from the software serial to the serial monitor, I am using the following code.
while(GPRS.available()!=0) {
Serial.write(GPRS.read());
}
GPRS is my software serial obviously. The problem is, the text is long and I only get a few characters from it. Something like this.
+CMGR: "REC READ","1511","","13/12/09,14:34:54-24" Welcome to TM eos8
This text is a "Welcome to T-Mobile" text that is much longer. The last few characters shown are scrambled. I have done some research and have seen that I can mod the serial buffer size to 256 instead of the default 64. I want to avoid this because I am sure there is an easier way. Any ideas?
Have you tried reading into a character array, one byte at a time? See if this helps:
if (GPRS.available()) { // GPRS talking ..
while(GPRS.available()) { // As long as it is talking ..
buffer[count++]=GPRS.read();     
// read char into array
if(count == 64) break; // Enough said!
}
Serial.write(buffer,count); // Display in Terminal
clearBufferArray();
count = 0;
}
You need to declare the variables 'buffer' and 'count' appropriately and define the function 'clearBufferArray()'
Let me know if this helps.
Looks like this is simply the result of the lack of flow control in all Arduino serial connections. If you cannot pace your GPRS() input byte sequence to a rate that guarantees the input FIFO can't overflow, then your Serial.write() will block when the output FIFO fills. At that point you will be dropping new GPRS input bytes on the floor until Serial output frees up more space.
Since the captured output is apparently clean up to about 64 bytes, this suggests
a) a 64 byte buffer,
b) a GPRS data rate much higher than the Serial one, and
c) that the garbage data is actually the occasional valid byte from later in the sequence.
You might confirm this by testing the return code from Serial.write. If you get back zero, that byte is getting lost.
If you were using 9600 for Serial and 57600 for GPRS, I would expect somewhat more than 64 bytes to come through before the output gets mangled, but if the GPRS rate is more than 64x the Serial rate, the entire output FIFO could fill up within a single output byte transmission time.
Capturing to an intermediate buffer should resolve your issue, as long as it is large enough for the whole message. Similarly, extending the size of either the source (in conjunction with testing the Serial.write) or destination (without any additional code) FIFOs to the maximum datagram size should work.
I've had the same problem trying to read messages and get 64 characters. I overcame it by adding a "delay(10)" in the loop calling the function that does the read from the GPRS. Seems to be enough to overcome the race scenario. - Using Arduino Mega.
void loop() {
ReadmyGPRS();
delay(10); //A race condition exists to get the data.
}
void ReadmyGPRS(){
if (Serial1.available()){ // if data is comming from GPRS serial port
count = 0; // reset counter
while(Serial1.available()) // reading data into char array
{
buffer[count++]=Serial1.read(); // writing data into array
if(count == 160)break;
}
Serial.write(buffer,count);
}
}

Arduino: serial.read within ISR

I am writing a small test program that attempts to perform a serial.write() followed by a serial.read() within an ISR. The code will eventually be used to prompt an external GSM shield to send an SMS on a regular basis.
ISR(TIMER2_OVF_vect) {
Serial.println("AT+CMGS=\"0123456789\""); // Tell Sim900 To prepare sms to number 01...
while(Serial.read()!='>'); // Wait for Sim900 to respond
Serial.print("A text message"); // the SMS body
Serial.write(0x1A); //Confirm send instruction
Serial.write(0x0D);
Serial.write(0x0A);
}
}
What I have found after a lot of testing is that Serial.read() within an ISR is not capable of reading a live serial prompt, instead it will only read any input that was buffered before the ISR was triggered.
Is there any way around this?
The only solution I have found is to place this code instead within the main loop(). But I want to send the SMS using a timer interrupt.
Thank you
You need to place the code in the loop() but using an IF:
float toBeSent = interval;
loop() {
if (millis() > toBeSent) {
Send();
toBeSent = milli() + interval;
}
}
interval is your sending interval in milliseconds.
I had a similar problem a while ago which I managed to resolve by using the Arduino SoftwareSerial library instead of the hardware based Serial.read.
There are some overheads associated with using SoftwareSerial, and you can only read one port at a time, so I leave it up to those with a better understanding of the Arduino platform to tell you if this is a good idea, but one of the benefits of this library is that you can use it within an ISR.
To use the SoftwareSerial library, add the following two lines of code at the top of your sketch remembering to replqce the rx_pin and tx_pin with the corresponding pin values you want to use:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(rx_pin, tx_pin);
Then replace the key word Serial throughout your sketch with mySerial (or whatever name you have chosen to give your SoftwareSerial instance).
An important thing to keep in mind when using SoftwareSerial is that you can only use certain pins on the Arduino so read the documentation first.
If you wanted to live dangerously you could enable interrupts inside the ISR and use a flag to prevent reentry.
int flag=0;
ISR(TIMER2_OVF_vect) {
flag = 1
if (flag){return;}
sei();
Serial.println("AT+CMGS=\"0123456789\""); // Tell Sim900 To prepare sms to number 01...
while(Serial.read()!='>'); // Wait for Sim900 to respond
Serial.print("A text message"); // the SMS body
Serial.write(0x1A); //Confirm send instruction
Serial.write(0x0D);
Serial.write(0x0A);
}
flag = 0;
}

Resources