Hi all -- this is my very first post here so I'm sorry if I make any mistakes, but I'll try my best to get the point across
This project includes an Arduino Uno R3, an LED, a servo motor, an APDS9960(Sparkfun Gesture Sensor), and a SSD1306(128x64 I2C OLED display by Adafruit).
Although this post is simply about OLED not initializing, since I can't put my finger on what the source of the problem is(no error messages on terminal) I'll explain the whole thing fyi.
Simply put, this is a vault, but instead of using numerical passwords, it takes gestures as input. When the sensor detects the "correct" gesture (which is decided by functions I will explain shortly), the servo will unlock the vault, and the LED will light up. The OLED is kind of just there to tell people which gestures the sensor is detecting, but I haven't yet written the code that will display corresponding images to respective gestures.
The "correct" gesture is determined by rsaGen() and rsaDecr(). Since this is a school project, I'm trying to simulate(on the serial monitor) a case where the vault receives information encrypted in RSA(which is (# representing one of the four random directions)*(randomly picked prime number)), and the vault decrypts it upon receipt. If the sensor detects the gesture the vault "received", it will open.
All libraries are up-to-date, not to mention that each device works perfectly fine on example codes(voltage, wiring, device address checked). One thing I noticed however is that when I remove the entire RSA encrypting-decrypting part of the sketch, the OLED initializes perfectly fine.. This has stumped me for days now... :'D
Thanks to everyone who read until this point, and I would really appreciate your help!
--Adria
/*Headers*/
#include <Wire.h>
#include <Servo.h>
#include <SparkFun_APDS9960.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
/*LED pins n stuff*/
#define LED_PIN 13
/*Servo init*/
#define SERVO_PIN 9
Servo myservo;
/*Gesture sensor init*/
#define APDS9960_INT 2
SparkFun_APDS9960 apds = SparkFun_APDS9960();
int isr_flag = 0;
/*OLED display init*/
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define SSD1306_ADDR 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
/*Defining Serial.print(F()) (Doesn't work for int, float, double, etc.)*/
#define Fprint(x) println(F(x))
/*Public variables*/
int left = 1223;
int right = 4001;
int up = 2677;
int down = 5099;
volatile int dir_input = 0;
void setup() {
Serial.begin(9600);
/*Attach pin*/
pinMode(LED_PIN, OUTPUT);
myservo.attach(SERVO_PIN);
//Checking for initialization
display.begin(SSD1306_SWITCHCAPVCC, SSD1306_ADDR);
attachInterrupt(digitalPinToInterrupt(APDS9960_INT), interruptRoutine, FALLING);
myservo.write(0);
if(myservo.read() == 0){
Serial.Fprint("Servo initialization successful!");
} else {
Serial.Fprint("Error initializing servo!");
}
if(!apds.init()){
Serial.Fprint("Error initializing APDS9960!");
} else {
Serial.Fprint("APDS9960 initialization successful!");
}
if(!display.begin(SSD1306_SWITCHCAPVCC, SSD1306_ADDR)) {
Serial.Fprint("Error initializing SSD1306!");
} else {
Serial.Fprint("SSD1306 initialization successful!");
}
display.display();
delay(2000);
display.clearDisplay();
display.display();
rsaPrint();
if(dir_input == rsaDecr()){
digitalWrite(LED_PIN, HIGH);
myservo.write(90);
delay(5000);
digitalWrite(LED_PIN, LOW);
myservo.write(0);
}
}
void loop() {
if( isr_flag == 1 ) {
detachInterrupt(digitalPinToInterrupt(APDS9960_INT));
handleGesture();
isr_flag = 0;
attachInterrupt(digitalPinToInterrupt(APDS9960_INT), interruptRoutine, FALLING);
}
}
void interruptRoutine() {
isr_flag = 1;
}
void handleGesture() {
if (apds.isGestureAvailable()) {
switch (apds.readGesture()) {
case DIR_LEFT:
Serial.println("LEFT");
dir_input = 1223;
break;
case DIR_RIGHT:
Serial.println("RIGHT");
dir_input = 2677;
break;
case DIR_UP:
Serial.println("UP");
dir_input = 7481;
break;
case DIR_DOWN:
Serial.println("DOWN");
dir_input = 5099;
break;
default:
Serial.println("NONE");
}
}
}
void rsaPrint(){
Serial.Fprint("-----------------------------");
Serial.print(F("Encrypting gesture info"));
delay(500);
Serial.print(F(". "));
delay(500);
Serial.print(F(". "));
delay(500);
Serial.Fprint(". ");
Serial.Fprint("");
delay(2000);
Serial.print(F("Sending"));
delay(500);
Serial.print(F(". "));
delay(500);
Serial.print(F(". "));
delay(500);
Serial.Fprint(". ");
Serial.Fprint("-----------------------------");
delay(2000);
Serial.print(F("Received: "));
delay(500);
Serial.println(rsaGen());
Serial.Fprint("");
delay(2000);
Serial.print(F("Decrypting"));
delay(500);
Serial.print(F(". "));
delay(500);
Serial.print(F(". "));
delay(500);
Serial.Fprint(". ");
Serial.Fprint("");
delay(500);
Serial.print(F("Gesture: "));
delay(1500);
if(rsaDecr() == 1223){
Serial.Fprint("LEFT");
} else {
if(rsaDecr() == 2677){
Serial.Fprint("RIGHT");
} else {
if(rsaDecr() == 7481){
Serial.Fprint("UP");
} else {
Serial.Fprint("DOWN");
}
}
}
}
long int rsaGen(){
randomSeed(analogRead(0)); //Makes ran more random by reading noise of an unconnected pin
int dir = 0;
int ran = random(100);
long int result = 0;
if(ran < 25){
dir = left;
} else {
if(ran < 50){
dir = right;
} else {
if(ran < 75){
dir = up;
} else {
dir = down;
}
}
}
result = dir * randomPrime();
return result;
}
long int rsaDecr(){
int dir = 0;
int mod = 0;
int Dir[4] = {1223, 4001, 2677, 5099};
for(int i=0; i<4; i++){
mod = rsaGen()%Dir[i];
if(mod == 0){
dir = Dir[i];
}
}
return dir;
}
int randomPrime(){
randomSeed(analogRead(1));
int ran = random(100);
int ranprime = 0;
if(ran < 10){
ranprime = 6007;
} else {
if(ran < 20){
ranprime = 7019;
} else {
if(ran < 30){
ranprime = 4441;
} else {
if(ran < 40){
ranprime = 6203;
} else {
if(ran < 50){
ranprime = 5107;
} else {
if (ran < 60){
ranprime = 3137;
} else {
if(ran < 70){
ranprime = 2963;
} else {
if(ran < 80){
ranprime = 7411;
} else {
if(ran < 90){
ranprime = 1709;
} else {
ranprime = 4001;
}
}
}
}
}
}
}
}
}
return ranprime;
}
16 posts - 4 participants