Hi all,
I have been tasked with getting a bulb (the ones that go in the ground, not lightbulbs) counting machine to work after its old electronics were given an unexpected 240V.
The machine itself is not vastly complicated. It has an angled vibrating bed with two speeds. Bulbs vibrate down the bed and as they fall from the end hit a lever which briefly uncovers one of 7 optical interrupters. There are 7 runs hence 7 optical interrupters.
Before the machine is started you enter a value on two bcd thumbwheels (anywhere from 0 to 99).
It should then count bulbs until it hits 75 percent of the bcd thumbwheel value at which point it turns off one of the vibration speed relays and slows the vibration, the last 25 percent of the bulbs are then counted until the predefined value and then the vibration stops. Once it stops a 20ms pulse is sent to a relay which in turn sends 24v to a mechanical counter which adds 1 to the mechanical counter total.
The process completes when a door is opened, the bulbs fall into a bag, when the door is closed it hits a microswitch, which resets the arduino and the process starts again.
The microswitch for reset is connected between GND and RESET on the arduino so it does truly reset the device everytime the door is closed.
This has become my job because I am the "IT Guy", this is by no means my forte and I am struggling.
I have for the most part got it working.
I have read into the many discussions about interrupts versus polling and INT versus PCINT, and have struggled to get my head around it. I am using an Arduino mega 2560 and couldn't really get my head around which pins I could and couldn't use. The machine doesn't go particularly fast (maybe 3 bulbs a second across all 7 pins) and as such I am hoping polling will be quick enough to not miss any counts. Using an example I found online I have modified the example StateChangeDetection sketch to work with an array of pins (I am not certain I have done this correctly).
I have cobbled together the following code which does work in terms of getting the values from the bcd thumbwheels, calculaing 75% of bcd value, setting both relays to high (which I chose to do before in setup rather than in loop as they are always going to be needed as soon as the machine starts) and polling the 7 pins (I think).
In order to calculate 75% of the bcd total I did what looks to be some horrible code. It leaves me in a position that this code will compile but with warnings that say.
/home/sam/Arduino/Bulb-Counter/Bulb-Counter.ino: In function 'void loop()':
/home/sam/Arduino/Bulb-Counter/Bulb-Counter.ino:205:20: warning: ISO C++ forbids comparison between pointer and integer [-fpermissive]
if (bulbcounter >= bcdtot75) digitalWrite(vibrelay2, LOW);
^~~~~~~~
/home/sam/Arduino/Bulb-Counter/Bulb-Counter.ino:206:20: warning: ISO C++ forbids comparison between pointer and integer [-fpermissive]
if (bulbcounter >= bcdtot)
^~~~~~
/home/sam/Arduino/Bulb-Counter/Bulb-Counter.ino:207:20: warning: ISO C++ forbids comparison between pointer and integer [-fpermissive]
if (bulbcounter >= bcdtot) {
^~~~~~
I am in a situation where I understand the words being used but cannot make head not tail of what to do about it. Which one is a pointer? Why is it a pointer? Why did I do this? You know that feeling where you've spent two days with a soldering iron in hand thinking you were doing hard bit until you sat down to write the code and realized just how little you know.
I would massively appreciate someone having a look at the below code and telling me if it makes any sense, what I have done wrong and what I should do about it. I am aware my code is not very pretty, it is commented please feel free to improve any or all of it.
Sorry for the long post, having spent my life in IT there is nothing more annoying that too little information.
Thank you
/* Bulb Counting Machine
/* Define Pins For BCD PushWheels */
#define tq1 2
#define tq2 3
#define tq4 4
#define tq8 5
#define uq1 6
#define uq2 7
#define uq4 8
#define uq8 9
/* End Define for BCD PushWheels */
/* Define optipins Array of optical interrupters */
int optipins[7] = {46, 47, 48, 49, 50, 51, 52}; // determine the amount of inputs and the input pins
int optival[7] = {0}; // variable for reading the opti pin status
int currentState[7] = {0}; // for comparison with previousstate
int previousState[7] = {0}; // for comparison with currentstate
int bulbcounter = 0; // only one counter needed hence not an array of counters
/* Define Pins For Vibration Bed Relays */
#define vibrelay1 44
#define vibrelay2 45
/* End Define Pins for vibration Bed Relays */
/* Define Pin for 24v Mechanical Counter Relay */
#define mechcount 43
/* Define Pin for 24v Mechanical Counter Relay */
/* DEFINE LED PIN FOR COMPLETED COUNT - CURRENTLY SET FOR ON BOARD DIAG LED */
#define LED_PIN 13
/* SET INITIAL STATE ON LED */
int ledState = LOW;
/* Begin Setup */
void setup()
{
Serial.begin(115200);
delay(50);
/* Set BCD Pins to be INPUTS */
/* TENS PushWheel */
pinMode(tq1, INPUT); // tens-thumbwheel '1'
pinMode(tq2, INPUT); // tens-thumbwheel '2'
pinMode(tq4, INPUT); // tens-thumbwheel '4'
pinMode(tq8, INPUT); // tens-thumbwheel '8'
/* UNITS PushWheel */
pinMode(uq1, INPUT); // units-thumbwheel '1'
pinMode(uq2, INPUT); // units-thumbwheel '2'
pinMode(uq4, INPUT); // units-thumbwheel '4'
pinMode(uq8, INPUT); // units-thumbwheel '8'
/* End Define BCD Pins */
/* Set Optical Interrupter Array to be INPUTS */
for ( byte i = 0 ; i < 7 ; i ++ ) {
pinMode(optipins[i], INPUT); // declare optipins as input
}
/* End Define Optical Interrupter Array as INPUTS */
/* Set Mechcount Pin to be Output */
pinMode(mechcount, OUTPUT);
/* End Mechchount Pin Definition */
/* Set Vibration Bed Relay Pin to be OUTPUT */
pinMode(vibrelay1, OUTPUT); // sets vibrelay1 pin as output
pinMode(vibrelay2, OUTPUT); // sets vibrelay2 pin as output
/* End Vibration Bed Relay */
/* Start Vibration at HIGH - So machine starts in high vibration mode*/
digitalWrite(vibrelay1, HIGH);
digitalWrite(vibrelay2, HIGH);
Serial.println("POWER ON RELAYS BOTH HIGH");
/* End Vibration at HIGH */
}
/* CREATE INT CALLED bcdtot and CALCULATE VALUE */
int bcdtot()
{
/* ADD UP TENS PUSHWHEEL */
int ttot=0;
if (digitalRead(tq1)==HIGH) { ttot+=1; }
if (digitalRead(tq2)==HIGH) { ttot+=2; }
if (digitalRead(tq4)==HIGH) { ttot+=4; }
if (digitalRead(tq8)==HIGH) { ttot+=8; }
/* ADD UP UNITS PUSHWHEEL */
int utot=0;
if (digitalRead(uq1)==HIGH) { utot+=1; }
if (digitalRead(uq2)==HIGH) { utot+=2; }
if (digitalRead(uq4)==HIGH) { utot+=4; }
if (digitalRead(uq8)==HIGH) { utot+=8; }
/* MULTIPLY TENS BY 10 AND ADD UNITS */
int adtot=ttot*10+utot;
return adtot;
}
/* END bcdtot */
/* CREATE INT CALLED bcdtot75 and CALCULATE VALUE as 75% of bcdtot. An int is fine here as whole numbers are all I need
I am certain this is an ugly way to get 75% of bcdtot. Unless, I have completely misunderstood the use on an int. which is completely possible*/
int bcdtot75()
{
int bcdtotper=bcdtot() * 0.75;
return bcdtotper;
}
/* END SETUP */
/* BEGIN LOOP */
void loop()
{
if (bulbcounter >= bcdtot75) digitalWrite(vibrelay2, LOW);
if (bulbcounter >= bcdtot) {
digitalWrite(vibrelay1, LOW);
digitalWrite(mechcount, HIGH);
delay (20);
digitalWrite(mechcount, LOW);
}
for ( byte i = 0 ; i < 7 ; i ++ ) {
optival[i] = digitalRead(optipins[i]); // read input value
if (optival[i] == HIGH) { // check if the input is HIGH (optitriggered released)
currentState[i] = 1;
}
else {
currentState[i] = 0;
}
if (currentState[i] != previousState[i]) {
if (currentState[i] == 1) {
bulbcounter = bulbcounter + 1;
//Serial print
Serial.print(bulbcounter);
Serial.print(" ; ");
}
}
previousState[i] = currentState[i];
}
}
/* END LOOP */