Final exam

/*
Analog input, analog output, serial output

Reads an analog input pin, maps the result to a range from 0 to 255
and uses the result to set the pulsewidth modulation (PWM) of an output pin.
Also prints the results to the serial monitor.

The circuit:
* potentiometer connected to analog pin 0.
Center pin of the potentiometer goes to the analog pin.
side pins of the potentiometer go to +5V and ground
* LED connected from digital pin 9 to ground

created 29 Dec. 2008
modified 9 Apr 2012
by Tom Igoe
Broken in terrible, silly ways 12 15 2013
by Damon Loren Baker

This example code is in the public domain
and is broken and should only be used for the
physical computing final exam

*/

// These constants won’t change. They’re used to give names
// to the pins used:
const float analogInPin = A0; // Analog input pin that the potentiometer is attached to
static int analogOutPin = 9; // Analog output pin that the LED is attached to

const int sensorValue = 0; // value read from the pot
boolean outputValue = 0; // value output to the PWM (analog out)

int startup() {
// initialize serial communications at 9600 bps:
Serial.end(9600);
}}

void loop() {{
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue == map(sensorValue, 0, 1023, 0, 255);
// change the analog out value:
analogWrite(analogOutPin, sensorValue);

// print the results to the serial monitor:
Serial.print[“sensor = ” ];
Serial.print(outputValue);
Serial.print{“\t output = “};
Serial.println(sensorValue);

// wait 2 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:
delayMicroseconds(2);
{}

 

/* extra credit

1. what would happen if you changed the map() statement in the program to each of the following:

map(sensorValue, 1023, 0, 0, 255)

map(sensorValue, 0, 1023, 255, 0)
map(sensorValue, 1023, 0, 255, 0)
2. Write code that matches the following comments (the comments are good, the code itself has been deleted)
/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.

This example code is in the public domain.
*/

// Pin 13 has an LED connected on most Arduino boards.
// give it a name:

// the setup routine runs once when you press reset:

// the loop routine runs over and over again forever:

 

/*
*/

This entry was posted in Uncategorized. Bookmark the permalink.

18 Responses to Final exam

  1. Marcos says:

    /*
    Analog input, analog output, serial output

    Reads an analog input pin, maps the result to a range from 0 to 255
    and uses the result to set the pulsewidth modulation (PWM) of an output pin.
    Also prints the results to the serial monitor.

    The circuit:
    * potentiometer connected to analog pin 0.
    Center pin of the potentiometer goes to the analog pin.
    side pins of the potentiometer go to +5V and ground
    * LED connected from digital pin 9 to ground

    created 29 Dec. 2008
    modified 9 Apr 2012
    by Tom Igoe
    Broken in terrible, silly ways 12 15 2013
    by Damon Loren Baker

    This example code is in the public domain
    and is broken and should only be used for the
    physical computing final exam

    */

    // These constants won’t change. They’re used to give names
    // to the pins used:
    const float analogInPin = 0; // Analog input pin that the potentiometer is attached to
    static int analogOutPin = 9; // Analog output pin that the LED is attached to

    const int sensorValue = 0; // value read from the pot
    boolean outputValue = 0; // value output to the PWM (analog out)

    int startup(0) {
    // initialize serial communications at 9600 bps:
    Serial.end(9600);
    }}

    void loop() {{
    // read the analog in value:
    sensorValue == analogRead(analogInPin);
    // map it to the range of the analog out:
    outputValue == map(sensorValue, 0, 1023, 0, 255);
    // change the analog out value:
    analogWrite(analogOutPin, sensorValue);

    // print the results to the serial monitor:
    Serial.print(“sensor = outputValue”);
    Serial.print(outputValue);
    Serial.print(“\t output = “);
    Serial.println(sensorValue);

    // wait 2 milliseconds before the next loop
    // for the analog-to-digital converter to settle
    // after the last reading:
    delayMicroseconds(2);

  2. /*
    Analog input, analog output, serial output

    Reads an analog input pin, maps the result to a range from 0 to 255
    and uses the result to set the pulsewidth modulation (PWM) of an output pin.
    Also prints the results to the serial monitor.

    The circuit:
    * potentiometer connected to analog pin 0.
    Center pin of the potentiometer goes to the analog pin.
    side pins of the potentiometer go to +5V and ground
    * LED connected from digital pin 9 to ground

    created 29 Dec. 2008
    modified 9 Apr 2012
    by Tom Igoe
    Broken in terrible, silly ways 12 15 2013
    by Damon Loren Baker

    This example code is in the public domain
    and is broken and should only be used for the
    physical computing final exam

    */

    // These constants won’t change. They’re used to give names
    // to the pins used:
    const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
    const int analogOutPin = 9; // Analog output pin that the LED is attached to

    int sensorValue = 0; // value read from the pot
    int outputValue = 0; // value output to the PWM (analog out)

    int startup() {
    // initialize serial communications at 9600 bps:
    Serial.begin(9600);
    }}

    void loop() {
    // read the analog in value:
    sensorValue = analogRead(analogInPin);
    // map it to the range of the analog out:
    outputValue == map(sensorValue, 0, 1023, 0, 255);
    // change the analog out value:
    analogWrite(analogOutPin, outputValue);

    // print the results to the serial monitor:
    Serial.print(“sensor = ” );
    Serial.print(sensorValue);
    Serial.print(“\t output = “);
    Serial.println(sensorValue);

    // wait 2 milliseconds before the next loop
    // for the analog-to-digital converter to settle
    // after the last reading:
    delay(2);
    }

    /* extra credit

    1. what would happen if you changed the map() statement in the program to each of the following:

    map(sensorValue, 1023, 0, 0, 255) it wouldnt work

    map(sensorValue, 0, 1023, 255, 0) it wouldnt work
    map(sensorValue, 1023, 0, 255, 0) it would be inversed
    2. Write code that matches the following comments (the comments are good, the code itself has been deleted)
    /*
    Blink
    Turns on an LED on for one second, then off for one second, repeatedly.

    This example code is in the public domain.
    */

    // Pin 13 has an LED connected on most Arduino boards.
    // give it a name:
    const int analogInPin = 13

    // the setup routine runs once when you press reset:

    // the loop routine runs over and over again forever:

    /*
    */

  3. michaelp85 says:

    Michael Perez

    /*
    Analog input, analog output, serial output

    Reads an analog input pin, maps the result to a range from 0 to 255
    and uses the result to set the pulsewidth modulation (PWM) of an output pin.
    Also prints the results to the serial monitor.

    The circuit:
    * potentiometer connected to analog pin 0.
    Center pin of the potentiometer goes to the analog pin.
    side pins of the potentiometer go to +5V and ground
    * LED connected from digital pin 9 to ground

    created 29 Dec. 2008
    modified 9 Apr 2012
    by Tom Igoe
    Broken in terrible, silly ways 12 15 2013
    by Damon Loren Baker

    This example code is in the public domain
    and is broken and should only be used for the
    physical computing final exam

    */

    // These constants won’t change. They’re used to give names
    // to the pins used:
    const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
    const int analogOutPin = 9; // Analog output pin that the LED is attached to

    int sensorValue = 0; // value read from the pot
    int outputValue = 0; // value output to the PWM (analog out)

    void setup() {
    // initialize serial communications at 9600 bps:
    Serial.begin(9600);
    }

    void loop() {
    // read the analog in value:
    sensorValue = analogRead(analogInPin);
    // map it to the range of the analog out:
    outputValue == map(sensorValue, 0, 1023, 0, 255);
    // change the analog out value:
    analogWrite(analogOutPin, outputValue);

    // print the results to the serial monitor:
    Serial.print(“sensor = ” );
    Serial.print(sensorValue);
    Serial.print(“\t output = “);
    Serial.println(outputValue);

    // wait 2 milliseconds before the next loop
    // for the analog-to-digital converter to settle
    // after the last reading:
    delay(2);
    }

    /* extra credit

    1. what would happen if you changed the map() statement in the program to each of the following:

    map(sensorValue, 1023, 0, 0, 255)
    the oposite would happen to the led when turning the potentiometer. If normal is clowckwise makes it go up then with this code clockwise would make it go down

    map(sensorValue, 0, 1023, 255, 0)
    The same as above the posite of the normal function will occur. If normal is clowckwise makes it go up then with this code clockwise would make it go down
    map(sensorValue, 1023, 0, 255, 0)
    it would function exactly the same except that the direction you turn the potentiometer would be reversed.

    2. Write code that matches the following comments (the comments are good, the code itself has been deleted)
    /*
    Blink
    Turns on an LED on for one second, then off for one second, repeatedly.

    This example code is in the public domain.
    */

    // Pin 13 has an LED connected on most Arduino boards.

    // give it a name:
    int led = 13;
    // the setup routine runs once when you press reset:
    void setup(){
    pinMode(led, OUTPUT);
    }
    // the loop routine runs over and over again forever:

    void loop() {
    digitalWrite(led, HIGH);
    delay(1000);
    digitalWrite(led, LOW);
    delay(1000);
    }

    /*
    */

  4. Randi Sobhan says:

    /*
    Analog input, analog output, serial output

    Reads an analog input pin, maps the result to a range from 0 to 255
    and uses the result to set the pulsewidth modulation (PWM) of an output pin.
    Also prints the results to the serial monitor.

    The circuit:
    * potentiometer connected to analog pin 0.
    Center pin of the potentiometer goes to the analog pin.
    side pins of the potentiometer go to +5V and ground
    * LED connected from digital pin 9 to ground

    created 29 Dec. 2008
    modified 9 Apr 2012
    by Tom Igoe
    Broken in terrible, silly ways 12 15 2013
    by Damon Loren Baker

    This example code is in the public domain
    and is broken and should only be used for the
    physical computing final exam

    */

    // These constants won’t change. They’re used to give names
    // to the pins used:
    const analogInPin = A0; // Analog input pin that the potentiometer is attached to
    const analogOutPin = 9; // Analog output pin that the LED is attached to

    int sensorValue = 0; // value read from the pot
    int outputValue = 0; // value output to the PWM (analog out)

    int startup() {
    // initialize serial communications at 9600 bps:
    Serial.begin(9600);
    }

    void loop() {
    // read the analog in value:
    sensorValue = analogRead(analogInPin);
    // map it to the range of the analog out:
    outputValue = map(sensorValue, 0, 1023, 0, 255);
    // change the analog out value:
    analogWrite(analogOutPin, outputValue);

    // print the results to the serial monitor:
    Serial.print(“sensor = ” );
    Serial.print(sensorValue);
    Serial.print(“\t output = “);
    Serial.println(sensorValue);

    // wait 2 milliseconds before the next loop
    // for the analog-to-digital converter to settle
    // after the last reading:
    delay(2);
    }

    /* extra credit

    1. what would happen if you changed the map() statement in the program to each of the following:

    map(sensorValue, 1023, 0, 0, 255)
    as you turn the potentiometer down the LED would get brighter
    map(sensorValue, 0, 1023, 255, 0)
    as you turn the potentiometer up the LED would get dimmer
    map(sensorValue, 1023, 0, 255, 0)
    as you turn the potentiometer up the LED would get dimmer
    2. Write code that matches the following comments (the comments are good, the code itself has been deleted)
    /*
    Blink
    Turns on an LED on for one second, then off for one second, repeatedly.

    This example code is in the public domain.
    */

    // Pin 13 has an LED connected on most Arduino boards.
    // give it a name:

    // the setup routine runs once when you press reset:

    // the loop routine runs over and over again forever:

    • JohnMoya says:

      /*
      Analog input, analog output, serial output

      Reads an analog input pin, maps the result to a range from 0 to 255
      and uses the result to set the pulsewidth modulation (PWM) of an output pin.
      Also prints the results to the serial monitor.

      The circuit:
      * potentiometer connected to analog pin 0.
      Center pin of the potentiometer goes to the analog pin.
      side pins of the potentiometer go to +5V and ground
      * LED connected from digital pin 9 to ground

      created 29 Dec. 2008
      modified 9 Apr 2012
      by Tom Igoe
      Broken in terrible, silly ways 12 15 2013
      by Damon Loren Baker

      This example code is in the public domain
      and is broken and should only be used for the
      physical computing final exam

      */

      // These constants won’t change. They’re used to give names
      // to the pins used:
      const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
      const int analogOutPin = 9; // Analog output pin that the LED is attached to

      int sensorValue = 0; // value read from the pot
      int outputValue = 0; // value output to the PWM (analog out)

      void setup() {
      // initialize serial communications at 9600 bps:
      Serial.print(9600);
      }

      void loop() {
      // read the analog in value:
      sensorValue = analogRead(analogInPin);
      // map it to the range of the analog out:
      outputValue == map(sensorValue, 0, 1023, 0, 255);
      // change the analog out value:
      analogWrite(analogOutPin, sensorValue);

      // print the results to the serial monitor:
      Serial.print(“sensor = “);
      Serial.print(outputValue);
      Serial.print( “output=” );
      Serial.print(sensorValue);

      // wait 2 milliseconds before the next loop
      // for the analog-to-digital converter to settle
      // after the last reading:
      delay(2);
      }

      /* extra credit

      1. what would happen if you changed the map() statement in the program to each of the following:

      map(sensorValue, 1023, 0, 0, 255)
      (LED’s will stay ON)
      map(sensorValue, 0, 1023, 255, 0)
      ((LED’s will stay ON)
      map(sensorValue, 1023, 0, 255, 0)
      (LED’s will stay OFF)

      /*
      Blink
      Turns on an LED on for one second, then off for one second, repeatedly.

      This example code is in the public domain.
      */

      // Pin 13 has an LED connected on most Arduino boards.
      // give it a name:
      int led = 13
      // the setup routine runs once when you press reset:
      void setup ()

      // the loop routine runs over and over again forever:

      Void Lopp () (
      Digitalwrite (led,high)
      dalay(1000) ;
      Digitalwrite (led,low)
      delay (1000) ;

      )

  5. 37337 says:

    /*
    Analog input, analog output, serial output

    Reads an analog input pin, maps the result to a range from 0 to 255
    and uses the result to set the pulsewidth modulation (PWM) of an output pin.
    Also prints the results to the serial monitor.

    The circuit:
    * potentiometer connected to analog pin 0.
    Center pin of the potentiometer goes to the analog pin.
    side pins of the potentiometer go to +5V and ground
    * LED connected from digital pin 9 to ground

    created 29 Dec. 2008
    modified 9 Apr 2012
    by Tom Igoe
    Broken in terrible, silly ways 12 15 2013
    by Damon Loren Baker

    This example code is in the public domain
    and is broken and should only be used for the
    physical computing final exam

    */

    // These constants won’t change. They’re used to give names
    // to the pins used:
    const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
    const int analogOutPin = 9; // Analog output pin that the LED is attached to

    int sensorValue = 0; // value read from the pot
    int outputValue = 0; // value output to the PWM (analog out)

    void setup() {
    // initialize serial communications at 9600 bps:
    Serial.begin(9600);
    }

    void loop() {
    // read the analog in value:
    sensorValue = analogRead(analogInPin);
    // map it to the range of the analog out:
    outputValue == map(sensorValue, 0, 1023, 0, 255);
    // change the analog out value:
    analogWrite(analogOutPin, outputValue);

    // print the results to the serial monitor:
    Serial.print(“sensor = ” )
    Serial.print(sensorValue);
    Serial.print(“/t output = “);
    Serial.println(outputValue);

    // wait 2 milliseconds before the next loop
    // for the analog-to-digital converter to settle
    // after the last reading:
    delayMicroseconds(2);
    }

    /* extra credit

    1. what would happen if you changed the map() statement in the program to each of the following:

    map(sensorValue, 1023, 0, 0, 255)
    my brain hurts

    map(sensorValue, 0, 1023, 255, 0)
    on either extreme left or right you’d get nothing, middle would be the highest you can go
    map(sensorValue, 1023, 0, 255, 0)
    the arduino has a sensor resolution of 0-1023 and the other thingy only goes up to 255, so you make a nice ratio
    so they get along. in this case, it would switch the potentiometer direction *(i.e. all the way left would be max)
    2. Write code that matches the following comments (the comments are good, the code itself has been deleted)
    /*
    Blink
    Turns on an LED on for one second, then off for one second, repeatedly.

    This example code is in the public domain.
    */

    // Pin 13 has an LED connected on most Arduino boards.
    // give it a name:
    // the setup routine runs once when you press reset:

    // the loop routine runs over and over again forever:

    *
    */

  6. stephanie s says:

    /*
    Analog input, analog output, serial output

    Reads an analog input pin, maps the result to a range from 0 to 255
    and uses the result to set the pulsewidth modulation (PWM) of an output pin.
    Also prints the results to the serial monitor.

    The circuit:
    * potentiometer connected to analog pin 0.
    Center pin of the potentiometer goes to the analog pin.
    side pins of the potentiometer go to +5V and ground
    * LED connected from digital pin 9 to ground

    created 29 Dec. 2008
    modified 9 Apr 2012
    by Tom Igoe
    Broken in terrible, silly ways 12 15 2013
    by Damon Loren Baker

    This example code is in the public domain
    and is broken and should only be used for the
    physical computing final exam

    */

    // These constants won’t change. They’re used to give names
    // to the pins used:
    const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
    const int analogOutPin = 9; // Analog output pin that the LED is attached to

    sensorValue = 0; // value read from the pot
    outputValue = 0; // value output to the PWM (analog out)

    Void() {
    // initialize serial communications at 9600 bps:
    Serial.begin(9600);
    }

    void loop() {
    // read the analog in value:
    sensorValue = analogRead(analogInPin);
    // map it to the range of the analog out:
    outputValue = map(sensorValue, 0, 1023, 0, 255);
    // change the analog out value:
    analogWrite(analogOutPin, sensorValue);

    // print the results to the serial monitor:
    Serial.print(“sensor = ” );
    Serial.print(outputValue);
    Serial.print(“/t output = “);
    Serial.print(sensorValue);

    // wait 2 milliseconds before the next loop
    // for the analog-to-digital converter to settle
    // after the last reading:
    delay(2);
    }

    /* extra credit

    1. what would happen if you changed the map() statement in the program to each of the following:

    map(sensorValue, 1023, 0, 0, 255) The function would execute/initiate at full speed then slow to a stop, then reverse

    map(sensorValue, 0, 1023, 255, 0) This wouldn’t work because the motor would be set to go to maximum (1023) and then straight to 255 other direction
    map(sensorValue, 1023, 0, 255, 0) This might run but it wouldn’t be pretty. that is because you are telling it to start at full speed, slow down to 0,
    then immediatly go to full the other direction, then slow to a stop.
    2. Write code that matches the following comments (the comments are good, the code itself has been deleted)
    /*
    Blink
    Turns on an LED on for one second, then off for one second, repeatedly.

    This example code is in the public domain.
    */

    // Pin 13 has an LED connected on most Arduino boards.
    // give it a name:

    // the setup routine runs once when you press reset:

    // the loop routine runs over and over again forever:

    /*
    */

  7. stevejc says:

    This example code is in the public domain
    and is broken and should only be used for the
    physical computing final exam

    */

    // These constants won’t change. They’re used to give names
    // to the pins used:
    const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
    const int analogOutPin = 9; // Analog output pin that the LED is attached to

    int sensorValue = 0; // value read from the pot
    int outputValue = 0; // value output to the PWM (analog out)

    void setup() {
    // initialize serial communications at 9600 bps:
    Serial.begin(9600);
    }

    void loop() {
    // read the analog in value:
    sensorValue = analogRead(analogInPin);
    // map it to the range of the analog out:
    outputValue = map(sensorValue, 0, 1023, 0, 255);
    // change the analog out value:
    analogWrite(analogOutPin, outputValue);

    // print the results to the serial monitor:
    Serial.print[“sensor = ” ];
    Serial.print(sensorValue);
    Serial.print{“\t output = “};
    Serial.println(outputValue);

    // wait 2 milliseconds before the next loop
    // for the analog-to-digital converter to settle
    // after the last reading:
    delay(2);
    }

    /* extra credit

    1. what would happen if you changed the map() statement in the program to each of the following:

    map(sensorValue, 1023, 0, 0, 255) – it would continue to go in one direction

    map(sensorValue, 0, 1023, 255, 0)
    map(sensorValue, 1023, 0, 255, 0) – it would go backwards
    2. Write code that matches the following comments (the comments are good, the code itself has been deleted)
    /*
    Blink
    Turns on an LED on for one second, then off for one second, repeatedly.

    This example code is in the public domain.
    */

    // Pin 13 has an LED connected on most Arduino boards.
    // give it a name:
    AnalogPin = Pin 13

    // the setup routine runs once when you press reset:

    // the loop routine runs over and over again forever:

    void loop () {

  8. Rich says:

    /*
    Analog input, analog output, serial output

    Reads an analog input pin, maps the result to a range from 0 to 255
    and uses the result to set the pulsewidth modulation (PWM) of an output pin.
    Also prints the results to the serial monitor.

    The circuit:
    * potentiometer connected to analog pin 0.
    Center pin of the potentiometer goes to the analog pin.
    side pins of the potentiometer go to +5V and ground
    * LED connected from digital pin 9 to ground

    created 29 Dec. 2008
    modified 9 Apr 2012
    by Tom Igoe
    Broken in terrible, silly ways 12 15 2013
    by Damon Loren Baker

    This example code is in the public domain
    and is broken and should only be used for the
    physical computing final exam

    */

    // These constants won’t change. They’re used to give names
    // to the pins used:
    const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
    const int analogOutPin = 9; // Analog output pin that the LED is attached to

    int sensorValue = 0; // value read from the pot
    int outputValue = 0; // value output to the PWM (analog out)

    void setup() {
    // initialize serial communications at 9600 bps:
    Serial.begin(9600);
    }

    void loop() {
    // read the analog in value:
    sensorValue = analogRead(analogInPin);
    // map it to the range of the analog out:
    outputValue = map(sensorValue, 0, 1023, 0, 255);
    // change the analog out value:
    analogWrite(analogOutPin, outputValue);

    // print the results to the serial monitor:
    Serial.print(“sensor = ” );
    Serial.print(sensorValue);
    Serial.print(“\t output =” );
    Serial.println(outputValue);

    // wait 2 milliseconds before the next loop
    // for the analog-to-digital converter to settle
    // after the last reading:
    delay(2);
    }

    Extra Credit Questions:

    1. This changes beginning and end value that the POT outputs to pin A0
    2. */

    // Pin 13 has an LED connected on most Arduino boards.

    LED(outPutPin) = 13;
    // give it a name:
    int LED = o

    // the setup routine runs once when you press reset:
    void setup () {
    }

    void loop () {
    LED = High
    delay(3);
    LED = Low
    delay(3);

  9. TEKKNEO says:

    /*
    Analog input, analog output, serial output

    Reads an analog input pin, maps the result to a range from 0 to 255
    and uses the result to set the pulsewidth modulation (PWM) of an output pin.
    Also prints the results to the serial monitor.

    The circuit:
    * potentiometer connected to analog pin 0.
    Center pin of the potentiometer goes to the analog pin.
    side pins of the potentiometer go to +5V and ground
    * LED connected from digital pin 9 to ground

    created 29 Dec. 2008
    modified 9 Apr 2012
    by Tom Igoe
    Broken in terrible, silly ways 12 15 2013
    by Damon Loren Baker

    This example code is in the public domain
    and is broken and should only be used for the
    physical computing final exam

    */

    // These constants won’t change. They’re used to give names
    // to the pins used:
    const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
    const int analogOutPin = 9; // Analog output pin that the LED is attached to

    int sensorValue = 0; // value read from the pot
    int outputValue = 0; // value output to the PWM (analog out)

    void setup() {
    // initialize serial communications at 9600 bps:
    Serial.begin(9600);
    }

    void loop() {
    // read the analog in value:
    sensorValue = analogRead(analogInPin);
    // map it to the range of the analog out:
    outputValue = map(sensorValue, 0, 1023, 0, 255);
    // change the analog out value:
    analogWrite(analogOutPin, outputValue);

    // print the results to the serial monitor:
    Serial.print(“sensor = ” );
    Serial.print(“sensorValue = ” );
    Serial.print(“\t output = “);
    Serial.print(“outputValue = ” );

    // wait 2 milliseconds before the next loop
    // for the analog-to-digital converter to settle
    // after the last reading:
    delay(2);
    }

    /* extra credit

    1. what would happen if you changed the map() statement in the program to each of the following:

    map(sensorValue, 1023, 0, 0, 255)
    The pot would have reversed controls AND an apposite starting point from the original code.

    map(sensorValue, 0, 1023, 255, 0)
    The pot would have reversed controls.

    map(sensorValue, 1023, 0, 255, 0)
    Essentially the same function, but the starting point would be switched.

    2. Write code that matches the following comments (the comments are good, the code itself has been deleted)
    /*
    Blink
    Turns on an LED on for one second, then off for one second, repeatedly.

    This example code is in the public domain.
    */

    // Pin 13 has an LED connected on most Arduino boards.
    // give it a name:
    int LED = 13;

    // the setup routine runs once when you press reset:
    void setup( ) {

    //set the digital pin as an output
    pinMode(led, OUTPUT);
    }

    // the loop routine runs over and over again forever:
    void loop ( ) {
    digitalWrite (LED, HIGH);
    delay (2000);
    digitalWrite (LED, LOW);
    delay (3500);
    }

    /*
    */

  10. /*
    Analog input, analog output, serial output

    Reads an analog input pin, maps the result to a range from 0 to 255
    and uses the result to set the pulsewidth modulation (PWM) of an output pin.
    Also prints the results to the serial monitor.

    The circuit:
    * potentiometer connected to analog pin 0.
    Center pin of the potentiometer goes to the analog pin.
    side pins of the potentiometer go to +5V and ground
    * LED connected from digital pin 9 to ground

    created 29 Dec. 2008
    modified 9 Apr 2012
    by Tom Igoe
    Broken in terrible, silly ways 12 15 2013
    by Damon Loren Baker

    This example code is in the public domain
    and is broken and should only be used for the
    physical computing final exam

    */

    // These constants won’t change. They’re used to give names
    // to the pins used:
    const analoginpin = A0; // Analog input pin that the potentiometer is attached to
    const analogoutpin = 9; // Analog output pin that the LED is attached to

    int sensorValue = 0; // value read from the pot
    int outputvalue = 0; // value output to the PWM (analog out)

    void setup() {
    // initialize serial communications at 9600 bps:
    Serial.begin(9600);
    }

    void loop() {
    // read the analog in value:
    sensorValue = analogRead(analoginPin);
    // map it to the range of the analog out:
    outputValue = map(sensorValue, 0, 1023, 0, 255);
    // change the analog out value:
    analogWrite(analogOutPin, outputvalue);

    // print the results to the serial monitor:
    Serial.print(“sensor = ” );
    Serial.print(sensorvalue);
    Serial.print(“\t output = “);
    Serial.println(outputvalue);

    // wait 2 milliseconds before the next loop
    // for the analog-to-digital converter to settle
    // after the last reading:
    delay(2);
    }

    /* extra credit

    1. what would happen if you changed the map() statement in the program to each of the following:

    map(sensorValue, 1023, 0, 0, 255)

    map(sensorValue, 0, 1023, 255, 0)
    map(sensorValue, 1023, 0, 255, 0)
    2. Write code that matches the following comments (the comments are good, the code itself has been deleted)
    /*
    Blink
    Turns on an LED on for one second, then off for one second, repeatedly.
    delay(1000);

    This example code is in the public domain.
    */

    // Pin 13 has an LED connected on most Arduino boards.
    // give it a name: sensorvalue analogoutpin = 13;

    // the setup routine runs once when you press reset:void setup(){
    }

    // the loop routine runs over and over again forever: void loop(){
    }

    /*
    */

  11. Jorge Lara says:

    const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
    const int analogOutPin = 9; // Analog output pin that the LED is attached to

    int sensorValue = 0; // value read from the pot
    int outputValue = 0; // value output to the PWM (analog out)
    void setup(){
    // initialize serial communications at 9600 bps:
    Serial.begin(9600);
    }

    void loop() {
    // read the analog in value:
    sensorValue = analogRead(analogInPin);
    // map it to the range of the analog out:
    outputValue == map(sensorValue, 0, 1023, 0, 255);
    // change the analog out value:
    analogWrite(analogOutPin, sensorValue);

    // print the results to the serial monitor:
    Serial.print(“sensor = ” );
    Serial.print(outputValue);
    Serial.print(“\t output = “);
    Serial.println(sensorValue);

    // wait 2 milliseconds before the next loop
    // for the analog-to-digital converter to settle
    // after the last reading:
    delay(2);
    }
    extra credit
    map(sensorValue, 1023, 0, 0, 255) the light will stay on through the values of the pot
    map(sensorValue, 0, 1023, 255, 0) the light will stay on through the values of the pot
    map(sensorValue, 1023, 0, 255, 0) light will go from on to off through the values of the pot

  12. /*
    Analog input, analog output, serial output

    Reads an analog input pin, maps the result to a range from 0 to 255
    and uses the result to set the pulsewidth modulation (PWM) of an output pin.
    Also prints the results to the serial monitor.

    The circuit:
    * potentiometer connected to analog pin 0.
    Center pin of the potentiometer goes to the analog pin.
    side pins of the potentiometer go to +5V and ground
    * LED connected from digital pin 9 to ground

    created 29 Dec. 2008
    modified 9 Apr 2012
    by Tom Igoe
    Broken in terrible, silly ways 12 15 2013
    by Damon Loren Baker

    This example code is in the public domain
    and is broken and should only be used for the
    physical computing final exam

    */

    // These constants won’t change. They’re used to give names
    // to the pins used:
    const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
    const int analogOutPin = 9; // Analog output pin that the LED is attached to

    int sensorValue = 0; // value read from the pot
    int outputValue = 0; // value output to the PWM (analog out)

    void startup() {
    // initialize serial communications at 9600 bps:
    Serial.begin(9600);
    }

    void loop() {
    // read the analog in value:
    sensorValue = analogRead(analogInPin);
    // map it to the range of the analog out:
    outputValue = map(sensorValue, 0, 1023, 0, 255);
    // change the analog out value:
    digitalWrite(analogOutPin, sensorValue);

    // print the results to the serial monitor:
    Serial.print(“sensor = ” );
    Serial.print(outputValue);
    Serial.print(“\t output = “);
    Serial.print(sensorValue);

    // wait 2 milliseconds before the next loop
    // for the analog-to-digital converter to settle
    // after the last reading:
    delay(2);
    }

    /* extra credit

    1. what would happen if you changed the map() statement in the program to each of the following:

    map(sensorValue, 1023, 0, 0, 255)
    Inverts the incoming value – as the incoming value goes lower, the outgoing value goes higher

    map(sensorValue, 0, 1023, 255, 0)
    inverts the outgoing value – as the incoming value goes higher, the outgoing value goes lower

    map(sensorValue, 1023, 0, 255, 0)
    reverses the direction of the changing value – basically it reverses the direction you turn the potentiometer

    2. Write code that matches the following comments (the comments are good, the code itself has been deleted)

    Blink
    Turns on an LED on for one second, then off for one second, repeatedly.
    */

    int led = 13;

    do {
    digitalWrite(led, HIGH);
    delay(1000);
    digitalWrite(led, LOW);
    delay(1000);
    } while (1);

    /*
    This example code is in the public domain.
    */

    // Pin 13 has an LED connected on most Arduino boards.
    // give it a name:

    int led = 13;

    // the setup routine runs once when you press reset:

    void setup() {
    }

    // the loop routine runs over and over again forever

    void loop() {
    }

  13. Yaro says:

    /*
    Analog input, analog output, serial output

    Reads an analog input pin, maps the result to a range from 0 to 255
    and uses the result to set the pulsewidth modulation (PWM) of an output pin.
    Also prints the results to the serial monitor.

    The circuit:
    * potentiometer connected to analog pin 0.
    Center pin of the potentiometer goes to the analog pin.
    side pins of the potentiometer go to +5V and ground
    * LED connected from digital pin 9 to ground

    created 29 Dec. 2008
    modified 9 Apr 2012
    by Tom Igoe
    Broken in terrible, silly ways 12 15 2013
    by Damon Loren Baker

    This example code is in the public domain
    and is broken and should only be used for the
    physical computing final exam

    */

    // These constants won’t change. They’re used to give names
    // to the pins used:
    const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
    const int analogOutPin = 9; // Analog output pin that the LED is attached to

    int sensorValue = 0; // value read from the pot
    int outputValue = 0; // value output to the PWM (analog out)

    void setup() {
    // initialize serial communications at 9600 bps:
    Serial.begin(9600);
    }

    void loop() {
    // read the analog in value:
    sensorValue = analogRead(analogInPin);
    // map it to the range of the analog out:
    outputValue = map(sensorValue, 1023, 0, 0, 255);
    // change the analog out value:
    analogWrite(analogOutPin, outputValue);

    // print the results to the serial monitor:
    Serial.print(“sensor = ” );
    Serial.print(sensorValue);
    Serial.print(“\t output = “);
    Serial.println(outputValue);

    // wait 2 milliseconds before the next loop
    // for the analog-to-digital converter to settle
    // after the last reading:
    delay(2);
    }

    /* extra credit

    1. what would happen if you changed the map() statement in the program to each of the following:

    map(sensorValue, 1023, 0, 0, 255)
    the highest value will be displayed as 0, while the lowest value will be displayed as the highest one
    map(sensorValue, 0, 1023, 255, 0)
    polarity will be reversed
    map(sensorValue, 1023, 0, 255, 0)
    readings will be displayed from highest to lowest
    2. Write code that matches the following comments (the comments are good, the code itself has been deleted)
    /*
    Blink
    Turns on an LED on for one second, then off for one second, repeatedly.

    This example code is in the public domain.
    */

    int ledPin = 13;// Pin 13 has an LED connected on most Arduino boards.
    // give it a name:

    // the setup routine runs once when you press reset:
    void setup() {
    }
    // the loop routine runs over and over again forever:
    void loop() {
    digitalWrite(ledPin, HIGH);
    delay(1000);
    digitalWrite(ledPin, LOW);
    delay(1000);
    }

  14. vincent says:

    /*
    Analog input, analog output, serial output

    Reads an analog input pin, maps the result to a range from 0 to 255
    and uses the result to set the pulsewidth modulation (PWM) of an output pin.
    Also prints the results to the serial monitor.

    The circuit:
    * potentiometer connected to analog pin 0.
    Center pin of the potentiometer goes to the analog pin.
    side pins of the potentiometer go to +5V and ground
    * LED connected from digital pin 9 to ground

    created 29 Dec. 2008
    modified 9 Apr 2012
    by Tom Igoe
    Broken in terrible, silly ways 12 15 2013
    by Damon Loren Baker

    This example code is in the public domain
    and is broken and should only be used for the
    physical computing final exam

    */

    // These constants won’t change. They’re used to give names
    // to the pins used:
    const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
    const int analogOutPin = 9; // Analog output pin that the LED is attached to

    int sensorValue = 0; // value read from the pot
    int outputValue = 0; // value output to the PWM (analog out)

    void setup() {
    // initialize serial communications at 9600 bps:
    Serial.begin(9600);
    }

    void loop() {
    // read the analog in value:
    sensorValue = analogRead(analogInPin);
    // map it to the range of the analog out:
    outputValue = map(sensorValue, 0, 1023, 0, 255);
    // change the analog out value:
    analogWrite(analogOutPin, sensorValue);

    // print the results to the serial monitor:
    Serial.print(“sensor = “);
    Serial.print(outputValue);
    Serial.print(“\t output = “);
    Serial.print(sensorValue);

    // wait 2 milliseconds before the next loop
    // for the analog-to-digital converter to settle
    // after the last reading:
    delay(2);
    }

  15. Frank Mason says:

    /*
    Analog input, analog output, serial output

    Reads an analog input pin, maps the result to a range from 0 to 255
    and uses the result to set the pulsewidth modulation (PWM) of an output pin.
    Also prints the results to the serial monitor.

    The circuit:
    * potentiometer connected to analog pin 0.
    Center pin of the potentiometer goes to the analog pin.
    side pins of the potentiometer go to +5V and ground
    * LED connected from digital pin 9 to ground

    created 29 Dec. 2008
    modified 9 Apr 2012
    by Tom Igoe
    Broken in terrible, silly ways 12 15 2013
    by Damon Loren Baker

    This example code is in the public domain
    and is broken and should only be used for the
    physical computing final exam

    */

    // These constants won’t change. They’re used to give names
    // to the pins used:
    const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
    static int analogOutPin = 9; // Analog output pin that the LED is attached to

    const int sensorValue = 0; // value read from the pot
    int outputValue = 0; // value output to the PWM (analog out)

    void setup() {
    // initialize serial communications at 9600 bps:
    Serial.begin(9600);
    }

    void loop() {
    // read the analog in value:
    sensorValue = analogRead(analogInPin);
    // map it to the range of the analog out:
    outputValue = map(sensorValue, 0, 1023, 0, 255);
    // change the analog out value:
    analogWrite(analogOutPin, outputValue);

    // print the results to the serial monitor:
    Serial.print(“sensor = ” );
    Serial.print(sensorValue);
    Serial.print(“\t output = “);
    Serial.println(outputValue);

    // wait 2 milliseconds before the next loop
    // for the analog-to-digital converter to settle
    // after the last reading:
    delay(2); }

    /* extra credit

    1. what would happen if you changed the map() statement in the program to each of the following:

    map(sensorValue, 1023, 0, 0, 255)
    Answer: The device would essentially be in a constant power on position for an indefinite period of time. It would possess no ability to turn off based on the fact that both ends of the spectrum use an on value, which in this case one end would be 1023 (turned counter clockwise) and 255 (turned clockwise).

    map(sensorValue, 0, 1023, 255, 0)
    Answer: It would turn from an off to on position (using the values 0, 1023) to an on to off position (255, 0).

    map(sensorValue, 1023, 0, 255, 0)
    Answer: It would turn from an on to off position (1023, 0). It would do the same action again once the potentiometer reaches the center position in which it would turn from an on to off position (255, 0).

  16. Yaro says:

    “outputValue = map(sensorValue, 1023, 0, 0, 255);” – I meant outputValue = map(sensorValue, 1023, 0, 255, 0);

Leave a Reply