mirror of
https://github.com/arduino/Arduino.git
synced 2025-02-18 12:54:25 +01:00
Cleaned up examples (new Serial lib, etc)
This commit is contained in:
parent
260c5d5ee1
commit
53a6afd3f6
@ -130,18 +130,18 @@ void printBuff(){
|
|||||||
////////////////////////////////////////////
|
////////////////////////////////////////////
|
||||||
|
|
||||||
void readBuff(){
|
void readBuff(){
|
||||||
printString("into readbuff method");
|
Serial.print("into readbuff method");
|
||||||
serialVal = serialRead();
|
serialVal = Serial.read();
|
||||||
if (serialVal != -1){
|
if (serialVal != -1){
|
||||||
printString("information there");
|
Serial.print("information there");
|
||||||
if ((serialVal == 43) && !reading){
|
if ((serialVal == 43) && !reading){
|
||||||
reading = true;
|
reading = true;
|
||||||
printString("plus");
|
Serial.print("plus");
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((serialVal == 45) && (times == 0 && reading)){
|
if ((serialVal == 45) && (times == 0 && reading)){
|
||||||
writing = true;
|
writing = true;
|
||||||
printString("minus");
|
Serial.print("minus");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (reading && writing){
|
if (reading && writing){
|
||||||
@ -150,7 +150,7 @@ void readBuff(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (times >= 7){
|
if (times >= 7){
|
||||||
printString("Print to buff");
|
Serial.print("Print to buff");
|
||||||
times = 0;
|
times = 0;
|
||||||
reading = false;
|
reading = false;
|
||||||
writing = false;
|
writing = false;
|
||||||
@ -178,7 +178,8 @@ void max7219_init()
|
|||||||
|
|
||||||
// program initialization routine
|
// program initialization routine
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
|
Serial.begin(9600);
|
||||||
setPinModes();
|
setPinModes();
|
||||||
max7219_init();
|
max7219_init();
|
||||||
}
|
}
|
||||||
@ -186,7 +187,7 @@ void setup()
|
|||||||
// program loop
|
// program loop
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
printString("in the loop");
|
Serial.print("in the loop");
|
||||||
//max7219_all(0x00);
|
//max7219_all(0x00);
|
||||||
//delay(500);
|
//delay(500);
|
||||||
readBuff();
|
readBuff();
|
||||||
|
@ -35,7 +35,7 @@ int dato = 0; // dato is a varible we use to send data to the LEDs
|
|||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
//beginSerial(9600); // uncomment the serial-related lines to monitor the program's progress
|
//Serial.begin(9600); // uncomment the serial-related lines to monitor the program's progress
|
||||||
pinMode(data, OUTPUT); // declare all the control pins as outputs
|
pinMode(data, OUTPUT); // declare all the control pins as outputs
|
||||||
pinMode(clock, OUTPUT);
|
pinMode(clock, OUTPUT);
|
||||||
pinMode(strob, OUTPUT);
|
pinMode(strob, OUTPUT);
|
||||||
@ -60,7 +60,7 @@ void loop()
|
|||||||
// go through the "dato" variable and send it bit by bit over the data pin
|
// go through the "dato" variable and send it bit by bit over the data pin
|
||||||
for (count = 0; count < 8; count++) {
|
for (count = 0; count < 8; count++) {
|
||||||
digitalWrite(data, dato & 01);
|
digitalWrite(data, dato & 01);
|
||||||
//serialWrite((dato & 01) + 48);
|
//Serial.print((dato & 01) + 48, BYTE);
|
||||||
dato>>=1;
|
dato>>=1;
|
||||||
if (count == 7){
|
if (count == 7){
|
||||||
digitalWrite(oe, LOW);
|
digitalWrite(oe, LOW);
|
||||||
@ -74,6 +74,6 @@ void loop()
|
|||||||
digitalWrite(strob, LOW);
|
digitalWrite(strob, LOW);
|
||||||
delay(100);
|
delay(100);
|
||||||
|
|
||||||
//printNewline();
|
//Serial.println();
|
||||||
delay(100); // waits for a second
|
delay(100); // waits for a second
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ int potPin = 0; // analog pin where to plug the potentiometer at
|
|||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
pinMode(motorPin, OUTPUT); // declare the motor as an output
|
pinMode(motorPin, OUTPUT); // declare the motor as an output
|
||||||
beginSerial(9600); // connect to the serial port to send values back
|
Serial.begin(9600); // connect to the serial port to send values back
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
@ -23,8 +23,7 @@ void loop() {
|
|||||||
value = analogRead(potPin);
|
value = analogRead(potPin);
|
||||||
|
|
||||||
// print its value back to the computer
|
// print its value back to the computer
|
||||||
printInteger(value);
|
Serial.println(value);
|
||||||
printNewline();
|
|
||||||
|
|
||||||
// turn the motor on
|
// turn the motor on
|
||||||
digitalWrite(motorPin,HIGH);
|
digitalWrite(motorPin,HIGH);
|
||||||
|
@ -1,121 +1,127 @@
|
|||||||
/* Driving two DC motors
|
/* Driving two DC motors
|
||||||
* -----------------------
|
* -----------------------
|
||||||
*
|
*
|
||||||
* Custom timer originally intended to DC motor
|
* Custom timer originally intended to DC motor
|
||||||
* control purposes and made from two nested loops:
|
* control purposes and made from two nested loops:
|
||||||
* - "scan cycle" is the main loop
|
* - "scan cycle" is the main loop
|
||||||
* - "control cycle" is the internal loop
|
* - "control cycle" is the internal loop
|
||||||
* Timing id adjusted by changing number of iterations en each loop
|
* Timing id adjusted by changing number of iterations en each loop
|
||||||
* and the internal delay of each control cycle iteration (tic). If scan
|
* and the internal delay of each control cycle iteration (tic). If scan
|
||||||
* cycle code takes significative time to jam current control cycle, this
|
* cycle code takes significative time to jam current control cycle, this
|
||||||
* delay could be easily reduced or bypassed.
|
* delay could be easily reduced or bypassed.
|
||||||
*
|
*
|
||||||
* (copyleft) 2005 by Quique
|
* (copyleft) 2005 by Quique
|
||||||
* <mailto:info@spindesk.com>
|
* <mailto:info@spindesk.com>
|
||||||
* posted to the Arduino Forum
|
* posted to the Arduino Forum
|
||||||
* <http://www.arduino.cc>
|
* <http://www.arduino.cc>
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
int ledPin0 = 13; // LED connected to digital pin 13
|
int ledPin0 = 13; // LED connected to digital pin 13
|
||||||
int motorPin1 = 9; // Motor 1 connected to digital pin 9
|
boolean ledValue = LOW;
|
||||||
int motorPin2 = 8; // Motor 1 connected to digital pin 8
|
int motorPin1 = 9; // Motor 1 connected to digital pin 9
|
||||||
|
int motorPin2 = 8; // Motor 1 connected to digital pin 8
|
||||||
int potPin1 = 5; // Potentiometer1 connected to analog pin 5 ( 5 is 1 in some boards)
|
|
||||||
int potPin2 = 4; // Potentiometer1 connected to analog pin 5 ( 4 is 2 in some boards)
|
int potPin1 = 5; // Potentiometer1 connected to analog pin 5 ( 5 is 1 in some boards)
|
||||||
|
int potPin2 = 4; // Potentiometer1 connected to analog pin 5 ( 4 is 2 in some boards)
|
||||||
// Timing Setup
|
|
||||||
|
// Timing Setup
|
||||||
int scanCycle = 10; // Number of control cycles in a scan cycle
|
|
||||||
int controlCycle = 200; // Control cycle iterations
|
int scanCycle = 10; // Number of control cycles in a scan cycle
|
||||||
int tic = 6; // Control cycle iteration aditional delay in microseconds
|
int controlCycle = 200; // Control cycle iterations
|
||||||
|
int tic = 6; // Control cycle iteration aditional delay in microseconds
|
||||||
int currentSCycle = 0; // Current scan cycle iteration
|
|
||||||
int currentCCycle = 0; // Current control cycle iteration
|
int currentSCycle = 0; // Current scan cycle iteration
|
||||||
boolean scanEnable = true; // Allows read analog & digital inputs
|
int currentCCycle = 0; // Current control cycle iteration
|
||||||
|
boolean scanEnable = true; // Allows read analog & digital inputs
|
||||||
// End timing setup
|
|
||||||
|
// End timing setup
|
||||||
int counter = 0; // Scan cycle counter used to change led status
|
|
||||||
int motor1_PW; // motor 1 Pulse Width
|
int counter = 0; // Scan cycle counter used to change led status
|
||||||
int motor2_PW; // motor 2 Pulse Width
|
int motor1_PW; // motor 1 Pulse Width
|
||||||
|
int motor2_PW; // motor 2 Pulse Width
|
||||||
/*
|
|
||||||
* Switch the boolean value assigned to any variable
|
/*
|
||||||
*/
|
* Switch the boolean value assigned to any variable
|
||||||
boolean boolSwitch (boolean *target)
|
*/
|
||||||
{
|
boolean boolSwitch (boolean *target)
|
||||||
if ( *target ) {*target = false;} else { *target = true; }
|
{
|
||||||
return *target;
|
if ( *target ) {
|
||||||
}
|
*target = false;
|
||||||
|
}
|
||||||
void setup()
|
else {
|
||||||
{
|
*target = true;
|
||||||
pinMode (ledPin0, OUTPUT); // sets the digital pin as output
|
}
|
||||||
pinMode (motorPin1, OUTPUT); // sets the digital pin as output
|
return *target;
|
||||||
pinMode (motorPin2, OUTPUT); // sets the digital pin as output
|
}
|
||||||
}
|
|
||||||
|
void setup()
|
||||||
void loop()
|
{
|
||||||
{
|
pinMode (ledPin0, OUTPUT); // sets the digital pin as output
|
||||||
// Scan cycle
|
pinMode (motorPin1, OUTPUT); // sets the digital pin as output
|
||||||
|
pinMode (motorPin2, OUTPUT); // sets the digital pin as output
|
||||||
if (scanEnable)
|
}
|
||||||
{
|
|
||||||
//
|
void loop()
|
||||||
// Scan cycle logic here
|
{
|
||||||
//
|
// Scan cycle
|
||||||
motor1_PW = analogRead (potPin1)/5; // Pot 1 read scale change
|
|
||||||
motor2_PW = analogRead (potPin2)/5; // Pot 1 read scale change
|
if (scanEnable)
|
||||||
|
{
|
||||||
// Swith led in pin 13 each 10 scan cycles. We can assume that while
|
//
|
||||||
// Led is on (or off), porgram has taken 10 scan cycles. So, if we adjust
|
// Scan cycle logic here
|
||||||
// blink time to 1 sec, we are able to scanning inputs each 100 msec.
|
//
|
||||||
if (counter++ >= 10)
|
motor1_PW = analogRead (potPin1)/5; // Pot 1 read scale change
|
||||||
{
|
motor2_PW = analogRead (potPin2)/5; // Pot 1 read scale change
|
||||||
digitalWrite (ledPin0, boolSwitch (ledPin0)); // Led blink each 10 scan cycles i.e. if
|
|
||||||
// led is on 1 second, we are scaning knobs each
|
// Swith led in pin 13 each 10 scan cycles. We can assume that while
|
||||||
// 100 miliseconds
|
// Led is on (or off), porgram has taken 10 scan cycles. So, if we adjust
|
||||||
counter =0;
|
// blink time to 1 sec, we are able to scanning inputs each 100 msec.
|
||||||
}
|
if (counter++ >= 10)
|
||||||
}
|
{
|
||||||
|
digitalWrite (ledPin0, boolSwitch (&ledValue)); // Led blink each 10 scan cycles i.e. if
|
||||||
// Control cycle
|
// led is on 1 second, we are scaning knobs each
|
||||||
for (currentCCycle = 0; currentCCycle < controlCycle; currentCCycle ++)
|
// 100 miliseconds
|
||||||
{
|
counter =0;
|
||||||
delayMicroseconds (tic);
|
}
|
||||||
//
|
}
|
||||||
// Control cycle logic here
|
|
||||||
//
|
// Control cycle
|
||||||
if ( motor1_PW > currentCCycle )
|
for (currentCCycle = 0; currentCCycle < controlCycle; currentCCycle ++)
|
||||||
{
|
{
|
||||||
digitalWrite ( motorPin1, LOW);
|
delayMicroseconds (tic);
|
||||||
}
|
//
|
||||||
else
|
// Control cycle logic here
|
||||||
{
|
//
|
||||||
digitalWrite ( motorPin1, HIGH);
|
if ( motor1_PW > currentCCycle )
|
||||||
}
|
{
|
||||||
|
digitalWrite ( motorPin1, LOW);
|
||||||
if ( motor2_PW > currentCCycle )
|
}
|
||||||
{
|
else
|
||||||
digitalWrite ( motorPin2, LOW);
|
{
|
||||||
}
|
digitalWrite ( motorPin1, HIGH);
|
||||||
else
|
}
|
||||||
{
|
|
||||||
digitalWrite ( motorPin2, HIGH);
|
if ( motor2_PW > currentCCycle )
|
||||||
}
|
{
|
||||||
|
digitalWrite ( motorPin2, LOW);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
// Detect completed scan cycle
|
{
|
||||||
if ( currentSCycle ++ > scanCycle)
|
digitalWrite ( motorPin2, HIGH);
|
||||||
{
|
}
|
||||||
scanEnable = true; // Allow readings of external inputs
|
|
||||||
currentSCycle = 0; // Reset scan cycle counter
|
}
|
||||||
}
|
|
||||||
else
|
// Detect completed scan cycle
|
||||||
{
|
if ( currentSCycle ++ > scanCycle)
|
||||||
scanEnable = false; //
|
{
|
||||||
}
|
scanEnable = true; // Allow readings of external inputs
|
||||||
}
|
currentSCycle = 0; // Reset scan cycle counter
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
scanEnable = false; //
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -30,7 +30,7 @@ void readSerialString (char *strArray) {
|
|||||||
while (serialAvailable()){
|
while (serialAvailable()){
|
||||||
strArray[i] = Serial.read();
|
strArray[i] = Serial.read();
|
||||||
i++;
|
i++;
|
||||||
Serial.write(strArray[(i-1)]); //optional: for confirmation
|
Serial.print(strArray[(i-1)]); //optional: for confirmation
|
||||||
}
|
}
|
||||||
Serial.println(); //optional: for confirmation
|
Serial.println(); //optional: for confirmation
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@ void loop () {
|
|||||||
|
|
||||||
// if the input is '-1' then there is no data
|
// if the input is '-1' then there is no data
|
||||||
// at the input, otherwise store it
|
// at the input, otherwise store it
|
||||||
if (val != -1) {
|
if (serbyte != -1) {
|
||||||
val = serbyte;
|
val = serbyte;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user