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:
(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:
Use CTRL-T in the Arduino IDE to autoformat your complete code.
Paste the complete autoformatted code between code tags (the </> button)
so that we can easily see and deal with your code.
Paste the complete error message between code tags (the </> button)
so that we can easily see and deal with your messages.
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.
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..
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.
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
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/
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
}
}}
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
}
}
@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.
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.
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
}
}