Skip to content
This repository was archived by the owner on Jun 5, 2024. It is now read-only.

Commit b104298

Browse files
author
amd989
committed
Small Fixes
Changing serial baud rate Adding code to remember the last position and on open go to that position Changing delay on tilt button. Changing min and max positions (these depends on current sensor readout)
1 parent a42994a commit b104298

File tree

6 files changed

+191
-19
lines changed

6 files changed

+191
-19
lines changed

Code/AnalogInput/AnalogInput.ino

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ int sensorPin = A0; // select the input pin for the potentiometer
3131
int ledPin = 13; // select the pin for the LED
3232
int sensorValue = 0; // variable to store the value coming from the sensor
3333

34-
vxoid setup() {
35-
Serial.begin(9600);
34+
void setup() {
35+
Serial.begin(57600);
3636
// declare the ledPin as an OUTPUT:
3737
pinMode(ledPin, OUTPUT);
3838
}

Code/Code.ino

+6-6
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const int ForwardPin = 4;
3535
const int BackwardsPin = 5;
3636
const int OpenClosePin = 2;
3737
const int TiltPin = 3;
38-
const int AccessoryPin = 6;
38+
const int AccessoryPin = 8;
3939
const int LEDPin = 13;
4040

4141
// Handles Status
@@ -86,8 +86,8 @@ void loop() {
8686

8787
if(isLidClosed())
8888
{
89-
// if lid is closed, open it.
90-
currentStatus.lidStatus = openLid();
89+
// if lid is closed, open it.
90+
currentStatus.lidStatus = openLid(OpenStatus);
9191
}
9292
else
9393
{
@@ -101,8 +101,8 @@ void loop() {
101101
serialPrintLn("Accessory Power: ON ");
102102
serialPrintLn("Tilt button pressed...");
103103
// if lid is open, tilt it.
104-
currentStatus.lidStatus = tiltLid(currentStatus.lidStatus);
105-
delay(500); // Add small delay to prevent double triggering. (poor's man debouncer)
104+
currentStatus.lidStatus = tiltLid(currentStatus.lidStatus);
105+
delay(250); // Add small delay to prevent double triggering. (poor's man debouncer)
106106
}
107107
}
108108
idle();
@@ -128,7 +128,7 @@ void idle()
128128
delay(3000);
129129
serialPrintLn("Previous lid state: opened...");
130130
// If car is turned on and lid was open before, open it again.
131-
currentStatus.lidStatus = openLid();
131+
currentStatus.lidStatus = openLid(currentStatus.oldLidStatus);
132132
}
133133
}
134134
else

Code/LidPosition.ino

+5
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ LidPosition getNextTiltedStatusPositions(int currentLidStatus)
5858
break;
5959
}
6060
serialPrintLn("Moving: ", lidPosition.MovementDirection);
61+
serialPrintLn("Moving to Status: ", lidPosition.LidStatus);
62+
serialPrintLn("MinPosition : ", lidPosition.MinPosition);
63+
serialPrintLn("MaxPosition : ", lidPosition.MaxPosition);
64+
65+
return lidPosition;
6166
}
6267

6368
/// Checks whether the lid is closer or not.

Code/Movement.ino

+3-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ int moveLid(int minPosition, int maxPosition, int movementDirection, int newLidS
3737
{
3838
if(movementDirection == ForwardMovement)
3939
{
40+
serialPrintLn("P: ", currentPosition);
4041
moveForward();
4142
}
4243
else
@@ -80,7 +81,7 @@ int openLid()
8081
}
8182

8283
// Opens the lid to the specified status.
83-
void openLid(int newLidStatus)
84+
int openLid(int newLidStatus)
8485
{
8586
serialPrintLn("Opening Lid to defined Status...");
8687
// Gets the destination position for the new specified status
@@ -89,6 +90,7 @@ void openLid(int newLidStatus)
8990
// Move Lid
9091
moveLid(lidPosition.MaxPosition, ClosedPositionMax, BackwardsMovement, newLidStatus);
9192
serialPrintLn("Complete");
93+
return newLidStatus;
9294
}
9395

9496
/// Tilts the lid to any of the 3 possible positions.

Code/functions.h

+10-10
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@ const int ForwardMovement = 0;
33
const int BackwardsMovement = 1;
44

55
// Thresholds for positions.
6-
const int OpenPositionMin = 145;
7-
const int OpenPositionMax = 166;
6+
const int OpenPositionMin = 100;
7+
const int OpenPositionMax = 205;
88

9-
const int ClosedPositionMin = 970;
10-
const int ClosedPositionMax = 990;
9+
const int ClosedPositionMin = 1005;
10+
const int ClosedPositionMax = 1024;
1111

12-
const int tilt0PositionMin = 195;
13-
const int tilt0PositionMax = 205;
12+
const int tilt0PositionMin = 240; // 195;
13+
const int tilt0PositionMax = 250; // 205;
1414

15-
const int tilt1PositionMin = 255;
16-
const int tilt1PositionMax = 265;
15+
const int tilt1PositionMin = 300; // 255;
16+
const int tilt1PositionMax = 310; // 265;
1717

18-
const int tilt2PositionMin = 330; // 90 degrees
19-
const int tilt2PositionMax = 340;
18+
const int tilt2PositionMin = 375; // 330; // 90 degrees
19+
const int tilt2PositionMax = 385; // 340;
2020

2121
// Status
2222
const int ClosedStatus = 0;

Code/sleep/sleep.ino

+165
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
#include <avr/sleep.h>
2+
3+
/* Sleep Demo Serial
4+
* -----------------
5+
* Example code to demonstrate the sleep functions in an Arduino.
6+
*
7+
* use a resistor between RX and pin2. By default RX is pulled up to 5V
8+
* therefore, we can use a sequence of Serial data forcing RX to 0, what
9+
* will make pin2 go LOW activating INT0 external interrupt, bringing
10+
* the MCU back to life
11+
*
12+
* there is also a time counter that will put the MCU to sleep after 10 secs
13+
*
14+
* NOTE: when coming back from POWER-DOWN mode, it takes a bit
15+
* until the system is functional at 100%!! (typically <1sec)
16+
*
17+
* Copyright (C) 2006 MacSimski 2006-12-30
18+
* Copyright (C) 2007 D. Cuartielles 2007-07-08 - Mexico DF
19+
*
20+
* This program is free software: you can redistribute it and/or modify
21+
* it under the terms of the GNU General Public License as published by
22+
* the Free Software Foundation, either version 3 of the License, or
23+
* (at your option) any later version.
24+
*
25+
* This program is distributed in the hope that it will be useful,
26+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
27+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28+
* GNU General Public License for more details.
29+
*
30+
* You should have received a copy of the GNU General Public License
31+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
32+
*
33+
*/
34+
35+
int wakePin = 2; // pin used for waking up
36+
int sleepStatus = 0; // variable to store a request for sleep
37+
int count = 0; // counter
38+
39+
void wakeUpNow() // here the interrupt is handled after wakeup
40+
{
41+
// execute code here after wake-up before returning to the loop() function
42+
// timers and code using timers (serial.print and more...) will not work here.
43+
// we don't really need to execute any special functions here, since we
44+
// just want the thing to wake up
45+
}
46+
47+
void setup()
48+
{
49+
pinMode(wakePin, INPUT_PULLUP);
50+
51+
Serial.begin(9600);
52+
53+
/* Now it is time to enable an interrupt. In the function call
54+
* attachInterrupt(A, B, C)
55+
* A can be either 0 or 1 for interrupts on pin 2 or 3.
56+
*
57+
* B Name of a function you want to execute while in interrupt A.
58+
*
59+
* C Trigger mode of the interrupt pin. can be:
60+
* LOW a low level trigger
61+
* CHANGE a change in level trigger
62+
* RISING a rising edge of a level trigger
63+
* FALLING a falling edge of a level trigger
64+
*
65+
* In all but the IDLE sleep modes only LOW can be used.
66+
*/
67+
68+
attachInterrupt(0, wakeUpNow, LOW); // use interrupt 0 (pin 2) and run function
69+
// wakeUpNow when pin 2 gets LOW
70+
}
71+
72+
void sleepNow() // here we put the arduino to sleep
73+
{
74+
/* Now is the time to set the sleep mode. In the Atmega8 datasheet
75+
* http://www.atmel.com/dyn/resources/prod_documents/doc2486.pdf on page 35
76+
* there is a list of sleep modes which explains which clocks and
77+
* wake up sources are available in which sleep mode.
78+
*
79+
* In the avr/sleep.h file, the call names of these sleep modes are to be found:
80+
*
81+
* The 5 different modes are:
82+
* SLEEP_MODE_IDLE -the least power savings
83+
* SLEEP_MODE_ADC
84+
* SLEEP_MODE_PWR_SAVE
85+
* SLEEP_MODE_STANDBY
86+
* SLEEP_MODE_PWR_DOWN -the most power savings
87+
*
88+
* For now, we want as much power savings as possible, so we
89+
* choose the according
90+
* sleep mode: SLEEP_MODE_PWR_DOWN
91+
*
92+
*/
93+
set_sleep_mode(SLEEP_MODE_IDLE); // sleep mode is set here
94+
95+
sleep_enable(); // enables the sleep bit in the mcucr register
96+
// so sleep is possible. just a safety pin
97+
98+
/* Now it is time to enable an interrupt. We do it here so an
99+
* accidentally pushed interrupt button doesn't interrupt
100+
* our running program. if you want to be able to run
101+
* interrupt code besides the sleep function, place it in
102+
* setup() for example.
103+
*
104+
* In the function call attachInterrupt(A, B, C)
105+
* A can be either 0 or 1 for interrupts on pin 2 or 3.
106+
*
107+
* B Name of a function you want to execute at interrupt for A.
108+
*
109+
* C Trigger mode of the interrupt pin. can be:
110+
* LOW a low level triggers
111+
* CHANGE a change in level triggers
112+
* RISING a rising edge of a level triggers
113+
* FALLING a falling edge of a level triggers
114+
*
115+
* In all but the IDLE sleep modes only LOW can be used.
116+
*/
117+
118+
attachInterrupt(0,wakeUpNow, LOW); // use interrupt 0 (pin 2) and run function
119+
// wakeUpNow when pin 2 gets LOW
120+
121+
sleep_mode(); // here the device is actually put to sleep!!
122+
// THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP
123+
124+
sleep_disable(); // first thing after waking from sleep:
125+
// disable sleep...
126+
detachInterrupt(0); // disables interrupt 0 on pin 2 so the
127+
// wakeUpNow code will not be executed
128+
// during normal running time.
129+
130+
}
131+
132+
void loop()
133+
{
134+
// display information about the counter
135+
Serial.print("Awake for ");
136+
Serial.print(count);
137+
Serial.println("sec");
138+
count++;
139+
delay(1000); // waits for a second
140+
141+
// compute the serial input
142+
if (Serial.available()) {
143+
int val = Serial.read();
144+
if (val == 'S') {
145+
Serial.println("Serial: Entering Sleep mode");
146+
delay(100); // this delay is needed, the sleep
147+
//function will provoke a Serial error otherwise!!
148+
count = 0;
149+
sleepNow(); // sleep function called here
150+
}
151+
if (val == 'A') {
152+
Serial.println("Hola Caracola"); // classic dummy message
153+
}
154+
}
155+
156+
// check if it should go to sleep because of time
157+
if (count >= 10) {
158+
Serial.println("Timer: Entering Sleep mode");
159+
delay(100); // this delay is needed, the sleep
160+
//function will provoke a Serial error otherwise!!
161+
count = 0;
162+
sleepNow(); // sleep function called here
163+
}
164+
}
165+

0 commit comments

Comments
 (0)