Assistance with conveyor belt project required

I have created the following code for this project brief but do not currently have the equipment to test it. Looking for advice as to if the code will work or not. Thanks

Code_for_conveyor_1.ino (1.3 KB)

Please edit your post and post the code using code tags rather than attaching it. This will make it much easier to provide help

See How to use this forum

When is our assignment due to be handed in ?

For a start, you probably means this:

digitalRead(sensorValue);

... to be:

sensorValue = digitalRead(sensorPin);
const int sensorPin = A0;    // pin that the sensor is attached to
//const int motorPin = 9;        // pin that the LED is attached to

int sensorValue = 0;         // the sensor value
int sensorMin = 1023;        // minimum sensor value
int sensorMax = 0;           // maximum sensor value
int inPin = 8;               // choose the input pin (for a pushbutton)
int val = 0;                 // variable for reading the pin status
boolean lastButton=LOW;
boolean motorState = LOW;
boolean currentButton = LOW;
void setup() {
  Serial.begin(9600);
  // put your setup code here, to run once:
 // turn on LED to signal the start of the calibration period:
 pinMode(9, OUTPUT);
 pinMode(8, INPUT);    // declare pushbutton as input
 digitalWrite(9, LOW);
}
void loop() {
 currentButton=digitalRead(8);
 if (lastButton != currentButton)
 {delay(5);
 currentButton = digitalRead(8);
 
 }  
  
 if (currentButton == HIGH && lastButton == LOW)
 {
  motorState = !motorState;
 }
 {
 digitalWrite (9, motorState);
 lastButton = currentButton;
 

  // put your main code here, to run repeatedly:
// read the sensor:
digitalRead(sensorValue);
}
if (sensorValue > 1000){

digitalWrite (9,LOW);
delay(2000);
}
else {
  digitalWrite (9,HIGH);

 Serial.println(sensorValue);
delay(2000);
 }
}[code]

hi Brian
I could detect a couple a mistakes

//const int motorPin = 9;

it should be

const int motorPin = 9;

and the other one

digitalRead(sensorValue);

for this you have to declare a variable (let it be sensorPIN) for the sensor pin and then it would be

digitalRead(sensorPIN) = sensorValue;

cheers

I am not sure if there is a beginners guide to the concepts of writing code.

my personal strategy is :

in loop()
I read everything first. all of the inputs.

then do all the logic and set variables.

then, at the end of loop() use those variables to change pins, outputs, etc

then, and the very end, do the things like lastButton = currentButton;

it does add more fields, takes bit more memory.

as for delay()...
this can be used well. or cause problems.
you look at the input, might check it twice in 5ms
then delay(2000)

so, you check it twice in 5ms, every 2 seconds.

if this works, then all is fine. if you find there is a delay you don't like, then "blink without delay" would be the best solution.
it is an example in your IDE examples.

as for not having a conveyor to work with, that is not a problem.
you can use LED's to visualize signals out.

one for the button high/low
one for the motor signal off/on
that often helps you get a feel for operations.

if you have a tiny motor, the spinning gives the feel for spinning a larger motor.

brian_crowley11:
I ... do not currently have the equipment to test it.

I see a bit of a logic error in the sensor part, but tbh I think it would be better for you to get your hands on the kit and figure it out for yourself.

Will not affect the functioning of the code, but you assigned the input & output pin numbers to variables at the beginning of the sketch, but then use the actual pin numbers in the code instead of the variable names. The whole point of assigning the pin numbers to variables is so that you can use the variable names in the code, and to make it easy to change the pin number if needed, since you would then only have to change it in the variable declaration instead of throughout the entire code.

I do see a problem with the code, but it will show up immediately once you have the equipment to run some tests. (I see someone else has pointed this particular problem out already)

You might want to do some research on state machines, this type of task is well suited to that style of logic, otherwise you get into having to set variables to indicate which part of the cycle you are currently in. As an example, you really don't care what the input from the IR sensor is unless you have already pressed the button and the conveyor belt is running, or pressing the button after the conveyor has started should have no effect until the entire process has completed.

Thanks for the help. This should help solve the problem

dave-in-nj:
as for not having a conveyor to work with, that is not a problem.
you can use LED's to visualize signals out.

Agreed: I've written a complete system for this washing machine project using just leds to mimic the output.

david_2018:
You might want to do some research on state machines

That would be time really well spent. Do it now before it's too late!

mrgoelrox:
for this you have to declare a variable (let it be sensorPIN) for the sensor pin and then it would be

digitalRead(sensorPIN) = sensorValue;

Ummm, No! :astonished:

"I have created the following code for this project brief but do not currently have the equipment to test it."

You actually need very little hardware wise to develop your code. Some hookup wire and a couple of jumpers, a pot for analog input testing, maybe an inexpensive Harbor Freight red multimeter for measuring analog voltages and high/low on pins, and the serial monitor. With that you can start developing and verifying your code as you go.