Declare I/O Const Int Proxinput 2 Const Int Led 13 : (Select)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

You schematic doesn't show the negative 24v power supply connection.

It's best to draw a


complete circuit and don't assume that connecting a wire to a "ground" point completes a circuit.
The 24 volt common needs to be connected to both the prox common and the Arduino GND.
Obviously they're connected or things wouldn't be working.

A lot of industrial control systems using 24 vdc control voltage ground the common of the
supply as you've shown, actually connecting this point to metal back panels and ultimately to
earth ground through the power system. It's a bad idea for a number of reasons but it seems to be
gaining acceptance as 24 vdc control voltage systems replace older US standard 120 vac control
systems. That's a topic for another day...

Your code, while working, could be refined a bit as a learning exercise.

You first declare your prox variable as "int sensorState = 0;" which is fine but the initial setting
to zero is redundant, all Arduino read/write variables are initialized to zero upon a power
up/reset.

Next, you setup the prox input with the pinMode statement as pin 2 and then read pin 3. It works
because all pins are initialized in the Arduino as inputs so there's no actual need to set the pin
mode to input, other than for clairity.

You then use an "if" statement to compare the prox status to LOW and then write a LOW to LED
when true. That's absolutely fine but it's redundant, if the desired output value is the same as the
input, there is no need to use the if test.

Here's a modified version that adds variables that represent the pin numbers, this avoids
numbering mistakes and makes the program more readable. The "const" keyword added before
the int size declaration tells the compiler the name is a constant and cannot be changed. If you try
to assign a new value to the name, you'll get a compile time error.

Code: [Select]

// declare I/O
const int proxInput = 2;
const int LED = 13;

//declare variables
int sensorState;
void setup() {
pinMode(proxInput, INPUT);
pinMode(LED, OUTPUT);
}

void loop() {
sensorState = digitalRead(proxInput);
digitalWrite(LED, sensorState);
}
wvmarle


 Shannon Member
 Posts: 13,353
 Karma: 666 [add]

Re: Interfacing a inductive proximity sensor to an Arduino Uno


#19
Jan 05, 2018, 05:49 am
Quote from: avr_fred on Jan 05, 2018, 04:34 am
You first declare your prox variable as "int sensorState = 0;" which is fine but the initial setting to zero is
redundant, all Arduino read/write variables are initialized to zero upon a power up/reset.

That must be specific Arduino then (handled by the IDE?) as it's not the C++ standard which
would initialise the value to whatever happens to be on that memory address.

This specific line can do with some optimisation. As it's a digital pin state the value can only be
0 or 1, and this would be better declared as bool or byte. That saves 1 byte of RAM and should
make the code run a little faster (yes, purely academic and not of practical importance in this
code).

In this example code the whole variable is actually unnecessary as it's not used for anything but
setting that output pin. The loop() can be written as:

Code: [Select]
void loop() {
digitalWrite(LED, digitalRead(proxInput));
}

For the declaration of pin numbers I prefer to use #define statements. I still don't know if there's
any real difference between the two methods.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless
answers are a sign of a poor question.
avr_fred
 Guest

Re: Interfacing a inductive proximity sensor to an Arduino Uno


#20
Jan 05, 2018, 04:02 pm
Quote
That must be specific Arduino then (handled by the IDE?) as it's not the C++ standard which would initialise the
value to whatever happens to be on that memory address.

Yup, it's specific to the Arduino run time environment. Both the compile and run time behaviors
are significantly non-standard C++ and not at all documented, AFAIK.

Quote
For the declaration of pin numbers I prefer to use #define statements. I still don't know if there's any real difference
between the two methods.

Yes, there is a difference, at least to the compiler. But in the Arduino environment, it really
doesn't matter. It's a matter of programming style.

Defines are simple (and sometimes complex) text substitutions using the pre-processor. When
defining a pin with a number, the compiler just sees the number, the original text was stripped
out by the pre-processor. While that works for our purposes, the compiler knows nothing about
what the number represents or if it meets the range specification of a possible target location.

C++ upsized the capability of variable type checking that originated in ANSI C. This was
required for expanding the language to support OOP, allowing the compiler to check that you
don't store a double into an int and other similar programming errors. That behavior brings us
full circle with your comment:

Quote
As it's a digital pin state the value can only be 0 or 1, and this would be better declared as bool or byte.

Love or or hate it, the Arduino environment completely breaks the rules set forth in C++
designed to prevent stupid programmer mistakes, like storing a double into an int. Arduino will
let you do that, the compiler will remain absolutely mute. They've removed one of the key
features of C++, type checking, in the interest of ease of learning. As an example:

Code: [Select]
void setup() {
}

void loop() {
double testDouble;
int testInt;

digitalWrite(0, testDouble);
digitalWrite(0, testInt);
testDouble = digitalRead(0);
testInt = digitalRead(0);
testInt = testDouble;
}
This compiles without a peep, any pure C++ compiler would be barfing error messages all over
that code... but not in Arduino land where you can clobber ajacent variables with wild abandon. I
guess that helps develop debugging skills? Oh, right, there's no debugger...

This total lack of type checking is exactly why your comment about using a bool or a byte gets
lost in the discussion as well as the discussion of #defines verses the C++ way of doing things. In
the world that is Arduino, it simply doesn't matter. The functions digitalWrite/read are actually
declared as using bytes but since the compiler does not care what is written to or read from, it's
up to the user to determine the suitability of the variable involved which is why you see int used
as a default type in so many programs. It's easy and it always allows for other mistakes to be
made without crashing your program due to variable overlap, so long as you don't go past 16
bits...
wvmarle


 Shannon Member
 Posts: 13,353
 Karma: 666 [add]

Re: Interfacing a inductive proximity sensor to an Arduino Uno


#21
Jan 05, 2018, 04:27 pm
Quote from: avr_fred on Jan 05, 2018, 04:02 pm
Code: [Select]

digitalWrite(0, testDouble);
digitalWrite(0, testInt);
testDouble = digitalRead(0);
testInt = digitalRead(0);

This compiles without a peep, any pure C++ compiler would be barfing error messages all over that code...

Compiler or libraries?
I see the IDE uses a compiler called "avr-gcc".
I've had more than often had the compiler throw errors on me for not using the correct variable
type when calling a function. So someone, somewhere created a function like:

Code: [Select]

void digitalWrite(int, float) {


}

And that for all possible types...


Quality of answers is related to the quality of questions. Good questions will get good answers. Useless
answers are a sign of a poor question.
PhilGuitar


 Newbie
 Posts: 6
 Karma: 0 [add]

Re: Interfacing a inductive proximity sensor to an Arduino Uno


#22
Jan 06, 2018, 09:57 pm
@avr_fred - Ah my mistake. Realised this after I had connected it all up and nothing happened.
Did what you said and it worked just fine. I felt my coding could've been a bit more refined but
I'm still a novice and kind of "frankensteined" parts of code from the projects in the Arduino
projects book. Definitely something I need to work on. Thanks for the info and the modified
code.

Elon Musk

1. About Tesla
2. About SpaceX
3. About OpenAI
4. About Neuralink
5. About The Boring Company

Elon Musk is the co-founder and CEO at Tesla, overseeing all product design, engineering
and manufacturing of the company's electric vehicles, battery products, and Solar Roofs.

Since the company’s inception in 2003, Tesla’s mission has been to accelerate the world’s
transition to a sustainable energy future. The first Tesla product, the Roadster sports car,
debuted in 2008, followed by the Model S sedan, which was introduced in 2012, and
the Model X SUV, which launched in 2015. Model S was named Motor Trend 2013 Car of
the Year and received Consumer Reports’ Best Overall Car, while Model X was the first
SUV ever to earn 5-star safety ratings in every category and sub-category in the National
Highway Traffic Safety Administration’s tests. In 2017, Tesla began deliveries of Model 3, a
$35,000 mass-market electric car with over 215 miles of range, and unveiled Tesla Semi,
which is designed to save owners at least $200,000 over a million miles based on fuel costs
alone.
Tesla also produces two energy storage products, the Powerwall home battery and
the Powerpack industrial-scale battery. In 2016, Tesla became the world’s first vertically-
integrated sustainable energy company with the acquisition of SolarCity, the leading
provider of solar power systems in the United States, and in 2017 released Solar Roof – a
beautiful and affordable energy generation product.
Elon is also the co-founder, CEO and lead designer of Space Exploration Technologies
(SpaceX), where he oversees the development and manufacturing of advanced rockets and
spacecraft for missions to and beyond Earth orbit, with the goal of creating a self-sustaining
city on Mars. He is also CEO of Neuralink, which is developing ultra-high bandwidth brain-
machine interfaces to connect the human brain to computers. Elon also founded and serves
as CEO of The Boring Company, which combines fast, affordable tunneling technology with
an all-electric public transportation system in order to alleviate soul-crushing urban
congestion and enable high-speed long-distance travel.

Previously, Elon co-founded and sold PayPal, the world's leading Internet payment system,
and Zip2, one of the first internet maps and directions services, which helped bring major
publishers like the New York Times and Hearst online.

South African entrepreneur Elon Musk is known for founding Tesla


Motors and SpaceX, which launched a landmark commercial
spacecraft in 2012.
Who Is Elon Musk?

Elon Reeve Musk is a South African-born American entrepreneur and businessman who founded
X.com in 1999 (which later became PayPal), SpaceX in 2002 and Tesla Motors in 2003. Musk
became a multimillionaire in his late 20s when he sold his start-up company, Zip2, to a division
of Compaq Computers.

Musk made headlines in May 2012, when SpaceX launched a rocket that would send the first
commercial vehicle to the International Space Station. He bolstered his portfolio with the
purchase of SolarCity in 2016, and cemented his standing as a leader of industry by taking on an
advisory role in the early days of President Donald Trump's administration.

Elon Musk’s Mom and Dad

Musk’s mother, Maye Musk, is a Canadian model and the oldest woman to star in a Covergirl
campaign. When Musk was growing up, she worked five jobs at one point to support her family.

Musk’s father, Errol Musk, is a wealthy South African engineer.

Musk spent his early childhood with his brother Kimbal and sister Tosca in South Africa. His
parents divorced when he was 10.
Early Life

Musk was born on June 28, 1971, in Pretoria, South Africa. As a child, Musk was so lost in his
daydreams about inventions that his parents and doctors ordered a test to check his hearing.

At about the time of his parents’ divorce, when he was 10, Musk developed an interest in
computers. He taught himself how to program, and when he was 12 he sold his first software: a
game he created called Blastar.

In grade school Musk was short, introverted and bookish. He was bullied until he was 15 and
went through a growth spurt and learned how to defend himself with karate and wrestling.

Elon Musk’s Education

At age 17, in 1989, Musk moved to Canada to attend Queen’s University and avoid mandatory
service in the South African military. Musk obtained his Canadian citizenship that year, in part
because he felt it would be easier to obtain American citizenship via that path.

In 1992, Musk left Canada to study business and physics at the University of Pennsylvania. He
graduated with an undergraduate degree in economics and stayed for a second bachelor’s degree
in physics.

After leaving Penn, Musk headed to Stanford University in California to pursue a PhD in energy
physics. However, his move was timed perfectly with the Internet boom, and he dropped out of
Stanford after just two days to become a part of it, launching his first company, Zip2 Corporation
in 1995. Musk became a U.S. citizen in 2002.

Elon Musk’s Companies

Zip2 Corporation
Musk launched his first company, Zip2 Corporation, in 1995 with his brother, Kimbal Musk. An
online city guide, Zip2 was soon providing content for the new websites of both The New York
Times and the Chicago Tribune. In 1999, a division of Compaq Computer Corporation bought
Zip2 for $307 million in cash and $34 million in stock options.

PayPal
In 1999, Elon and Kimbal Musk used the money from their sale of Zip2 to found X.com, an
online financial services/payments company. An X.com acquisition the following year led to the
creation of PayPal as it is known today.

In October 2002, Musk earned his first billion when PayPal was acquired by eBay for $1.5
billion in stock. Before the sale, Musk owned 11 percent of PayPal stock.

SpaceX

Musk founded his third company, Space Exploration Technologies Corporation, or SpaceX, in
2002 with the intention of building spacecraft for commercial space travel. By 2008, SpaceX was
well established, and NASA awarded the company the contract to handle cargo transport for the
International Space Station—with plans for astronaut transport in the future—in a move to
replace NASA’s own space shuttle missions.
Elon Musk poses for a portrait in Los Angeles, California, on July 25, 2008.
Photo: Dan Tuffs/Getty Images

Falcon 9 Rockets

On May 22, 2012, Musk and SpaceX made history when the company launched its Falcon 9
rocket into space with an unmanned capsule. The vehicle was sent to the International Space
Station with 1,000 pounds of supplies for the astronauts stationed there, marking the first time a
private company had sent a spacecraft to the International Space Station. Of the launch, Musk
was quoted as saying, "I feel very lucky. ... For us, it's like winning the Super Bowl."

In December 2013, a Falcon 9 successfully carried a satellite to geosynchronous transfer orbit, a


distance at which the satellite would lock into an orbital path that matched the Earth's rotation. In
February 2015, SpaceX launched another Falcon 9 fitted with the Deep Space Climate
Observatory (DSCOVR) satellite, aiming to observe the extreme emissions from the sun that
affect power grids and communications systems on Earth.

In March 2017, SpaceX saw the successful test flight and landing of a Falcon 9 rocket made
from reusable parts, a development that opened the door for more affordable space travel.

A setback came in November 2017, when an explosion occurred during a test of the company's
new Block 5 Merlin engine. SpaceX reported that no one was hurt, and that the issue would not
hamper its planned rollout of a future generation of Falcon 9 rockets.

The company enjoyed another milestone moment in February 2018 with the successful test
launch of the powerful Falcon Heavy rocket. Armed with additional Falcon 9 boosters, the
Falcon Heavy was designed to carry immense payloads into orbit and potentially serve as a
vessel for deep space missions. For the test launch, the Falcon Heavy was given a payload of
Musk's cherry-red Tesla Roadster, equipped with cameras to "provide some epic views" for the
vehicle's planned orbit around the sun.

In July 2018, Space X enjoyed the successful landing of a new Block 5 Falcon rocket, which
touched down on a drone ship less than 9 minutes after liftoff.

BFR Mission to Mars

In September 2017, Musk presented an updated design plan for his BFR (an acronym for either
"Big F---ing Rocket" or "Big Falcon Rocket"), a 31-engine behemoth topped by a spaceship
capable of carrying at least 100 people. He revealed that SpaceX was aiming to launch the first
cargo missions to Mars with the vehicle in 2022, as part of his overarching goal of colonizing the
Red Planet.
In March 2018, the entrepreneur told an audience at the annual South by Southwest festival in
Austin, Texas, that he hoped to have the BFR ready for short flights early the following year,
while delivering a knowing nod at his previous problems with meeting deadlines.

The following month, it was announced that SpaceX would construct a facility at the Port of Los
Angeles to build and house the BFR. The port property presented an ideal location for SpaceX,
as its mammoth rocket will only be movable by barge or ship when completed.

Starlink Internet Satellites

In late March 2018, SpaceX received permission from the U.S. government to launch a fleet of
satellites into low orbit for the purpose of providing Internet service. The satellite network,
named Starlink, would ideally make broadband service more accessible in rural areas, while also
boosting competition in heavily populated markets that are typically dominated by one or two
providers.

SpaceX launched the first batch of 60 satellites in May 2019, and followed with another payload
of 60 satellites that November. While this represented significant progress for the Starlink
venture, the appearance of these bright orbiters in the night sky, with the potential of thousands
more to come, worried astronomers who felt that a proliferation of satellites would increase the
difficulty of studying distant objects in space.

Tesla Motors
Musk is the co-founder, CEO and product architect at Tesla Motors, a company formed in
2003 that is dedicated to producing affordable, mass-market electric cars as well as battery
products and solar roofs. Musk oversees all product development, engineering and design of the
company's products.

Roadster

Five years after its formation, in March 2008, Tesla unveiled the Roadster, a sports car capable
of accelerating from 0 to 60 mph in 3.7 seconds, as well as traveling nearly 250 miles between
charges of its lithium ion battery.

With a stake in the company taken by Daimler and a strategic partnership with Toyota, Tesla
Motors launched its initial public offering in June 2010, raising $226 million.

Model S
In August 2008, Tesla announced plans for its Model S, the company's first electric sedan
that was reportedly meant to take on the BMW 5 series. In 2012, the Model S finally entered
production at a starting price of $58,570. Capable of covering 265 miles between charges, it was
honored as the 2013 Car of the Year by Motor Trend magazine.

In April 2017, Tesla announced that it surpassed General Motors to become the most valuable
U.S. car maker. The news was an obvious boon to Tesla, which was looking to ramp up
production and release its Model 3 sedan later that year.

In September 2019, using what Musk described as a "Plaid powertrain," a Model S set a speed
record for four-door sedan at Lacuna Seca Raceway in Monterey County, California.

Model 3

The Model 3 was officially launched in March 2019 following extensive production delays. The
car was initially priced at $35,000, a much more accessible price point than the $69,500 and up
for its Model S and X electric sedans.

After initially aiming to produce 5,000 new Model 3 cars per week by December 2017, Musk
pushed that goal back to March 2018, and then to June with the start of the new year. The
announced delay didn't surprise industry experts, who were well aware of the company's
production problems, though some questioned how long investors would remain patient with the
process. It also didn't prevent Musk from garnering a radical new compensation package as
CEO, in which he would be paid after reaching milestones of growing valuation based on $50
billion increments.

By April 2018, with Tesla expected to fall short of first-quarter production forecasts, news
surfaced that Musk had pushed aside the head of engineering to personally oversee efforts in that
division. In a Twitter exchange with a reporter, Musk said it was important to "divide and
conquer" to meet production goals and was "back to sleeping at factory."

After signaling that the company would reorganize its management structure, Musk in June
announced that Tesla was laying off 9 percent of its workforce, though its production department
would remain intact. In an email to employees, Musk explained his decision to eliminate some
"duplication of roles" to cut costs, admitting it was time to take serious steps toward turning a
profit.

The restructuring appeared to pay dividends, as it was announced that Tesla had met its goal of
producing 5,000 Model 3 cars per week by the end of June 2018, while churning out another
2,000 Model S sedans and Model X SUVs. "We did it!" Musk wrote in an celebratory email to
the company. "What an incredible job by an amazing team."
The following February, Musk announced that the company was finally rolling out its standard
Model 3. Musk also said that Tesla was shifting to all-online sales, and offering customers the
chance to return their cars within seven days or 1,000 miles for a full refund.

Semi Truck

In November 2017, Musk made another splash with the unveiling of the new Tesla Semi and
Roadster at the company's design studio. The semi truck, which entered into production in 2019,
boasts 500 miles of range as well as a battery and motors built to last 1 million miles. The truck
is set to launch in 2019.

Model Y and Roadster

In March 2019, Musk unveiled Tesla’s long-awaited Model Y. The compact crossover, expected
for release in 2020, will have a driving range of 300 miles and a 0 to 60 mph of as little as 3.5
seconds.

The Roadster, also set to be released in 2020, will become the fastest production car ever made,
with a 0 to 60 time of 1.9 seconds.

SolarCity
In August 2016, in Musk’s continuing effort to promote and advance sustainable energy and
products for a wider consumer base, a $2.6 billion dollar deal was solidified to combine his
electric car and solar energy companies. His Tesla Motors Inc. announced an all-stock deal
purchase of SolarCity Corp., a company Musk had helped his cousins start in 2006. He is a
majority shareholder in each entity.

“Solar and storage are at their best when they're combined. As one company, Tesla (storage) and
SolarCity (solar) can create fully integrated residential, commercial and grid-scale products that
improve the way that energy is generated, stored and consumed,” read a statement on Tesla’s
website about the deal.

The Boring Company

In January 2017, Musk launched The Boring Company, a company devoted to boring and
building tunnels in order to reduce street traffic. He began with a test dig on the SpaceX property
in Los Angeles.
In late October of that year, Musk posted the first photo of his company's progress to his
Instagram page. He said the 500-foot tunnel, which would generally run parallel to Interstate
405, would reach a length of two miles in approximately four months.

In May 2019 the company, now known as TBC, landed a $48.7 million contract from the Las
Vegas Convention and Visitors Authority to build an underground Loop system to shuttle people
around the Las Vegas Convention Center.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy