Noobie needs programming help for New Media art project ASAP

So before I go into what I'm trying to do, here's a picture of what I have set up hardware wise (there's some problem with the resistor I'm using because it just smoked out--everything else is fine though so I shall figure that out later, or now if you want to tell the correct resistor).

Alright. So essentially what I'm trying to do is have the IR sensor control (inconsistent little thing) the motor speed dependent upon the distance between the viewer and the sensor. The motor is going to spin a peg that fits in the motor that will have hemp strings attached that will fling paint inside a paper enclosure; the electronic components will be safe in a box. Anyways, the closer you are, the faster it will sling paint about. Simple enough concept, easy enough for me to set up the hardware. I just keep getting tripped up on the code.

Currently I've slopped together some various codes (a tip120 code a teacher gave me one time and Lucky Larry's IR distance code) that I've used to test the circuit and make sure everything is working, with some if, else statements. None of which is functional as I have no idea how to connect the sketches. Help me clean this mess of code up to be something functional please.

 const int transistorPin = 9;    // connected to the base of the transistor, also ledPin?
 int IRpin = 1;
 const int threshold = 70;   //at this point one will be just out of the range of stupid kinds of close
 const int threshold2 = 400;   // between distance
 const int threshold3 = 7000;   //far away


 void setup() {
  Serial.begin(9600);
  // set  the transistor pin as output:
  pinMode(transistorPin, OUTPUT);
 }

 void loop() {
  
 float volts = analogRead(IRpin)*0.0048828125;   // value from sensor * (5/1024) - if running 3.3.volts then change 5 to 3.3
  float distance = 65*pow(volts, -1.10);          // worked out from graph 65 = theretical distance / (1/Volts)S - luckylarry.co.uk
  Serial.println(distance);                       // print the distance
  delay(100);  // arbitary wait time.
 }
 
  // if the someone isn't close enough, slow down:
  if (analogValue > threshold) {
    digitalWrite(transistorPin, (voltageoutput=10here));
  }
   // if someone is even further away, slow down more:
  if (analogValue > threshold && analogValue > threshold2) {
    digitalWrite(transistorPin, (voltageoutput=8here));
  } 
    // if someone is stupid kinds of far away, slow down to lowest voltage possible to have the motor still running:
  if (analogValue > threshold && analogValue > threshold2 && analogValue > threshold3) {
    digitalWrite(transistorPin, (voltageoutput=6here));
  } 
 //if some is close, go fast: 
  else {
    digitalWrite(ledPin,(voltageoutput=12here)); 
  }

Moderator edit: CODE TAGS

Any help will be much appreciated. Thank you.

First and formost, unless I am completely mistaken, you cannot power your Arduino the way you are doing it. I don't have one in front of me but I am pretty certain you are bypassing the Voltage regulator by putting 12V directly into the 5V pin.

That 5V pin comes from the on board regulator and you can use that pin to power other 5V devices in parrallel. It is not designed to supply power to the Arduino. If you haven't already lost the magic smoke from your board, you are likely to very soon if you actually plug in the battery. You can plug regulated 5V into this pin to power it but this is not what you are doing.

I could be wrong about that and I'm sure someone will correct me if I am.

What you should do for now is plug the Arduino in via USB since you will need this to program anyway. You can and should still use the 12V battery for the Motor, there is just no reason for this 12V to ever go back to the Arduino. However, you do need to tie the grounds together from the Arduino and the 12V supply.

Once you fix this, what I would do is ignore the IR sensor at first and just get the motor to spin using fixed values. Once you know that works, then tackle the IR sensor.

There is a great series of tutorials on Youtube that I would recommend watching to get you going in the right direction. I can't link youtube directly from work but search for sciguy14 and look for his arduino tutorial series.

It's actually not going back to the Arduino; it's hooked up as an external power supply. The 5v pin is supplying me with some base power for my IR sensor and the like, and then the power supply is going exclusively to the motor. Also, I do have the Ardunio hooked up to my laptop for the basic power source; fritzing just doesn't have a MacBook Pro.

I'm not a complete noob; I just really suck at code.

I will check out those videos though, thank you.

It has been my experience that matching opening and closing braces is easier when code looks like

void loop()
{
}

then when it looks like

void loop(){
}

It makes no difference to the compiler, but it does to people.

Similarly, indenting consistently makes it easier to see a block of code. The IDE provides a means of cleaning up improperly indented code - Tools + Auto format. I'd suggest that you use it.

I'd also suggest that you try compiling code before posting it, and don't bother posting it if it won't compile. Yours will not. You have a lot of code outside of loop().

For what you are trying to do, is the exact distance of any value? That is what you are trying to determine with this code:

 float volts = analogRead(IRpin)*0.0048828125;   // value from sensor * (5/1024) - if running 3.3.volts then change 5 to 3.3
  float distance = 65*pow(volts, -1.10);          // worked out from graph 65 = theretical distance / (1/Volts)S - luckylarry.co.uk

You get a reading from the analog pin if something is right in front of the sensor and another reading if there is nothing for 5 feet in front of the sensor. Those two values will bound your speed. Simply map the actual reading using those values as the from range and 0 to 255 as the to range.

The result will be a non-linear, but does that really matter?

You then want to connect the transistor to a PWM pin, and use analogWrite() to change the speed of the motor (assuming that the transistor is appropriate for the current and voltage of the motor). The motor will be powered/unpowered for various amounts of time, but the appearance will be that of running at different speeds. Of course, the different speeds will only really be apparent if there is some load on the motor.

I went about changing thresholds, ridding myself of the ir distance code, and tidying up the format. It obviously still doesn't work, but that's why I came here--to work on getting it functional and understanding why it became that way when I changed what I did.

You then want to connect the transistor to a PWM pin, and use analogWrite() to change the speed of the motor (assuming that the transistor is appropriate for the current and voltage of the motor). The motor will be powered/unpowered for various amounts of time, but the appearance will be that of running at different speeds. Of course, the different speeds will only really be apparent if there is some load on the motor.

Alright, so considering the pin is plugged into the PWM (digital) pin, how analogWrite go about changing anything? Would that involving be plugged in the analog side? And, also, how would I program it to change speeds. I really only know how to code for something to turn on and off and some flashy led stuff--so knowing what to put after the write statement and why it works would be helpful

I only ask because I want to fully understand why I'm doing what I'm doing, otherwise--what's the point? I won't learn anything; it won't stick.

Alright, so considering the pin is plugged into the PWM (digital) pin, how analogWrite go about changing anything?

Have you looked at the documentation for analogWrite()?

Would that involving be plugged in the analog side?

No. analogWrite() was a lousy name for the function, but we didn't get to vote...

I only ask because I want to fully understand why I'm doing what I'm doing, otherwise--what's the point? I won't learn anything; it won't stick.

Good approach/attitude. If, after reading the documentation, and trying out the analogWrite() function, you still have issues/questions, feel free to ask again. And again, if that is what it takes. We prefer that people understand what they are making the Arduino do. If you do, then you can more easily make it do other things.

I It's actually not going back to the Arduino; it's hooked up as an external power supply. The 5v pin is supplying me with some base power for my IR sensor and the like, and then the power supply is going exclusively to the motor. Also, I do have the Ardunio hooked up to my laptop for the basic power source; fritzing just doesn't have a MacBook Pro.

I see it now. That red wire all the way on the end connecting the two sides is what threw me. Sorry bout that.