Arduino SSD1306 Display - Strange Behaviour - arduino

I have 2 SSD1306 (126x64) displays that are behaving in a very strange way:
Here is a short video of the sketch I ran: https://streamable.com/dl0p8j
And the sketch itself is here (adafruit SSD1306 + GFX). All of the displays are on I2C port 0x3c.
The one on the left is from a different producer and works fine. I can't figure out if I have defective units or if they need slightly different treatment. When the displays are written to, it seems like the entire image is drawn into the 6-8 pixel area at the top.
I would greatly appreciate if anyone has experience in this. I'm also not sure if this is the right place to post this, so let me know if there is a more fitting SE site.

In order for I2C devices running on the same bus to work, each device need to have its own I2C addr, unfortunately the SSD1306 only has two I2C addresses that you could select, 0x3c or 0x3d.
For more than two SSD1306 running on the same I2C, you will need to either have an Arduino boards that could provided more than one I2C interface or to add some hardware solution like an I2C multiplexer.
In the meantime, if you want to run two displays together, you need to modify the code to create two display instances with different addresses.
Change the line:
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
to:
Adafruit_SSD1306 display1(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Adafruit_SSD1306 display2(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Change the line:
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
to
if(!display1.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("display 1 failed"));
for(;;);
}
if(!display2.begin(SSD1306_SWITCHCAPVCC, 0x3D)) {
Serial.println(F("display 2 failed"));
for(;;);
}
For all the subsequent calling of display methods, you need to change it to display1 and add another similar line for display2, for example:
display.display();
will need to change to:
dispaly1.display();
display2.display();

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.

Vibrationsensor keeps giving us the same value and it doesn't change when we "vibrate" it

We want to get the value of our vibrationsensor but it keeps giving us the value "677". We are new to programming so we don't really know how to fix this. The problem we encountered is in Arduino IDE
We tried to change the Serial.begin number but that was just a desperate call for help which obviously didn't work. We also tried to put the the power input in another port. First we put it in the 5V port, but it only gave us the value 1023, which is the max (I think). But then we put it in the 3.5V port and it gave us 677, which is a bit better but it doesn't change no matter how much we vibrate it.
Our code is:
int vibrationsensor = A0;
int vibrationvalue;
void setup()
{
pinMode(vibrationsensor, INPUT);
Serial.begin(9600);
}
void loop()
{
vibrationvalue = analogRead(vibrationsensor);
Serial.println(vibrationvalue);
delay(300);
}
We expected that the output would be consistent when the area is not vibrating and that it would change when we would vibrate it. But at this moment it just gives us 677 which isn't exactly wrong IF the area isn't vibrating, but it stays 677 even when we vibrate it.
As mentionned is a reference for your sensor:
it will react to shock and vibration by closing the circuit.
So, this vibration sensor is working in an ON/OFF mode: it will not return a analogic values of the vibration, it will only tell you if there is a shock or not. Seems that this sensor is not adapted to what you want to perform.
Hope it helps,

Protocol for writing data to 16x2 LCD via I2C

I am new to electronics and has completed a tutorial on how to operate a 16x2 Character LCD via I2C in Arduino using liquidCrystal_I2C. Everything works fine but I have a question about the low level interaction between the I2C and the LCD. Looking at the library's source code, I notice that when writing a 4 bits nibble (LiquidCrystal_I2C::write4bits), the code writes the nibble to the I2C expander first
(LiquidCrystal_I2C::expanderWrite), and then writes again when pulsing the Enable bit. Why is the first expanderWrite necessary? Why can't write4bits just call pulseEnable (with the blacklight bit set)?
I am sure there is a reason as I checked other library like RPLCD and see a similar pattern. Can anyone enlighten me? Thank you.
From the datasheet I found the LCD requires specific timing in the communication protocol.
On the rising edge of the enable line the Register Select and Read/Write lines must have already settled for tsu1 (100ns). On the falling edge of the enable line the data must have already settled for tsu2 (60ns). By writing _data they are also writing the RS and R/W lines as they are the lower nibble of _data.
This article covers the topic very thoroughly.
//**** From LiquidCrystal_I2C.h
// flags for backlight control
#define LCD_BACKLIGHT 0x08
#define LCD_NOBACKLIGHT 0x00
#define En B00000100 // Enable bit
#define Rw B00000010 // Read/Write bit
#define Rs B00000001 // Register select bit
// ^--------Backlight bit defined above
// ^^^^---------Data bits
//**** From LiquidCrystal_I2C.cpp
void LiquidCrystal_I2C::write4bits(uint8_t value) {
expanderWrite(value);
pulseEnable(value);
}
void LiquidCrystal_I2C::expanderWrite(uint8_t _data){
Wire.beginTransmission(_addr);
Wire.write((int)(_data) | _backlightval);
Wire.endTransmission();
}
void LiquidCrystal_I2C::pulseEnable(uint8_t _data){
expanderWrite(_data | En); // En high
delayMicroseconds(1); // enable pulse must be >450ns
expanderWrite(_data & ~En); // En low
delayMicroseconds(50); // commands need > 37us to settle
}

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;
}

Strange initial output using Serial.print

When I'm writing to the serial interface, I'm getting strange and unexpected output when my sketches first run. The output seems to be a variant of what should be printed:
eg:
String text1 = "foobar";
void setup() {
Serial.begin(9600);
Serial.print("\n");
Serial.print(text1);
}
void loop() {
}
Results in the output:
fo
foobar
(the new line appears before "fo" but I couldn't work out how to include it).
So some variant of whatever is supposed to be printed, gets printed before the actual text that is supposed to be printed. Changing the output, changes the anomalous text (sometimes it'll be two characters, sometimes three). Making changes that don't affect the output and recompiling has no effect on the anomalous text.
I'm a total Arduino newbie (I only started writing my own code today), but I can only assume this isn't normal.
I'm using a Freetronics EtherTen and the 1.0 IDE
thanks in advance
Arduino is restarting your sketch when you open its serial port on the computer.
so it prints out, and then initialized again.
after
Serial.begin(9600);
try to put either:
delay(500)
or
while (!Serial); // while the serial stream is not open, do nothing:
This is most likely a Serial communication Reset issue as Eran W pointed out. See my previous answer here.
The Arduino automatically resets when it receives serial communication from most things other than the Arduino IDE. This is why you can send from the IDE but not anything else.
I have an Uno and put a capacitor between Reset and Ground.Here's a page with some good info on the subject.
Good luck. http://arduino.cc/playground/Main/DisablingAutoResetOnSerialConnection
You should probably terminate your string with a 0.
Like:
String text1 = "foobar",0;

Resources