Make train run using UNO

I am trying to make a train run around a track until it goes over a photoresistor, where it will stop. I seem to be having some trouble with the coding. The train runs just fine with just a voltage source and no code, but when I connect it to the circuit, it doesn't run because of the code. Any suggestions?

Post your code, use the code tag </> button, and how things are wired up.

3 suggestions. Post your code. Post a schematic of your circuit. Read the "how to use the forum" stickies to see how to ask questions, format and post code.

/*
  AnalogReadSerial
  Reads an analog input on pin 0, prints the result to the serial monitor.
  Graphical representation is available using serial plotter (Tools > Serial Plotter menu)
  Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

  This example code is in the public domain.
*/
float voltage=0;
// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
pinMode(13, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
//  sensorValue =  sensorValue*(5/1023);
  // print out the value you read:
  voltage=sensorValue*(5/1023);
  Serial.println(voltage);
  delay(1);        // delay in between reads for stability
{
 
if(A0<4.8)
digitalWrite(13, HIGH);
else(A0>=4.8);
digitalWrite(13, LOW);
}}

Here is the circuit

IMG_4808-2.pdf (996 KB)

IMG_4808-3.pdf (41.8 KB)

if(A0<4.8)
digitalWrite(13, HIGH);
else(A0>=4.8);
digitalWrite(13, LOW);

I'd concentrate on fixing that bit first.

In my experience, 14 is always greater than 4.8

And besides core errors like that, how do you think to drive the train? Don't see it in the schematic image... (And you didn't post a schematic)

There is another Thread about a similar project here

...R

if(A0<4.8)

digitalWrite(13, HIGH);
else(A0>=4.8);
digitalWrite(13, LOW);

Semicolons (;) are not just decoration for the end of the line. If you were expecting that the last digital write would be executed only when (A0<4.8) evaluates to false, then you have put the semicolon in the wrong place. As it is written, that digital write will always occur, so the train will always run (or always stop, I didn't look at the wiring too closely.)

The expression (A0>=4.8) in the else clause does nothing. Since you already know that A0 is not less than 4.8, then this could be put as a comment, to remind you that this part of the code is expected to execute in the times when A0 is not less than 4.8. But as written, it just evaluates this expression and does nothing with the result.

This is on top of the observation that A0 is the number of the pin, not the analog value from reading the pin.

[edited by MorganS]I hate it when A0 is less then 4.8) :stuck_out_tongue:

septillion:
I hate it when A0 is less then 4.8) :stuck_out_tongue:

Yes, I fixed the smileys. Thanks for noticing.

Another problem is in the calculation of the voltage; compare the two voltage outputs of the below sketch.

5 / 1023 is always 0 :wink:

void setup()
{
  Serial.begin(115200);


  int sensorValue = analogRead(A0);
  Serial.print("sensorValue: "); Serial.println(sensorValue);

  float voltage;
  voltage = sensorValue * (5 / 1023);
  Serial.print("voltage: "); Serial.println(voltage);
  voltage = sensorValue * (5.0 / 1023);
  Serial.print("voltage: "); Serial.println(voltage);
}

void loop()
{
  // put your main code here, to run repeatedly:

}

Hi,
OPs images.


pic2.jpg

Unfortunately they are not circuit diagrams.
Sorry but we need a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Where do the green and yellow wire go.
Tom... :slight_smile:

septillion:
I hate it when A0 is less then 4.8) :stuck_out_tongue:

as a newby, he probably is not familiar with the pin counting.

the printed labels on the circuit board are mere references. the chip uses different numbers internally.
the IDE is a software interface that trys to make it simple.

your digial pins in the IDE run up to digial pin13, then the analog pins enter, starting at pin 14.

what you did was fill the value of A0 with the pin number.

A0 = 14

typically you would use the first part of yoru sketch to gather all the values.

Switch_value = analogRead(A0) ;

this would now set the value of Switch_value to be the same as the input on pin A0

also, when you do your sketch, in the menu under TOOLS is auto format. tap that once in awhile.

belated.. welcome to the forum and kudu's for posting both, your sketch in code tags and a photo of your circuit.

as a side note, almost no matte what you post for your circuit, we will point out that it is wrong. it is not colored propelry, it is in a progam we make fun of. we would rather see it in a different format.....

dave-in-nj:
as a newby, he probably is not familiar with the pin counting.

Has nothing to do with the pin counting :slight_smile: But MorganS accedentally posted code with smilly code in it :stuck_out_tongue:

And as far as the topic starter, no matter if he knows the difference between the Arduino numbering and what the uC does, he made an error to check the pin number to a value instead of the value read from that pin. As pointed out by MorganS