0

I have a device power by arduino nano atmega328. The idea of this HW is to detects dropped seed (corn, oil bean etc.) and to count it.

I wrote initial code, but id does not work in an expected way. it increments one count "channel" (I have 8 pairs of IR diodes 3mm) automatically, and passing it does not detect.

Here is the code maybe, someone has some similar experience and maybe help me to refactor the code.

#include<wire.h>
#include<Adafruit_SSD1306.h>

int irPin1 = 1;
int irPin2 = 2;
int irPin3 = 3;
int irPin4 = 4;
int irPin5 = 5;
int irPin6 = 6;
int irPin7 = 7;
int irPin8 = 8;

int count1,count2,count3,count4,count5,count6,count7,count8, count = 0;
boolean state = true;

void setup(){
  Serial.begin(9600);
}

void loop(){
if(!digitalRead(irPin1) && state)
 {
   count1++;
   state = false;
   Serial.print("count1: ");
   Serial.println(count1);
   delay(200);
 }

if(digitalRead(irPin1))
  {
    state=true;
    delay(100);
  }

if(!digitalRead(irPin2) && state)
 {
   count2++;
   state = false;
   Serial.print("count2: ");
   Serial.println(count2);
   delay(200);
 }

if(digitalRead(irPin2))
  {
    state=true;
    delay(100);
  }

  if(!digitalRead(irPin3) && state)
 {
   count3++;
   state = false;
   Serial.print("count3: ");
   Serial.println(count3);
   delay(200);
 }

if(digitalRead(irPin3))
  {
    state=true;
    delay(100);
  }

    if(!digitalRead(irPin4) && state)
 {
   count4++;
   state = false;
   Serial.print("count4: ");
   Serial.println(count4);
   delay(200);
 }

if(digitalRead(irPin4))
  {
    state=true;
    delay(100);
  }

 if(!digitalRead(irPin5) && state)
 {
   count5++;
   state = false;
   Serial.print("count5: ");
   Serial.println(count5);
   delay(200);
 }

if(digitalRead(irPin5))
  {
    state=true;
    delay(100);
  }
 if(!digitalRead(irPin6) && state)
 {
   count6++;
   state = false;
   Serial.print("count6: ");
   Serial.println(count6);
   delay(200);
 }

if(digitalRead(irPin6))
  {
    state=true;
    delay(100);
  }
 if(!digitalRead(irPin7) && state)
 {
   count7++;
   state = false;
   Serial.print("count7: ");
   Serial.println(count7);
   delay(200);
 }

if(digitalRead(irPin7))
  {
    state=true;
    delay(100);
  }

 if(!digitalRead(irPin8) && state)
 {
   count8++;
   state = false;
   Serial.print("count8: ");
   Serial.println(count8);
   delay(200);
 }

if(digitalRead(irPin8))
  {
    state=true;
    delay(100);
  }
count= count1+count2+count3+count4+count5+count6+count7+count8;
}

I work in platformIO.

10
  • 1
    One of the the things that stands out immediately is that you're using the serial TX pin for an IR sensor but also using Serial to send. The way you've asked the question, this isn't exactly an answer though.
    – timemage
    Jun 6 at 20:22
  • 1
    When the delays are active nothing else is. Try using the millies instead.
    – Gil
    Jun 6 at 20:25
  • 2
    See the blinkWithoutDelay example for using millis() instead of delay(). This is also referred to as non-blocking code.
    – RowanP
    Jun 6 at 22:04
  • 1
    Digital pin 1 is used for serial TX. Avoid using it for any other purpose if you need it for serial. Allocate a different free pin for sensor 1.
    – RowanP
    Jun 6 at 22:09
  • 2
    you are trying to run before you can walk ... get this to work with only one sensor ... then try two sensors ... it is a waste of everyone's time, including yours, by asking about eight sensors if you can't get one to work
    – jsotola
    Jun 7 at 0:06

1 Answer 1

1

Use arrays instead of a copy/paste coding style. Use millis() instead of delay() statements to eliminate blocking code. Explicitly configure input pins.

compiles, but untested:

uint8_t irPin[ 8 ] = { 2, 3, 4, 5, 6, 7, 8, 9 } ; // set the sensor pins here . Avoid 0 and 1.
uint16_t seedCount[ 8 ] = { 0 } ;
uint32_t lastSeedAtMs[ 8 ] = { 0 } ;
const uint32_t lockOutTimeMs = 200UL ; // milliseconds - prevent 1 seed generating multiple counts

void setup() {
  Serial.begin(9600);
    for ( uint8_t channel = 0; channel < 8 ; channel++ ) {
      pinMode( irPin[ channel ] , INPUT_PULLUP ) ;  // configure all input pins
    }
}

void loop() {
  for ( uint8_t channel = 0; channel < 8 ; channel++ ) {
    if ( digitalRead( irPin[ channel ] ) == LOW ) {  // sensor active low
      if ( millis() - lastSeedAtMs[ channel ] > lockOutTimeMs )  {  // if X ms passed since last detection on this channel
        seedCount[ channel ] ++ ;
        lastSeedAtMs[channel ] = millis() ;
        Serial.print("count");
        Serial.print(channel);
        Serial.print(": ");
        Serial.println( seedCount[ channel ] );
      }
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.