Many sensors/timing

I have several question regarding a rather large sketch.

I have 2 potmeters and and 9 seonsor connected to analog pins through a multiplexer, then I have a pickup sensor which either is high or low with a maximum of 200 pulses per second, then i have strobe which needs to be pulsed at maximum 50Hz

So my problem is to be able to read the pulses without missing to many input pulses: readRPM()

and generate the output pulses:
strobeSpeed();
at the same time I want to log the data and scroll between the values of the analog inputs on the lcd scrren.

Does the sketch below look like a good start? It compiles, but Im not sure how to solve the task metioned above

Cheers

// Sketch for meassuring sensor on SB100
#include <ServoTimer2.h>    // the servo library
#include <lcd.h>            // the lcd library

// Debug config
long previousDebug = 0;
boolean debugOn = true;
int debugInterval = 1000;
boolean debugServo = true;


// Pin configuration
//Digital pins
int servoPin  = 13;
int strobePin =  5;
int pickUpPin =  4;
int s0        = 12;
int s1        = 12;
int s2        = 12;
int AD0       =  0;
int AD1       =  1;
int AD2       =  2;
int AD3       =  3;
int AD4       =  4;
int AD5       =  5;

// Constants for logging data
int logInterval = 1000;
long previousLog = 0;

int countPrint = 0;

// Constants for LCD diplay
long previousLCD = 0;
int scrollInterval = 1000;

// constants regarding moving the servo for the webcam
int servoPot  = 11;           
int pulsWidthMin   = 1200;
int pulsWidthMax   = 1800;
boolean servoDirection = 0;

ServoTimer2 servo; 
int deltaPulsWidth = pulsWidthMax - pulsWidthMin;
int pulsWidth = deltaPulsWidth/2; // this will be overidden later;

// constants for ADC with multiplexer
int r0 = 0;    //value select pin at the 4051 (s0)
int r1 = 0;    //value select pin at the 4051 (s1)
int r2 = 0;    //value select pin at the 4051 (s2)
int row = 0;   //storing the bin code
int count = 0; //counter for all the adc
int bin [] = {000,1,10,11,100,101,110,111}; //binear
int analogRef [] = {0,0,0,0,0,0,0,0,0,0,0,0,0};


// constants for strobe ...
long previousStrobePuls =  0;
int pulseSpeed          =  0;
int pulsePot            = 12;

// contants for pickup
boolean pickUp = false;


void setup(){
  //Setting pins for servo
  servo.attach(servoPin);     // attach a pin to the servos 
  
  //Setting pins for multiplexer
  pinMode(s0,OUTPUT);
  pinMode(s1,OUTPUT);
  pinMode(s2,OUTPUT);
  
  
  lcd_init(LCD_DISP_ON);
  lcd_clrscr();
  Serial.begin(9600);
}

void loop(){
  readADC();
  moveServo();
  readRPM();
  strobeSpeed();
  sendLoggingData();
  showOnLCD();
  
  if(debugOn){
    debug();
  }
  
}

void readRPM(){
  
  if(digitalRead(pickUpPin)==HIGH){
    pickUp = true;
  }
  else {
    pickUp = false;
  }
  
}

void strobeSpeed(){
  pulseSpeed = 1024-analogRef[pulsePot];
  if(millis()-previousStrobePuls> pulseSpeed/2){
    digitalWrite(strobePin,HIGH);
  }
  if(millis()-previousStrobePuls> pulseSpeed){
    digitalWrite(strobePin,LOW);
    previousStrobePuls = millis();
    
  }
  
}


void moveServo(){
  if(servoDirection){
    pulsWidth = (deltaPulsWidth * analogRef[servoPot])/1024 + pulsWidthMin;
  }
  else{
    pulsWidth = pulsWidthMax - (deltaPulsWidth * analogRef[servoPot])/1024 ;
  }
  servo.write(pulsWidth);
  
}

void readADC(){
 for(count = 0; count<8;count++){
  row = bin[count];
  r0 = row & 0x01; 
  r1 = (row>>1) & 0x01;
  r2 = (row>>2) & 0x01;
  
  digitalWrite(s0,r0);
  digitalWrite(s1,r0);
  digitalWrite(s2,r0);
  analogRef[count] = analogRead(AD0);
 }
 
 analogRef[count++] = analogRead(AD1);
 analogRef[count++] = analogRead(AD2);
 analogRef[count++] = analogRead(AD3);
 analogRef[count++] = analogRead(AD4);
 analogRef[count++] = analogRead(AD5);
}

void sendLoggingData(){
  if(millis()-previousLog> logInterval)
    for(countPrint = 0; countPrint<12;countPrint++){
       Serial.print(analogRef[countPrint]);
       Serial.print(",");
    }
   Serial.println(analogRef[countPrint++]);
   previousLog = millis();
}
  
void showOnLCD(){
  if(millis()-previousLCD> scrollInterval){  
   lcd_puts("P. Suction: ");   
   //lcd_puts(analogRef[0]);
   lcd_puts(" bar");
  }
 
  if(millis()-previousLCD> 2*scrollInterval){   
   lcd_puts("P. Inlet: ");   
   //lcd_puts(analogRef[1]);
   lcd_puts(" bar");
   previousLCD = millis();
  }     
   
}  

void debug(){
   if(millis()-previousDebug> debugInterval){ 
     if(debugServo){
       Serial.print("Servo pos: ");
       Serial.println(pulsWidth);
       Serial.print("Potmeter pos: ");
       Serial.println(analogRef[servoPot]);
     }
     
       previousDebug = millis();
   }
}

I think I will use interupt on the pulse in signal, i wil post the code if I get evrything to work…

I think this:

int bin [] = {000,1,10,11,100,101,110,111}; //binear

Should be this:

byte bin [] = {B000,B1,B10,B11,B100,B101,B110,B111}; //binary

…maybe :slight_smile: