controlling a toy train with

This is a school project project
We are making a train that stops for 2 seconds in each of 6 stations.
motor s[eed is fixed to half speed
Station is a reflective patch like a sticker at non equal distances put on the track.

schematic I made; showing sensor will be fixed at train front end..

I need some help with the code..
This is what I wrote so far:


void setup()
pinMode(1, INPUT);
pinMode(5,OUTPUT);
SensorValue=pinmode;

void loop() {
int sensorValue = digitalRead(2);

digitalwrite(5,125);
if(sensorValue == 1)

digitalWrite(5,low)
delay 2000;
digitalwrite(5,125);
}

What does pin 1 do?
What does pin 2 do?
What does pin 5 do?
Why don't they have nice names?
What do imagine digitalwrite(5,125);does?

What do you imagine SensorValue=pinmode;does? (if it even compiles)

Why didn't you even try to get your code to compile?

(1) The code is posted incorrectly. I will put the instructions for posting code below.

digitalwrite(5,125); 
  if(sensorValue == 1)

digitalWrite(5,low)
delay 2000;
digitalwrite(5,125);

(2) Capitalization matters. digitalwrite should be digitalWrite.
(3) digitalWrite is documented at digitalWrite() - Arduino Reference. Its value is either HIGH or LOW, not 125. Instead of digitalwrite(5,125); , we should see digitalWrite(5,HIGH); or digitalWrite(5,LOW);
(4) Pins should be named. A pin number of 5 is not useful. There should be something like const byte motorPower = 5; and later digitalWrite(motorPower, HIGH); .
(5) delay is a function. We should see delay(2000) ;
(6) Do you realize that nothing useful can occur during delay(2000) for two seconds? (Yes, there are exceptions, but not for newbies.)
(7) The code set pinMode for pins 1 and 5, but then uses pin 2 in a digitalRead. See why pin names are useful?
(8) The only line within if(sensorValue == 1) is digitalWrite(5,low) , which is also missing a semicolon. You probably intended to have some curly brackets around some of the code. It is a good idea to use curly brackets ALWAYS, even when they are not required.
(9) The setup() function is missing curly brackets.

This code will not compile. The compiler would have told you about some of the mistakes.

Here are the instructions for posting code...

To post code and/or error messages:

  1. Use CTRL-T in the Arduino IDE to autoformat your complete code.
  2. Paste the complete autoformatted code between code tags (the </> button)
    so that we can easily see and deal with your code.
  3. Paste the complete error message between code tags (the </> button)
    so that we can easily see and deal with your messages.
  4. If you already posted without code tags, you may add the code tags by
    editing your post. Do not change your existing posts in any other way.
    You may make additional posts as needed.

Before posting again, you should read the three locked topics at the top of the Programming Questions forum, and any links to which these posts point.

If your project involves wiring, please provide a schematic and/or a wiring diagram and/or a clear photograph of the wiring.

Good Luck!

As well as the questions and comments already made, you need to tell us what Arduino you are using.

...R

Many thanks for all the comments. I will consider and rewrite.

  • I am using digispark USB stump arduino
  • I need 2 find a way to avoid using delay(2000) ..maybe find. other method bcoz during the train stop I need to add a tone later like train whistle..so I understood from Vaj4088 that delay will halt everything...
  • digitalwrite(5,125) I thought will run motor at half speed which is 125 ..looks like I messed it..

Goldenshuttle:

  • I need 2 find a way to avoid using delay(2000)

Have a look at how millis() is used to manage timing without blocking in several things at a time

And I think you meant "to" rather than the numeral "2". A compiler would not tolerate that sort of thing and I don't believe humans should be expected to.

...R

const int analogPin = 0;    // pin that the IR sensor is attached to
 const int MotPin = 1;       // pin that the Motor is attached to
 const int threshold = 400;   // an arbitrary threshold level that's in the range of the analog input

void setup() {
   
   pinMode(MotPin, OUTPUT);// initialize the Motpin as an output:
   
}

void loop() {
  digitalWrite(MotPin, HIGH);// run the motor
   int analogValue = analogRead(analogPin); // check if sensor gets reflection from sticker:

   // if the analog value is high enough, turn motor off for 2 seconds:
   if (analogValue > threshold) {
     

digital write(Motpin,low);
delay(2000);
   } 
   else {
     digitalWrite(ledPin, HIGH); 
   }

    
}

OK here is the second attempt, but being slow minded, I was unable to get rid of Delay..plz help...
here is the schematic after making corrections

Have a look at the "blink without delay" example in the examples provided with the IDE.

When you write your code using the IDE, you can use the menu item, Tools > Auto Format, which will arrange everything in an easy to read format, that will also help you spot logic errors.

Download the example Robin pointed to, compile and run it on a full sized Arduino like the Uno, with all the peripherals (or at least the LEDs). The digispark uses a similar, but less capable chip, the ATTiny 85. While I love the tiny85, it has limitations that you should not need to deal with while learning the essence of the sport.

Once you understand how to use millis() to time a sketch, you can go back to the smaller boards.

ps, variable names are a 'thing' for me. Motpin is not much better than 5 in my book. There is no penalty for using long, descriptive names, like motorEnablePin. The length of the name is only used during compilation. When it runs, it's just code, which is the same if you called it 5 or the longest name you could imagine.

I have tried to use millis() but attempt failed because it uses If-else statement. since I already have if-else statement for the sensor to stop the motor, the two if-else statements clashed and I was unable to solve it..please help/

Post. Your. Code.

here it is: compiles but does not work

 const int analogPin = 0;    // pin that the IR sensor is attached to
 const int MotPin = 1;       // pin that the Motor is attached to
 const int threshold = 400;   // an arbitrary threshold level that's in the range of the analog input
unsigned long previousMillis = 0;        // will store last time LED was updated
const long interval = 2000;           // interval at which motor stops(milliseconds)

void setup() {
   
   pinMode(MotPin, OUTPUT);// initialize the Motpin as an output:
   
}

void loop() {
  unsigned long currentMillis = millis();
  digitalWrite(MotPin, HIGH);// run the motor
   int analogValue = analogRead(analogPin); // check if sensor gets reflection from sticker:

   // if the analog value is high enough, turn motor off for 2 seconds:
   if (analogValue > threshold) {

 
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
 
      digitalWrite (MotPin, LOW);
    } else { 

digitalWrite(MotPin, HIGH);// run the motor
    
   }
    
}}

but does not work

It does something.
You expect it to do something.

All that "it does not work" means is that your expectations are wrong.

To help you correctly align your expectations, we need to know what the code actually does, and what your expectations are.

Try this. And look carefully at the changes I have made.
Notice how the business of stopping is separate from the business of timing.
And the use of a variable to keep track of the state of the system.

const int analogPin = 0;    // pin that the IR sensor is attached to
const int MotPin = 1;       // pin that the Motor is attached to
const int threshold = 400;   // an arbitrary threshold level that's in the range of the analog input
unsigned long locoStopMillis = 0;        // will store last time LED was updated
bool locoStopped = false;
const long interval = 2000;           // interval at which motor stops(milliseconds)

void setup() {
    
    pinMode(MotPin, OUTPUT);// initialize the Motpin as an output:
    
}

void loop() {
    unsigned long currentMillis = millis();
    int analogValue = analogRead(analogPin); // check if sensor gets reflection from sticker:

    // if the analog value is high enough, turn motor off for 2 seconds:
    if (analogValue > threshold) {
        locoStopMillis = millis();
        digitalWrite (MotPin, LOW);
        locoStopped = true;
    }

    if (locoStopped == true) {
        if (currentMillis - locoStopMillis >= interval) {
            digitalWrite(MotPin, HIGH);// run the motor
            digitalWrite (MotPin, HIGH);
            locoStopped = false;
        }
    }
    else {
        digitalWrite(MotPin, HIGH);// run the motor
    }
}

By the way I have not tested it.

...R

@Robin2: thanks for help..it compiles, so I will load, connect the motor & sensor & see the result. to understand it fully, I still need to study the use of bool/True/false bcoz I am not familiar with these...add to it my slow ability to understand.

I have tried this code but it did not work..Motor is just not moving.
I tried changing this

const int analogPin = 0;

to This

const int analogPin = A0;

but also no results...any more hints plz?

Any more code, please?

Goldenshuttle:
but also no results...any more hints plz?

Neither pin 0 nor pin A0 is a PWM pin so I would not expect anything useful to happen. analogWrite() only works with the PWM pins which are usually marked on the Arduino board.

If you want more advice please post a complete program.

...R

This is the code; nothing moving:(

const int analogPin = A0;    // pin that the IR sensor is attached to
const int MotPin = 1;       // pin that the Motor is attached to
const int threshold = 200;   // an arbitrary threshold level that's in the range of the analog input
unsigned long locoStopMillis = 0;        // will store last time LED was updated
bool locoStopped = false;
const long interval = 2000;           // interval at which motor stops(milliseconds)

void setup() {
    
    pinMode(MotPin, OUTPUT);// initialize the Motpin as an output:
    
}

void loop() {
    unsigned long currentMillis = millis();
    int analogValue = analogRead(analogPin); // check if sensor gets reflection from sticker:

    // if the analog value is high enough, turn motor off for 2 seconds:
    if (analogValue > threshold) {
        locoStopMillis = millis();
        digitalWrite (MotPin, LOW);
        locoStopped = true;
    }

    if (locoStopped == true) {
        if (currentMillis - locoStopMillis >= interval) {
            digitalWrite(MotPin, HIGH);// run the motor
            digitalWrite (MotPin, HIGH);
            locoStopped = false;
        }
    }
    else {
        digitalWrite(MotPin, HIGH);// run the motor
    }
}

Why don't you start getting your sketch to tell you what it is doing?
Of course, you'll need to do a little rewiring first.