OLED display + ISR (interrupt service routine)

Hi my name is Sergio and I'm trying to make a vending machine using Arduino and a multi coin acceptor, i already have a code that works just fine with the serial monitor but i want to add a monochrome OLED display (JMD 2.42" i2c/spi - brand "smart").
For what i've read, the wire.h library and the use of interrupts at the same sketch is not possible, so i would like if there is any other option so i can display the information other that serial monitor?
i'll attach the working code without any OLED display (just the Serial.prints():wink:

By the way, i tried an example code (ssd1306_128x64_spi)and the display works just fine.

/*--------CODIGOS DE PRODUCTOS--------------------------------------------------------------------
jpr = jabon para ropa       
spr = suavizante para ropa  
jpt = jabon para trastes    
clr = cloro                 
lpp = limpiador para pisos                                                    
*///------PRECIOS DE LOS PRODUCTOS----------------------------------------------------------------
int jpr = 20; 
int spr = 27; 
int jpt = 37;         //INTRODUCIR EL PRECIO EN PESOS MEXICANOS.............
int clr = 12; 
int lpp = 23;
//--------TIEMPOS DE LLENADO----------------------------------------------------------------------
int TJPR = 3500;
int TSPR = 3000;
int TJPT = 4000;     //INTRODUCIR EL TIEMPO EN MILISEGUNTOS / EJEMPLO: [5.5 seg = 5500]   
int TCLR = 2000;
int TLPP = 1800;
//--------PINES DE SALIDA PARA CONTROLAR BOMBAS---------------------------------------------------
const int cambio = 11;
const int bjpr = 10;
const int bspr = 9;
const int bjpt = 6;
const int bclr = 5;
const int blpp = 3;
//--------ENTRADAS--------------------------------------------------------------------------------
int CAMBIO;
int JPR;
int SPR;
int JPT;
int CLR;
int LPP;
//------PIN DE ENTRADA DE MONEDAS-----------------------------------------------------------------
const int SignalPin = 2;
//-----NUMERO DE PULSOS POR MONEDA----------------------------------------------------------------
const int UnPeso = 1;
const int DosPesos = 2;
const int CincoPesos = 3;
const int DiezPesos = 4;
const int DiezPesos5 = 5;
//------------------------------------------------------------------------------------------------
volatile int pulso = 0;
unsigned long MillisUltPulso = 0;
//------------------------------------------------------------------------------------------------
int PulsosAcum = 0;
int CreditoAcum = 0;
int MaxTimePulse = 200;
////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
//pinMode(blpp,OUTPUT);
Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(SignalPin), coinInterrupt, RISING);
delay(100);}
////////////////////////////////////////////////////////////////////////////////////////////////
void coinInterrupt(){
pulso++;
MillisUltPulso = millis();
Serial.println("Interrupcion");}
///////////////////////////////////////////////////////////////////////////////////////////////
void loop(){
int valuea0=analogRead(A0);
int valuea1=analogRead(A1);
//-----CAMBIO----------------------------------------------------------------------------------
//CAMBIO=analogRead(valuea1>-1000&&valuea1<200);
if((valuea1>-1000&&valuea1<200)&&(CreditoAcum||(0))){
while(CreditoAcum||(0)){
CreditoAcum-=1;
Serial.print("CREDITO: $");
Serial.println(CreditoAcum);
analogWrite(cambio,255);
delay (300);
analogWrite(cambio,0);
delay(300);}
Serial.print("CREDITO: $ ");
Serial.print(CreditoAcum);
Serial.println(".00"); 
Serial.println("¡¡GRACIAS POR SU COMPRA!!");
Serial.println("... ↓↓↓ TOME SU CAMBIO ↓↓↓ ... ");}
else {analogWrite(cambio,255);}
//-----JABON PARA ROPA-------------------------------------------------------------------------
JPR = analogRead(valuea1>200&&valuea1<630);
if((valuea1>200&&valuea1<630)&&(CreditoAcum>=(jpr))){
analogWrite(blpp,255);
Serial.println("SIRVIENDO JABON PARA ROPA...");
delay (TJPR);
CreditoAcum=CreditoAcum-(jpr);
analogWrite(bjpr,0);
Serial.print("PRODUCTO SERVIDO / CREDITO RESTANTE:  $");
Serial.print(CreditoAcum);
Serial.println(".00");}
else if ((valuea1>200&&valuea1<630)&&(CreditoAcum<jpr)){
Serial.println("INTRODUSCA MAS MONEDAS");
Serial.print("CREDITO INSUFICIENTE:  $");
Serial.print(CreditoAcum);
Serial.println(".00");
delay(500);}
//-----SUAVIZANTE PARA ROPA--------------------------------------------------------------------
//SPR = analogRead(valuea1>630&&valuea1< 900);
if((valuea1>630&&valuea1< 900)&&(CreditoAcum>=(spr))){
analogWrite(bspr,255);
Serial.println("SIRVIENDO SUAVIZANTE PARA ROPA...");
delay (TSPR);
CreditoAcum=CreditoAcum-(spr);
analogWrite(bspr,0);
Serial.print("PRODUCTO SERVIDO / CREDITO RESTANTE:  $");
Serial.print(CreditoAcum);
Serial.println(".00");}
else if ((valuea1>630&&valuea1< 900)&&(CreditoAcum<spr)){
Serial.println("INTRODUSCA MAS MONEDAS");
Serial.print("CREDITO INSUFICIENTE:  $");
Serial.print(CreditoAcum);
Serial.println(".00");
delay(500);}
//-----JABON PARA TRASTES----------------------------------------------------------------------
//JPT = analogRead(valuea1>630&&valuea1< 900);
if((valuea0>-1000&&valuea0<200)&&(CreditoAcum>=(jpt))){
analogWrite(bjpt,255);
Serial.println("SIRVIENDO JABON PARA TRASTES...");
delay (TJPT);
CreditoAcum=CreditoAcum-(jpt);
analogWrite(bjpt,0);
Serial.print("PRODUCTO SERVIDO / CREDITO RESTANTE:  $");
Serial.print(CreditoAcum);
Serial.println(".00");}
else if ((valuea0>-1000&&valuea0<200)&&(CreditoAcum<jpt)){
Serial.println("INTRODUSCA MAS MONEDAS");
Serial.print("CREDITO INSUFICIENTE:  $");
Serial.print(CreditoAcum);
Serial.println(".00");
delay(500);}
//-----CLORO-----------------------------------------------------------------------------------
//CLR = analogRead(valuea0>200&&valuea0<630);
if((valuea0>200&&valuea0<630)&&(CreditoAcum>=(clr))){
analogWrite(bclr,0);
Serial.println("SIRVIENDO CLORO...");
delay (TCLR);
CreditoAcum=CreditoAcum-(clr);
analogWrite(bclr,255);
Serial.print("PRODUCTO SERVIDO / CREDITO RESTANTE:  $");
Serial.print(CreditoAcum);
Serial.println(".00");}
else if ((valuea0>200&&valuea0<630)&&(CreditoAcum<clr)){
Serial.println("INTRODUSCA MAS MONEDAS");
Serial.print("CREDITO INSUFICIENTE:  $");
Serial.print(CreditoAcum);
Serial.println(".00");
delay(500);}
else {analogWrite(bclr,255);}
//-----LIMPIADOR PARA PISOS--------------------------------------------------------------------
//LPP = analogRead(valuea0>630&&valuea0< 900);
if((valuea0>630&&valuea0< 900)&&(CreditoAcum>=(lpp))){
analogWrite(blpp,0);
Serial.println("SIRVIENDO LIMPIADOR PAR PISOS...");
delay (TLPP);
CreditoAcum=CreditoAcum-(lpp);
analogWrite(blpp,255);
Serial.print("PRODUCTO SERVIDO / CREDITO RESTANTE:  $");
Serial.print(CreditoAcum);
Serial.println(".00");}
else if ((valuea0>630&&valuea0< 900)&&(CreditoAcum<lpp)){
Serial.println("INTRODUSCA MAS MONEDAS");
Serial.print("CREDITO INSUFICIENTE:  $");
Serial.print(CreditoAcum);
Serial.println(".00");
delay(500);}
else {analogWrite(blpp,255);}
//-----LOGICA DE CREDITOS----------------------------------------------------------------------
unsigned long lastTime = millis() - MillisUltPulso;
if((pulso > 0) && (lastTime >= MaxTimePulse)){
PulsosAcum = pulso;
pulso = 0;
Serial.print("Pulses: ");
Serial.print(PulsosAcum);
Serial.print(" LastTime: ");
Serial.print(lastTime);
Serial.print(" LastPulse: ");
Serial.println(MillisUltPulso);}
switch (PulsosAcum){
case UnPeso:
PulsosAcum = 0;
Serial.println("MONEDA DEPOSITADA DE $ 1.00");
CreditoAcum += 1;
Serial.print("CREDITO: $ ");
Serial.print(CreditoAcum);
Serial.println(".00");
break;
case DosPesos:
PulsosAcum = 0;
Serial.println("MONEDA DEPOSITADA DE $ 2.00");
CreditoAcum += 2;
Serial.print("CREDITO: $ ");
Serial.print(CreditoAcum);
Serial.println(".00");
break;
case CincoPesos:
PulsosAcum = 0;
Serial.println("MONEDA DEPOSITADA DE $ 5.00");
CreditoAcum += 5;
Serial.print("CREDITO: $ ");
Serial.print(CreditoAcum);
Serial.println(".00");
break;
case DiezPesos:
PulsosAcum = 0;
Serial.println("MONEDA DEPOSITADA DE $ 10.00");
CreditoAcum += 10;
Serial.print("CREDITO: $ ");
Serial.print(CreditoAcum);
Serial.println(".00");
break;
case DiezPesos5:
PulsosAcum = 0;
Serial.println("MONEDA DEPOSITADA DE $ 10.00");
CreditoAcum += 10;
Serial.print("CREDITO: $ ");
Serial.print(CreditoAcum);
Serial.println(".00");
break;}}

vending machine.ino (7.69 KB)

Hi matagato5,

It would be better if you wrote your code to use millis() for timing and make the code multitasking, and got rid of the interrupts for your coin acceptor. You should not be using delay() at all.This is one of the most important things you should learn on the way from beginner to experienced programmer.

Start with the blink without delay example in the IDE.

Then study these:
Using millis for timing
Demonstration for several things at the same time

I have a tutorial about reading inputs from buttons and similar things How to get the best out of this forum - General Electronics - Arduino Forum

It makes code a lot easier to read if you break it into functions, each with its own task, as in the examples in my tutorial.

You can expect to have to spend some time studying those and understanding how they work and what to do to modify your code.

Good luck, and ask if you get stuck.

Oh! And variable names, make them longer and more meaningful! The compiler doesn't care how long they are, so make them into words that mean something.

I ended up using a 16x2 LCD display it seems to work just fine for this project but thanks for the advice, I'll definitely look forward to understand Millis, I'm a beginner with Arduino and I would like to improve my skills.
About the variable names, they are acronyms (but in Spanish) so they are meaningful to me :stuck_out_tongue: sorry about that.
anyhow thank you very much for your interest and disposition to help :slight_smile:

About the variable names, they are acronyms (but in Spanish) so they are meaningful to me

Ok, good, as long as they mean something to you. It's another common mistake to make them short and meaningless; it doesn't matter to the compiler or make any difference to the code that gets uploaded to the microcontroller.

This topic was automatically closed after 55 days. New replies are no longer allowed.