OLED display behaves strangely

Dear friends, currently I am working on an ultrasound-"radar" project. An ultrasound sensor swipes over a certain angle with the help of a stepper motor, reads distances and a tiny little OLED display is supposed to show the "environment" graphically. I know there exist a million similar projects out there with code and all but I wanted to make it by myself. (I just "borrowed" the declaration part.) I broke it into different tasks and managed to get the stepper and the ultrasound sensor working well. Just the display behaves strangely. It is an SSD1306 0.96" IIC. I tested different small things first. When I write code like:

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define linelength 40
#define x1_line 64
#define y1_line 60
#define wait 100

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);


void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);

if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
}
}

void loop() {
  // put your main code here, to run repeatedly:

display.clearDisplay();
display.display();

display.drawLine(64,64,10,10,WHITE);
display.display();
delay(50);
display.clearDisplay();
display.drawLine(64,64,7,10,WHITE);
display.display();
delay(50);
display.clearDisplay();
display.drawLine(64,64,15,10,WHITE);
display.display();
delay(wait);
display.clearDisplay();

display.drawLine(64,64,30,10,WHITE);
display.display();
delay(wait);
display.clearDisplay();
display.drawLine(64,64,45,10,WHITE);
display.display();
delay(wait);
display.clearDisplay();
display.drawLine(64,64,60,10,WHITE);
display.display();
delay(wait);
display.clearDisplay();
display.drawLine(64,64,75,10,WHITE);
display.display();
delay(wait);
display.clearDisplay();
display.drawLine(64,64,90,10,WHITE);
display.display();
delay(wait);
display.clearDisplay();
display.drawLine(64,64,105,10,WHITE);
display.display();
delay(wait);
display.clearDisplay();
display.drawLine(64,64,128,10,WHITE);
display.display();
delay(wait);
display.clearDisplay();

delay(5);
}

it works well and shows, the display is able to show many lines one after the other. This was only a preliminary test. Now I wanted to "let the beam swipe over the display" with the help of a for loop. It still is not connected to the stepper or sensor, it is still just a display test:

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define linelength 40
#define x1_line 64
#define y1_line 60
#define wait 100

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);


void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);

if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
}
}

void loop() {
  // put your main code here, to run repeatedly:

display.clearDisplay();
display.display();
for (int i= (-45); i<45; i=i+5){
   display.clearDisplay();
   delay(wait);
   int angle = i + 90; // turn graphics around by 90 degrees
   int i_rad = angle*PI/180;
   int x2_line = x1_line+(linelength * cos(i_rad)); //polar to cartesian coordinates
   int y2_line = y1_line-(linelength * sin(i_rad));
   
   display.drawLine(x1_line,y1_line,x2_line,y2_line,WHITE);
   display.display();
   Serial.println(i);
   delay(wait);
   }
delay(5);
}

I expected a "swipe motion" with 5 degree steps but I only got three lines roughly 57(=1 rad?) degrees from each other. They appear one after the other and then get cleared and it starts over again. On the serial monitor I get good 5 degree readings for i. What do I miss?

everything is correct, you calculate radians in integers, and there are only 3 radians in 180 degrees.
Try to define i_rad variable as float

Thank you so much b707! Seems I was just one hair away from the solution (1 rad) but you spotted it right away. Now with float i_rad the code works. Now I can go on and build the stuff. Thank you!

Please mark the thread as solved if got the answer

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.