So I am programming scrips with Arduino and I am supposed to make an arrow that turns 90 degrees when the up button is pressed. I don't really understand how I am supposed to make it turn when the Up button is pressed. I provided the whole code if you want to test it. I'm doing it in german so "unten" is bottom and "oben" is top
#include <Arduino.h>
#include <Adafruit_GFX.h>
#include <Adafruit_LEDBackpack.h>
Adafruit_8x8matrix matrix = Adafruit_8x8matrix();
const int IO_BUTTON_UP = 4;
void setup() {
Serial.begin(115200);
Serial.println("Matrix8x8");
Serial.println("<Autor>");
matrix.begin(0x70);
matrix.setBrightness(1);
pinMode(IO_BUTTON_UP, INPUT);
}
int loops = 0;
void loop() {
Serial.println("loop");
int ON = 1;
int OFF = 0;
int unten = 0;
int oben = 7;
int rotation = 0;
matrix.setRotation(rotation);
if (digitalRead(IO_BUTTON_UP) == 1){
rotation ++;
}
if (loops == 0){
unten = 3;
oben = 4;
} else if (loops == 1){
unten = 3;
oben = 4;
} else if (loops == 2){
unten = 3;
oben = 4;
} else if (loops == 3){
unten = 3;
oben = 4;
} else if (loops == 4){
unten = 0;
oben = 7;
} else if (loops == 5){
unten = 1;
oben = 6;
} else if (loops == 6){
unten = 2;
oben = 5;
} else if (loops == 7){
unten = 3;
oben = 4;
}
for (int i = unten; i <= oben; i++){
matrix.drawPixel(loops, i, ON);
}
matrix.writeDisplay();
delay(200);
if (loops == 7){
delay (200);
matrix.clear();
matrix.writeDisplay();
delay (200);
}
loops = (loops = loops + 1) % 8;
Serial.print ("loops: ");
Serial.println (loops);
}
I got an answer. I just had to define "rotation" outside of the "void loop"
#include <Arduino.h>
#include <Adafruit_GFX.h>
#include <Adafruit_LEDBackpack.h>
Adafruit_8x8matrix matrix = Adafruit_8x8matrix();
int IO_BUTTON_UP = 4;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Matrix8x8");
Serial.println("<Autor>");
matrix.begin(0x70);
matrix.setBrightness(1);
pinMode(IO_BUTTON_UP, INPUT);
}
int loops = 0;
int rotation = 0;
void loop() {
// put your main code here, to run repeatedly:
Serial.println("loop");
int ON = 1;
int OFF = 0;
int unten = 0;
int oben = 7;
Serial.println(IO_BUTTON_UP);
if (digitalRead(IO_BUTTON_UP) == 1){
rotation++;
}
matrix.setRotation(rotation);
if (loops == 0){
unten = 3;
oben = 4;
} else if (loops == 1){
unten = 3;
oben = 4;
} else if (loops == 2){
unten = 3;
oben = 4;
} else if (loops == 3){
unten = 3;
oben = 4;
} else if (loops == 4){
unten = 0;
oben = 7;
} else if (loops == 5){
unten = 1;
oben = 6;
} else if (loops == 6){
unten = 2;
oben = 5;
} else if (loops == 7){
unten = 3;
oben = 4;
}
for (int i = unten; i <= oben; i++){
matrix.drawPixel(loops, i, ON);
}
matrix.writeDisplay();
delay(200);
if (loops == 7){
delay (200);
matrix.clear();
matrix.writeDisplay();
delay (200);
}
loops = (loops = loops + 1) % 8;
Serial.print ("loops: ");
Serial.println (loops);
}
Related
//Program by: Alim Mulyadi
//tgl: 21/02/2021
//Program: Countdown Timer Sederha1. na
[enter image description here](https://i.stack.imgur.com/58w5W.jpg)
#include <LiquidCrystal_I2C.h>
#include "Countimer.h"
Countimer tdown;
LiquidCrystal_I2C lcd(0x3F, 16, 2);
#define pb_set A0 //Set
#define pb_up A1 //Up dan Start
#define pb_down A2 //Down, Pause, dan Reset
int relay = 4;
int buzzer = 9;
int set_detik = 0;
int set_menit = 0;
int set_jam = 0;
int last_set_detik = 0;
int last_set_menit = 0;
int last_set_jam = 0;
int set = 0;
bool kondisi_set = 0;
bool kondisi_relay = 0;
bool kondisi_reset = 0;
unsigned long lastmillis;
void setup() {
Serial.begin (9600);
pinMode(pb_set, INPUT_PULLUP);
pinMode(pb_up, INPUT_PULLUP);
pinMode(pb_down, INPUT_PULLUP);
pinMode(relay, OUTPUT);
pinMode(buzzer, OUTPUT);
lcd.begin();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Countdown Timer");
lcd.setCursor(0, 1);
lcd.print("By: Alim Mulyadi");
tdown.setInterval(print_time, 1000);
delay(2000);
lcd.clear();
}
void print_time() {
set_detik = set_detik - 1;
if (set_detik < 0) {
set_detik = 59;
set_menit = set_menit - 1;
}
if (set_menit < 0) {
set_menit = 59;
set_jam = set_jam - 1;
}
}
void loop() {
tdown.run();
if (digitalRead (pb_set) == 0) {
if (kondisi_set == 0 && kondisi_relay == 0) {
kondisi_set = 1;
set = set + 1;
if (set > 3) {
set = 0;
}
delay(100);
}
} else {
kondisi_set = 0;
}
if (digitalRead (pb_up) == 0) {
if (set == 0) {
tdown.start();
kondisi_relay = 1;
}
if (set == 1) {
set_detik++;
last_set_detik = set_detik;
}
if (set == 2) {
set_menit++;
last_set_menit = set_menit;
}
if (set == 3) {
set_jam++;
last_set_jam = set_jam;
}
if (set_detik > 59) {
set_detik = 0;
}
if (set_menit > 59) {
set_menit = 0;
}
if (set_jam > 99) {
set_jam = 0;
}
delay(200);
}
if (digitalRead (pb_down) == 0) {
//delay(500);
lastmillis = millis();
kondisi_reset = 0;
while (digitalRead (pb_down) == 0 && set == 0) {
if (millis() - lastmillis > 500) {
kondisi_reset = 1;
kondisi_relay = 0;
tdown.stop();
set_detik = last_set_detik;
set_menit = last_set_menit;
set_jam = last_set_jam;
lcd.setCursor(4, 1);
if (set_jam <= 9) {
lcd.print("0");
}
lcd.print(set_jam);
lcd.print(":");
if (set_menit <= 9) {
lcd.print("0");
}
lcd.print(set_menit);
lcd.print(":");
if (set_detik <= 9) {
lcd.print("0");
}
lcd.print(set_detik);
lcd.print(" ");
delay(100);
}
}
if (kondisi_reset == 0) {
if (set == 0) {
tdown.stop();
kondisi_relay = 0;
}
if (set == 1) {
set_detik--;
last_set_detik = set_detik;
}
if (set == 2) {
set_menit--;
last_set_menit = set_menit;
}
if (set == 3) {
set_jam--;
last_set_jam = set_jam;
}
if (set_detik < 0) {
set_detik = 59;
}
if (set_menit < 0) {
set_menit = 59;
}
if (set_jam < 0) {
set_jam = 99;
}
delay(200);
}
}
lcd.setCursor(0, 0);
if (set == 0) {
lcd.print(" Timer ");
}
if (set == 1) {
lcd.print(" Set Timer SS ");
}
if (set == 2) {
lcd.print(" Set Timer MM ");
}
if (set == 3) {
lcd.print(" Set Timer HH ");
}
lcd.setCursor(4, 1);
if (set_jam <= 9) {
lcd.print("0");
}
lcd.print(set_jam);
lcd.print(":");
if (set_menit <= 9) {
lcd.print("0");
}
lcd.print(set_menit);
lcd.print(":");
if (set_detik <= 9) {
lcd.print("0");
}
lcd.print(set_detik);
lcd.print(" ");
if (set_detik == 0 && set_menit == 0 && set_jam == 0 && kondisi_relay == 1) {
kondisi_relay = 0;
tdown.stop();
digitalWrite(relay, LOW);
digitalWrite(buzzer, HIGH);
delay(300);
digitalWrite(buzzer, LOW);
delay(200);
digitalWrite(buzzer, HIGH);
delay(300);
digitalWrite(buzzer, LOW);
delay(200);
digitalWrite(buzzer, HIGH);
delay(300);
digitalWrite(buzzer, LOW);
}
if (kondisi_relay == 1) {
digitalWrite(relay, HIGH);
}
else {
digitalWrite(relay, LOW);
}
delay(1);
}
I hope to find the answer
I'm currently programming an alarm clock with a 4x4 keypad, i2c 20x4 Lcd, DS3231 and buzzer.
Everything works, but I have problems with implementing the menu to change alarm times and other settings.
Sadly I'm under time pressure so this is very important to me. Any help is acknowledged.
The menu shows up and when I press a key it returns to the normal screen showing the clock. I tried debugging with Serial and it breaks off here (a schematic ) :
void menu(int menupos) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("1. Alarme einstellen");
lcd.setCursor(0, 1);
lcd.print("2. Sprache einst.");
lcd.setCursor(0, 2);
lcd.print("3. Beleuchtung einst");
lcd.setCursor(0, 3);
lcd.print("9. Zurueck");
keypressm = NO_KEY;
while (keypressm == NO_KEY) {
keypressm = Tastenfeld2.getKey();
// A Serial Print here still works but after a key is pressed it returns to the clock.
}
if (keypressm == "1") {
menuSetAlarm();
} else if (keypressm == "2") {
menuSetLanguage();
} else if (keypressm == "3") {
menuOtherSettings();
} else if (keypressm == "9") {
lcd.clear();
loop();
}
lcd.clear();
}
Here's the whole code (without EEPROM stuff)
#include <TimedAction.h>
#include <Wire.h> // I2C Adress
#include <LiquidCrystal_I2C.h> // Library for I2C LCD Displays
#include <Keypad.h> // Keypad Libary
#include "RTClib.h" // Library for DS3231 RTC
//Hier wird die größe des Keypads definiert
const byte COLS = 4; //4 Spalten
const byte ROWS = 4; //4 Zeilen
//Die Ziffern und Zeichen des Keypads werden eingegeben:
char hexaKeys[ROWS][COLS] = {
{'1', '4', '7', '*'},
{'2', '5', '8', '0'},
{'3', '6', '9', '#'},
{'A', 'B', 'C', 'D'}
};
RTC_DS3231 rtc; // declare RTC
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // declare LCD module (I2C adress, RS, E, D4, D5, D6, D7, backlightpin, polarity)
DateTime now;
byte colPins[COLS] = {2, 3, 4, 5}; //Definition der Pins für die 4 Spalten
byte rowPins[ROWS] = {6, 7, 8, 9}; //Definition der Pins für die 4 Zeilen
char key; //Taste ist die Variable für die jeweils gedrückte Taste.
char keypressx; //Taste um alarm abzubrechen
char keypressm; //menukeyw
Keypad Tastenfeld = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); //Das Keypad kann absofort mit "Tastenfeld" angesprochen werden
Keypad Tastenfeld2 = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); //Das Keypad kann absofort mit "Tastenfeld2" angesprochen werden
int menupos = -1;
boolean currentStateBacklight = true;
boolean scheduledBacklight = false;
int bs_ontime;
int bs_offtime;
const int alarm_pin = 10;
boolean endedAlarm = false;
int endedAlarmTime;
boolean alarm1_active = false;
boolean alarm2_active = false;
String alarm1_hour;
String alarm1_minute;
String alarm1_second;
String alarm1Days[] = {};
String alarm2_hour;
String alarm2_minute;
String alarm2_second;
String alarm2Days[] = {};
char daysOfTheWeek[7][12] = {"Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"};
void setup() {
Serial.begin(250000);
delay(3000); // wait for console opening
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, lets set the time!");
// Comment out below lines once you set the date & time.
// Following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
lcd.setBacklight(HIGH);
lcd.begin(20, 4); // set up the LCD's number of columns and rows
Wire.begin();
}
void loop() {
key = NO_KEY;
keypressx = NO_KEY;
keypressm = NO_KEY;
while (key == NO_KEY) {
key = Tastenfeld.getKey();
DateTime now = rtc.now();
if (endedAlarm) {
if ((millis() - endedAlarmTime) > 60000) {
endedAlarm = false;
endedAlarmTime = 0;
}
}
String nowhour = correctedValue(now.hour());
String nowmin = correctedValue(now.minute());
String nowsec = correctedValue(now.second());
String nowday = daysOfTheWeek[now.dayOfTheWeek()];
if (alarm1_active && nowhour == alarm1_hour && nowmin == alarm1_minute && !endedAlarm && arrayIncludeElement(alarm1Days, nowday)) { // && correctedValue(now.second()) == alarm1_second) {
lcd.clear();
lcd.setCursor(1, 10);
lcd.print("Get up !!!"); //Message to show when the alarm is ringing
int z = 0;
int x = 1000;
int y = 430;
while (keypressx == NO_KEY) {
tone(alarm_pin, 2870);
keypressx = Tastenfeld.getKey();
delay(y);
noTone(alarm_pin);
keypressx = Tastenfeld.getKey();
delay(x);
x = x * 0.95999;
y = y * 0.97999;
keypressx = Tastenfeld.getKey();
}
endedAlarm = true;
endedAlarmTime = millis();
lcd.clear();
}
if (alarm2_active && nowhour == alarm2_hour && nowmin == alarm2_minute && !endedAlarm && arrayIncludeElement(alarm2Days, nowday)) {
lcd.clear();
lcd.setCursor(1, 10);
lcd.print("Get up !!!"); //Message to show when the alarm is ringing
int z = 0;
int x = 1000;
int y = 430;
while (keypressx == NO_KEY) {
tone(alarm_pin, 2870);
keypressx = Tastenfeld.getKey();
delay(y);
noTone(alarm_pin);
keypressx = Tastenfeld.getKey();
delay(x);
x = x * 0.95999;
y = y * 0.97999;
keypressx = Tastenfeld.getKey();
}
endedAlarm = true;
endedAlarmTime = millis();
lcd.clear();
}
keypressx = NO_KEY;
noTone(alarm_pin);
now = rtc.now();
lcd.setCursor(0, 0); //Text soll beim ersten Zeichen in der ersten Reihe beginnen..
lcd.print(""); //In der ersten Zeile soll der Text „Test Zeile 1“ angezeigt werden
lcd.setCursor(18, 0);
if (alarm1_active == true) {
lcd.print("A1");
}
lcd.setCursor(6, 1); //Genauso geht es bei den weiteren drei Zeilen weiter
lcd.print(correctedValue(now.hour()) + ":" + correctedValue(now.minute()) + ":" + correctedValue(now.second()));
lcd.setCursor(18, 1);
if (alarm2_active == true) {
lcd.print("A2");
}
lcd.setCursor(7, 2);
lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
lcd.setCursor(5, 3);
lcd.print(correctedValue(now.day()) + "." + correctedValue(now.month()) + "." + String(now.year()));
delay(100);
}
Serial.println(key);
if (key == '1') {
if (alarm1_active) {
alarm1_active = false;
lcd.clear();
} else {
alarm1_active = true;
}
}
if (key == '2') {
if (alarm2_active) {
alarm2_active = false;
lcd.clear();
} else {
alarm2_active = true;
}
}
if (key == '*') {
menu(0);
}
}
boolean arrayIncludeElement(String array[], String element) {
for (int i = 0; i < 7; i++) {
if (array[i] == element) {
return true;
}
}
return false;
}
String correctedValue(char x) {
int y;
String z;
y = int(x);
if (y < 10) {
z = String(y);
z = "0" + z;
return z;
} else {
return String(y);
}
}
void menu(int menupos) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("1. Alarme einstellen");
lcd.setCursor(0, 1);
lcd.print("2. Sprache einst.");
lcd.setCursor(0, 2);
lcd.print("3. Beleuchtung einst");
lcd.setCursor(0, 3);
lcd.print("9. Zurueck");
keypressm = NO_KEY;
while (keypressm == NO_KEY) {
keypressm = Tastenfeld2.getKey();
key = "*";
}
if (keypressm == "1") {
//menupos = 1;
menuSetAlarm();
//menupos = 0;
} else if (keypressm == "2") {
//menupos = 2;
menuSetLanguage();
//menupos = 0;
} else if (keypressm == "3") {
//menupos = 3;
menuOtherSettings();
//menupos = 0;
} else if (keypressm == "9") {
lcd.clear();
loop();
//menupos = -1;
}
lcd.clear();
}
void menuSetAlarm() {
keypressm = NO_KEY;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("1. Alarm 1");
lcd.setCursor(0, 1);
lcd.print("2. Alarm 2");
lcd.setCursor(0, 3);
lcd.print("9. Zur");
lcd.print(" #ALT228# ");
lcd.print("ck");
keypressm = Tastenfeld.waitForKey();
if (keypressm == "1") {
} else if (keypressm == "2") {
} else if (keypressm == "9") {
lcd.clear();
menu(0);
}
menu(1);
lcd.clear();
}
void menuSetLanguage() {
keypressm = NO_KEY;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("1. Deutsch");
lcd.setCursor(0, 1);
lcd.print("2. English");
lcd.setCursor(0, 3);
lcd.print("9. Zur");
lcd.print(" #ALT228# ");
lcd.print("ck");
keypressm = Tastenfeld.waitForKey();
if (keypressm == "1") {
changeLanguage("en");
} else if (keypressm == "2") {
changeLanguage("de");
} else if (keypressm == "9") {
lcd.clear();
menu(0);
}
lcd.clear();
menu(2);
}
void menuOtherSettings() {
keypressm = NO_KEY;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("1. Permanent " + !currentStateBacklight);
lcd.setCursor(0, 1);
lcd.print("2. Geplant");
lcd.setCursor(0, 3);
lcd.print("9. Zur");
lcd.print(" #ALT228# ");
lcd.print("ck");
keypressm = Tastenfeld.waitForKey();
if (keypressm == "1") {
currentStateBacklight = !currentStateBacklight;
if (currentStateBacklight) {
lcd.setBacklight(HIGH);
} else {
lcd.setBacklight(LOW);
}
} else if (keypressm == "2") {
scheduledBacklight = true;
int x;
int y;
// set on hour
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Einschaltzeit: (Stunde)");
lcd.setCursor(9, 1);
lcd.print("XX");
keypressm = Tastenfeld.waitForKey();
if (keypressm == "#" || keypressm == "*") {
menu(3);
}
if (keypressm != "#" && keypressm != "*") {
if (keypressm == "0" || keypressm == "1" || keypressm == "2") {
if (keypressm = "0") {
y = 0;
} else if (keypressm = "1") {
y = 1;
} else if (keypressm = "2") {
y = 2;
}
x = y;
}
menu(3);
}
// 2nd digit
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Einschaltzeit: (Stunde)");
lcd.setCursor(9, 1);
lcd.print(x + "X");
keypressm = Tastenfeld.waitForKey();
if (keypressm == "#" || keypressm == "*") {
menu(3);
}
if (x == 2) {
if (keypressm == "0" || keypressm == "1" || keypressm == "2" || keypressm == "3" ) {
if (keypressm = "1") {
y = 1;
} else if (keypressm = "2") {
y = 2;
} else if (keypressm = "3") {
y = 3;
}
x = (x * 10) + y;
bs_ontime = x;
} else if (keypressm == "4") {
bs_ontime = 0;
}
}
if (x == 1) {
if (keypressm = "1") {
y = 1;
} else if (keypressm = "2") {
y = 2;
} else if (keypressm = "3") {
y = 3;
} else if (keypressm = "4") {
y = 4;
} else if (keypressm = "5") {
y = 5;
} else if (keypressm = "6") {
y = 6;
} else if (keypressm = "7") {
y = 7;
} else if (keypressm = "8") {
y = 8;
} else if (keypressm = "9") {
y = 9;
} else if (keypressm = "0") {
y = 0;
}
x = (x * 10) + y;
bs_ontime = x;
}
if (x == 0) {
if (keypressm = "1") {
y = 1;
} else if (keypressm = "2") {
y = 2;
} else if (keypressm = "3") {
y = 3;
} else if (keypressm = "4") {
y = 4;
} else if (keypressm = "5") {
y = 5;
} else if (keypressm = "6") {
y = 6;
} else if (keypressm = "7") {
y = 7;
} else if (keypressm = "8") {
y = 8;
} else if (keypressm = "9") {
y = 9;
} else if (keypressm = "0") {
y = 0;
}
x = (x * 10) + y;
bs_ontime = x;
}
// set on hour
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Einschaltzeit: (Stunde)");
lcd.setCursor(9, 1);
lcd.print("XX");
keypressm = Tastenfeld.waitForKey();
if (keypressm == "#" || keypressm == "*") {
menu(3);
}
if (keypressm != "#" && keypressm != "*") {
if (keypressm == "0" || keypressm == "1" || keypressm == "2") {
if (keypressm = "0") {
y = 0;
} else if (keypressm = "1") {
y = 1;
} else if (keypressm = "2") {
y = 2;
}
x = y;
}
menu(3);
}
// 2nd digit
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Einschaltzeit: (Stunde)");
lcd.setCursor(9, 1);
lcd.print(x + "X");
keypressm = Tastenfeld.waitForKey();
if (keypressm == "#" || keypressm == "*") {
menu(3);
}
if (x == 2) {
if (keypressm == "0" || keypressm == "1" || keypressm == "2" || keypressm == "3" ) {
if (keypressm = "1") {
y = 1;
} else if (keypressm = "2") {
y = 2;
} else if (keypressm = "3") {
y = 3;
}
x = (x * 10) + y;
bs_offtime = x;
} else if (keypressm == "4") {
bs_offtime = 0;
}
}
if (x == 1) {
if (keypressm = "1") {
y = 1;
} else if (keypressm = "2") {
y = 2;
} else if (keypressm = "3") {
y = 3;
} else if (keypressm = "4") {
y = 4;
} else if (keypressm = "5") {
y = 5;
} else if (keypressm = "6") {
y = 6;
} else if (keypressm = "7") {
y = 7;
} else if (keypressm = "8") {
y = 8;
} else if (keypressm = "9") {
y = 9;
} else if (keypressm = "0") {
y = 0;
}
x = (x * 10) + y;
bs_offtime = x;
}
if (x == 0) {
if (keypressm = "1") {
y = 1;
} else if (keypressm = "2") {
y = 2;
} else if (keypressm = "3") {
y = 3;
} else if (keypressm = "4") {
y = 4;
} else if (keypressm = "5") {
y = 5;
} else if (keypressm = "6") {
y = 6;
} else if (keypressm = "7") {
y = 7;
} else if (keypressm = "8") {
y = 8;
} else if (keypressm = "9") {
y = 9;
} else if (keypressm = "0") {
y = 0;
}
x = (x * 10) + y;
bs_offtime = x;
lcd.clear();
}
} else if (keypressm == "9") {
lcd.clear();
menu(0);
}
lcd.clear();
menupos = 1;
}
I think, the problem is loop();. Test the return; method. Example:
void menu(int menupos) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("1. Alarme einstellen");
lcd.setCursor(0, 1);
lcd.print("2. Sprache einst.");
lcd.setCursor(0, 2);
lcd.print("3. Beleuchtung einst");
lcd.setCursor(0, 3);
lcd.print("9. Zurueck");
keypressm = NO_KEY;
while (keypressm == NO_KEY) {
keypressm = Tastenfeld2.getKey();
// A Serial Print here still works but after a key is pressed it returns to the clock.
}
if (keypressm == "1") {
menuSetAlarm();
} else if (keypressm == "2") {
menuSetLanguage();
} else if (keypressm == "3") {
menuOtherSettings();
} else if (keypressm == "9") {
lcd.clear();
return; // !!
}
lcd.clear();
}
Good luck!
I'm trying to write a 3 axis movement stepper link ballscrew setup for which my inputs are xdistance to move, ydistance to move, z distance to move, time duration and nos of loop time for execution. I need to move the 3axis togather having a same start time and stop time.
I have written the below code where my
xdistance to move = 40mm
ydistance to move = 10mm
z distance to move = 5mm
time duration = 4 sec
nos of loop time for execution = 2
I'm facing a problem with having same start & stop time for all the three stepper motors while the distance to move are different for all. But as per my code it should start & stop in same time providing if distance are different there moving speed will vary accordingly.
int t = 4;
int xDist = 40;
const int xPitch = 5;
const int xStepsPerRevolution = 200;
float x_STEP_PERIOD_MS, x_STEP_PERIOD_MS1;
int x_STEPS_NOS = 0;
int x_STEPS_NOS1 = 0;
const int x_STEP_PIN = 5;
const int x_DIR_PIN = 3;
unsigned long xNextStepTime, xNextStepTime1;
volatile int xflag = 1;
volatile int xbackward_finished = 0;
int yDist = 10;
const int yPitch = 5;
const int yStepsPerRevolution = 200;
float y_STEP_PERIOD_MS, y_STEP_PERIOD_MS1;
int y_STEPS_NOS = 0;
int y_STEPS_NOS1 = 0;
const int y_STEP_PIN = 9;
const int y_DIR_PIN = 7;
unsigned long yNextStepTime, yNextStepTime1;
volatile int yflag = 1;
volatile int ybackward_finished = 0;
int zDist = 5;
const int zPitch = 10;
const int zStepsPerRevolution = 400;
float z_STEP_PERIOD_MS, z_STEP_PERIOD_MS1;
int z_STEPS_NOS = 0;
int z_STEPS_NOS1 = 0;
const int z_STEP_PIN = 11;
const int z_DIR_PIN = 13;
unsigned long zNextStepTime, zNextStepTime1;
volatile int zflag = 1;
volatile int zbackward_finished = 0;
int no_of_times=2;
void setup()
{
pinMode(x_STEP_PIN, OUTPUT);
digitalWrite(x_STEP_PIN, LOW);
pinMode(x_DIR_PIN, OUTPUT);
pinMode(A1, INPUT_PULLUP);
pinMode(A2, INPUT_PULLUP);
xNextStepTime = millis();
xNextStepTime1 = millis();
pinMode(y_STEP_PIN, OUTPUT);
digitalWrite(y_STEP_PIN, LOW);
pinMode(y_DIR_PIN, OUTPUT);
pinMode(A3, INPUT_PULLUP);
pinMode(A4, INPUT_PULLUP);
yNextStepTime = millis();
yNextStepTime1 = millis();
pinMode(z_STEP_PIN, OUTPUT);
digitalWrite(z_STEP_PIN, LOW);
pinMode(z_DIR_PIN, OUTPUT);
pinMode(A5, INPUT_PULLUP);
pinMode(A6, INPUT_PULLUP);
zNextStepTime = millis();
zNextStepTime1 = millis();
Serial.begin(9600);
}
void moveforward()
{
x_STEP_PERIOD_MS = ((1000 * t) / ((xStepsPerRevolution / xPitch) * xDist));
unsigned long xtime0 = millis();
if ( (xflag == 1) && ((long)(xNextStepTime - xtime0) < 0) && (x_STEPS_NOS < ((xStepsPerRevolution / xPitch) * xDist)) && ((digitalRead(A1)*digitalRead(A2)) == 1))
{{
digitalWrite(x_DIR_PIN, LOW);
xNextStepTime = xtime0 + x_STEP_PERIOD_MS;
digitalWrite(x_STEP_PIN, HIGH);
delayMicroseconds(2);
digitalWrite(x_STEP_PIN, LOW);
x_STEPS_NOS = x_STEPS_NOS + 1;
}
if (((long)(xNextStepTime - xtime0) == 0) || (x_STEPS_NOS == ((xStepsPerRevolution / xPitch) * xDist)))
{
xflag = 0; xtime0 = 0; x_STEPS_NOS = 0;
}
}
y_STEP_PERIOD_MS = ((1000 * t) / ((yStepsPerRevolution / yPitch) * yDist));
unsigned long ytime0 = millis();
if ( (yflag == 1) && ((long)(yNextStepTime - ytime0) < 0) && (y_STEPS_NOS < ((yStepsPerRevolution / yPitch) * yDist)) && ((digitalRead(A3)*digitalRead(A4)) == 1))
{{
digitalWrite(y_DIR_PIN, HIGH);
yNextStepTime = ytime0 + y_STEP_PERIOD_MS;
digitalWrite(y_STEP_PIN, HIGH);
delayMicroseconds(2);
digitalWrite(y_STEP_PIN, LOW);
y_STEPS_NOS = y_STEPS_NOS + 1;
}
if (((long)(yNextStepTime - ytime0) == 0) || (y_STEPS_NOS == ((yStepsPerRevolution / yPitch) * yDist)))
{
yflag = 0; ytime0 = 0; y_STEPS_NOS = 0;
}
}
z_STEP_PERIOD_MS = ((1000 * t) / ((zStepsPerRevolution / zPitch) * zDist));
unsigned long ztime0 = millis();
if ( (zflag == 1) && ((long)(zNextStepTime - ztime0) < 0) && (z_STEPS_NOS < ((zStepsPerRevolution / zPitch) * zDist)) && ((digitalRead(A5)*digitalRead(A6)) == 1))
{{
digitalWrite(z_DIR_PIN, LOW);
zNextStepTime = ztime0 + z_STEP_PERIOD_MS;
digitalWrite(z_STEP_PIN, HIGH);
delayMicroseconds(2);
digitalWrite(z_STEP_PIN, LOW);
z_STEPS_NOS = z_STEPS_NOS + 1;
}
if (((long)(zNextStepTime - ztime0) == 0) || (z_STEPS_NOS == ((zStepsPerRevolution / zPitch) * zDist)))
{
zflag = 0; ztime0 = 0; z_STEPS_NOS = 0;
}
}
}
void movebackward()
{
x_STEP_PERIOD_MS1 = ((1000 * t) / ((xStepsPerRevolution / xPitch) * xDist));
unsigned long xtime1 = millis();
if ( (xflag == 0) && ((long)(xNextStepTime1 - xtime1) < 0) && (x_STEPS_NOS1 < ((xStepsPerRevolution / xPitch) * xDist)) && ((digitalRead(A1)*digitalRead(A2)) == 1))
{{
digitalWrite(x_DIR_PIN, HIGH);
xNextStepTime1 = xtime1 + x_STEP_PERIOD_MS1;
digitalWrite(x_STEP_PIN, HIGH);
delayMicroseconds(2);
digitalWrite(x_STEP_PIN, LOW);
x_STEPS_NOS1 = x_STEPS_NOS1 + 1;
}
if (((long)(xNextStepTime1 - xtime1) == 0) || (x_STEPS_NOS1 == ((xStepsPerRevolution / xPitch) * xDist)))
{
xflag = 1; xtime1 = 0; x_STEPS_NOS1 = 0;
xbackward_finished = xbackward_finished + 1;
}
}
y_STEP_PERIOD_MS1 = ((1000 * t) / ((yStepsPerRevolution / yPitch) * yDist));
unsigned long ytime1 = millis();
if ( (yflag == 0) && ((long)(yNextStepTime1 - ytime1) < 0) && (y_STEPS_NOS1 < ((yStepsPerRevolution / yPitch) * yDist)) && ((digitalRead(A3)*digitalRead(A4)) == 1))
{{
digitalWrite(y_DIR_PIN, LOW);
yNextStepTime1 = ytime1 + y_STEP_PERIOD_MS1;
digitalWrite(y_STEP_PIN, HIGH);
delayMicroseconds(2);
digitalWrite(y_STEP_PIN, LOW);
y_STEPS_NOS1 = y_STEPS_NOS1 + 1;
}
if (((long)(yNextStepTime1 - ytime1) == 0) || (y_STEPS_NOS1 == ((yStepsPerRevolution / yPitch) * yDist)))
{
yflag = 1; ytime1 = 0; y_STEPS_NOS1 = 0;
ybackward_finished = ybackward_finished + 1;
}
}
z_STEP_PERIOD_MS1 = ((1000 * t) / ((zStepsPerRevolution / zPitch) * zDist));
unsigned long ztime1 = millis();
if ( (zflag == 0) && ((long)(zNextStepTime1 - ztime1) < 0) && (z_STEPS_NOS1 < ((zStepsPerRevolution / zPitch) * zDist)) && ((digitalRead(A5)*digitalRead(A6)) == 1))
{{
digitalWrite(z_DIR_PIN, HIGH);
zNextStepTime1 = ztime1 + z_STEP_PERIOD_MS1;
digitalWrite(z_STEP_PIN, HIGH);
delayMicroseconds(2);
digitalWrite(z_STEP_PIN, LOW);
z_STEPS_NOS1 = z_STEPS_NOS1 + 1;
}
if (((long)(zNextStepTime1 - ztime1) == 0) || (z_STEPS_NOS1 == ((zStepsPerRevolution / zPitch) * zDist)))
{
zflag = 1; ztime1 = 0; z_STEPS_NOS1 = 0;
zbackward_finished = zbackward_finished + 1;
}
}
}
void loop()
{
moveforward();
movebackward();
if (xbackward_finished == no_of_times) {
while (1) {
}
}
}
If anyone has worked on a similar type of problem having different coding approach please share it with me.
I also tried with Arduino due's scheduler library package but for that multiple motor is not working though multiple led sample code is working fine.
I am trying to send data on thingspeak using TCP/IP connection with SIM800L and PIC16F1936. But it is not sending data to thingspeak meanwhile the ADC value is getting changed with change in analog input. I am unable to find what I am missing.
#include "mcc_generated_files/mcc.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#define _XTAL_FREQ 16000000
#define RS RB0
#define RW RB1
#define EN RB2
char buff[80], a;
char data[80];
int flag, b, i = 0;
int adc_val;
bool conn = false;
bool server = false;
float f;
int e;
int val = 0;
char value[5];
//char b,c,d;
void server_conn();
void tcp_send();
void adc_display();
int adc_read(char);
void adc_init();
void send_value();
void lcd_cmd(unsigned char cmd)
{
PORTB = (0xF0 & cmd);
RS = 0;
RW = 0;
EN = 1;
__delay_ms(5);
EN = 0;
PORTB = (cmd<<4 & 0xf0);
RS = 0;
RW = 0;
EN = 1;
__delay_ms(5);
EN = 0;
}
void lcd_data(unsigned char data)
{
PORTB = (0xF0 & data);
RS = 1;
RW = 0;
EN = 1;
__delay_ms(5);
EN = 0;
PORTB = (data<<4 & 0xf0);
RS = 1;
RW = 0;
EN = 1;
__delay_ms(5);
EN = 0;
}
void lcd_string(char *str)
{
while(*str != '\0')
{
lcd_data(*str++);
}
}
void lcd_init()
{
lcd_cmd(0x02);///return to home
lcd_cmd(0x28);///4bitmode
lcd_cmd(0x0C);///cursor off
lcd_cmd(0x01);///display clear
lcd_cmd(0x06);///increment cursor
}
void uart_init()
{
SPBRG = 25; ////9600 baud rate at 16MHz FOSC
SYNC = 0;
BRGH = 0;
BRG16 = 0;
CREN = 1;
TXEN = 1;
SPEN = 1;
GIE = 1;
PEIE = 1;
RCIE = 1;
RCIF = 0;
TRISC6 = 0; ////TX pin
TRISC7 = 1; ////RX pin
}
void gsm_send_char(unsigned char data)
{
while(!TXIF);
TXREG = data;
}
char gsm_receive_char()
{
while(!RCIF);
return RCREG;
}
void gsm_send_string(char *p)
{
while(*p != '\0')
{
gsm_send_char(*p);
p++;
}
}
__interrupt() void isr(void)
{
if(RCIF == 1)
{
RCIF = 0;
a =gsm_receive_char();
buff[i] = a;
i++;
}
}
void main(void)
{
SYSTEM_Initialize();
ANSELA = 0x01; ////Select RA0 as analog
TRISA = 0xFF; ////input PORTA for ADC
TRISB = 0x00;
ANSELB = 0x00;
lcd_init();
uart_init();
lcd_cmd(0x01);
server_conn();
__delay_ms(2000);
adc_init();
while (1)
{
lcd_cmd(0xC0);
lcd_string("SENDING DATA");
adc_display();
tcp_send();
__delay_ms(1000);
}
}
void server_conn()
{
if(server == false)
{
i=0;
gsm_send_string("AT\r");
__delay_ms(500);
buff[sizeof(buff)-1] = '\0';
if(strstr(buff, "OK")!= NULL)
{
lcd_cmd(0xC0);
lcd_string("OK");
memset(buff, 0, sizeof(buff));
}
else{lcd_cmd(0xC0);
lcd_string("AT ERROR");
}
i=0;
gsm_send_string("AT+CIPSHUT\r");
__delay_ms(1000);
buff[sizeof(buff)-1] = '\0';
if(strstr(buff, "OK")!= NULL)
{
lcd_cmd(0xC0);
lcd_string("CIPSHUT OK");
memset(buff, 0, sizeof(buff));
}
else{lcd_cmd(0xC0);
lcd_string("CIPSHUT ERROR");
}
i=0;
gsm_send_string("AT+CSTT=\"airtelgprs.com\"\r\n");
__delay_ms(1000);
buff[sizeof(buff)-1] = '\0';
if(strstr(buff, "OK")!= NULL)
{
lcd_cmd(0xC0);
lcd_string("CSTT OK");
memset(buff, 0, sizeof(buff));
}
else{lcd_cmd(0xC0);
lcd_string("CSTT ERROR");
}
i=0;
gsm_send_string("AT+CIICR\r");
__delay_ms(5000);
buff[sizeof(buff)-1] = '\0';
if(strstr(buff, "OK")!=NULL)
{
lcd_cmd(0xC0);
lcd_string("CIICR OK");
memset(buff, 0, sizeof(buff));
}
else{lcd_cmd(0xC0);
lcd_string("CIICR ERROR");
}
i=0;
gsm_send_string("AT+CIFSR\r");
__delay_ms(5000);
buff[sizeof(buff)-1] = '\0';
if(strstr(buff, ".")!=NULL)
{
lcd_cmd(0xC0);
lcd_string("CIFSR OK");
memset(buff, 0, sizeof(buff));
}
else{lcd_cmd(0xC0);
lcd_string("CIFSR ERROR\r");
}
i=0;
gsm_send_string("AT+CIPSTART=\"TCP\",\"api.thingspeak.com\",\"80\"\r");
__delay_ms(5000);
buff[sizeof(buff)-1] = '\0';
if(strstr(buff, "CONNECT OK")!=NULL)
{
lcd_cmd(0xC0);
lcd_string("CIPSTART OK");
__delay_ms(500);
conn = true;
memset(buff, 0, sizeof(buff));
}
else{lcd_cmd(0xC0);
lcd_string("CIPSTART ERROR");
conn = false;
}
}
}
void tcp_send()
{
if(conn == true)
{
i=0;
gsm_send_string("AT+CIPSEND\r");
__delay_ms(2000);
buff[sizeof(buff)-1] = '\0';
if(strstr(buff, ">")!= NULL)
{
//gsm_send_string("GET https://api.thingspeak.com/update?api_key=KKNCJ61S64YMHJ9D&field1=");
//gsm_send_string(value);
strcat(data, "GET https://api.thingspeak.com/update?api_key=KKNCJ61S64YMHJ9D&field1=");
strcat(data, value);
gsm_send_string(data);
gsm_send_string("\r\n");
__delay_ms(4000);
if(strstr(buff, "SEND OK")!=NULL)
{
gsm_send_char(0x1A); ////CTRL Z
lcd_cmd(0xC0);
lcd_string("SEND OK");
memset(buff, 0, sizeof(buff));
}
}
}
else{}
}
void adc_init()
{
//ADCON0 = 0x01; /////ADON = 0 ADC channel 0
ADCON1 = 0xE0; ////Right justified, FOSC/64 4us at 16MHz acc to datasheet
}
int adc_read(char ch)
{
//ADCON0 = 0x00;
ADCON0 = (ch<<2);
ADON = 1;
__delay_ms(2);
GO_nDONE = 1;
while(GO_nDONE);
//ADON = 0;
return((ADRESH<<8)+ADRESL);
}
void adc_display()
{
adc_val = adc_read(0);
f = (float)(adc_val*5)/1024;
e = f*10;
value[0] = e/10 + '0'; ////2 of 25
value[1] = '.';
value[2] = e%10 + '0'; ////5 of 25
value[sizeof(value)-1] = '\0';
lcd_cmd(0x80);
lcd_string("VAL : ");
lcd_string(value);
}
It get stuck on "SENDING DATA" in while loop but Analog val is working fine.
I made some changes to the program. Now it is sending data but only once.
I am trying to send continuous data but I have no luck till now.
Below is the code.
#include "mcc_generated_files/mcc.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#define _XTAL_FREQ 16000000
#define RS RB0
#define RW RB1
#define EN RB2
char buff[80], a;
char data[80];
int flag, b, i = 0;
int adc_val;
bool conn = false;
bool server = false;
float f;
int e;
int val = 0;
char value[5];
//char b,c,d;
void server_conn();
void tcp_send();
void adc_display();
int adc_read(char);
void adc_init();
void send_value();
void lcd_cmd(unsigned char cmd)
{
PORTB = (0xF0 & cmd);
RS = 0;
RW = 0;
EN = 1;
__delay_ms(5);
EN = 0;
PORTB = (cmd<<4 & 0xf0);
RS = 0;
RW = 0;
EN = 1;
__delay_ms(5);
EN = 0;
}
void lcd_data(unsigned char data)
{
PORTB = (0xF0 & data);
RS = 1;
RW = 0;
EN = 1;
__delay_ms(5);
EN = 0;
PORTB = (data<<4 & 0xf0);
RS = 1;
RW = 0;
EN = 1;
__delay_ms(5);
EN = 0;
}
void lcd_string(char *str)
{
while(*str != '\0')
{
lcd_data(*str++);
}
}
void lcd_init()
{
lcd_cmd(0x02);///return to home
lcd_cmd(0x28);///4bitmode
lcd_cmd(0x0C);///cursor off
lcd_cmd(0x01);///display clear
lcd_cmd(0x06);///increment cursor
}
void uart_init()
{
SPBRG = 25; ////9600 baud rate at 16MHz FOSC
SYNC = 0;
BRGH = 0;
BRG16 = 0;
CREN = 1;
TXEN = 1;
SPEN = 1;
GIE = 1;
PEIE = 1;
RCIE = 1;
RCIF = 0;
TRISC6 = 0; ////TX pin
TRISC7 = 1; ////RX pin
}
void gsm_send_char(unsigned char data)
{
while(!TXIF);
TXREG = data;
}
char gsm_receive_char()
{
while(!RCIF);
return RCREG;
}
void gsm_send_string(char *p)
{
while(*p != '\0')
{
gsm_send_char(*p);
p++;
}
}
__interrupt() void isr(void)
{
if(RCIF == 1)
{
RCIF = 0;
a =gsm_receive_char();
buff[i] = a;
i++;
}
}
void main(void)
{
SYSTEM_Initialize();
ANSELA = 0x01; ////Select RA0 as analog
TRISA = 0xFF; ////input PORTA for ADC
TRISB = 0x00;
ANSELB = 0x00;
lcd_init();
uart_init();
lcd_cmd(0x01);
server_conn();
__delay_ms(2000);
adc_init();
while (1)
{
lcd_cmd(0xC0);
lcd_string("SENDING DATA");
adc_display();
tcp_send();
__delay_ms(1000);
}
}
void server_conn()
{
if(server == false)
{
i=0;
gsm_send_string("AT\r");
__delay_ms(500);
buff[sizeof(buff)-1] = '\0';
if(strstr(buff, "OK")!= NULL)
{
lcd_cmd(0xC0);
lcd_string("OK");
memset(buff, 0, sizeof(buff));
}
else{lcd_cmd(0xC0);
lcd_string("AT ERROR");
}
i=0;
gsm_send_string("AT+CIPSHUT\r");
__delay_ms(1000);
//buff[sizeof(buff)-1] = '\0';
if(strstr(buff, "OK")!= NULL)
{
lcd_cmd(0xC0);
lcd_string("CIPSHUT OK");
memset(buff, 0, sizeof(buff));
}
else{lcd_cmd(0xC0);
lcd_string("CIPSHUT ERROR");
}
i=0;
gsm_send_string("AT+CSTT=\"airtelgprs.com\"\r\n");
__delay_ms(1000);
// buff[sizeof(buff)-1] = '\0';
if(strstr(buff, "OK")!= NULL)
{
lcd_cmd(0xC0);
lcd_string("CSTT OK");
memset(buff, 0, sizeof(buff));
}
else{lcd_cmd(0xC0);
lcd_string("CSTT ERROR");
}
i=0;
gsm_send_string("AT+CIICR\r");
__delay_ms(5000);
//buff[sizeof(buff)-1] = '\0';
if(strstr(buff, "OK")!=NULL)
{
lcd_cmd(0xC0);
lcd_string("CIICR OK");
memset(buff, 0, sizeof(buff));
}
else{lcd_cmd(0xC0);
lcd_string("CIICR ERROR");
}
i=0;
gsm_send_string("AT+CIFSR\r");
__delay_ms(5000);
//buff[sizeof(buff)-1] = '\0';
if(strstr(buff, ".")!=NULL)
{
lcd_cmd(0xC0);
lcd_string("CIFSR OK");
memset(buff, 0, sizeof(buff));
}
else{lcd_cmd(0xC0);
lcd_string("CIFSR ERROR\r");
}
i=0;
gsm_send_string("AT+CIPSTART=\"TCP\",\"api.thingspeak.com\",\"80\"\r");
__delay_ms(5000);
//buff[sizeof(buff)-1] = '\0';
if(strstr(buff, "CONNECT OK")!=NULL)
{
lcd_cmd(0xC0);
lcd_string("CIPSTART OK");
__delay_ms(500);
conn = true;
memset(buff, 0, sizeof(buff));
}
else{lcd_cmd(0xC0);
lcd_string("CIPSTART ERROR");
conn = false;
}
}
}
void tcp_send()
{
if(conn == true)
{
i=0;
gsm_send_string("AT+CIPSEND\r");
__delay_ms(2000);
//buff[sizeof(buff)-1] = '\0';
if(strstr(buff, ">")!= NULL)
{
//gsm_send_string("GET https://api.thingspeak.com/update?api_key=KKNCJ61S64YMHJ9D&field1=");
//gsm_send_string(value);
strcat(data, "GET https://api.thingspeak.com/update?api_key=KKNCJ61S64YMHJ9D&field1=");
strcat(data, value);
gsm_send_string(data);
gsm_send_string("\r\n");
__delay_ms(4000);
gsm_send_char(0x1A); ////CTRL Z
}
i=0;
// buff[sizeof(buff)-1] = '\0';
if(strstr(buff, "SEND OK")!=NULL)
{
lcd_cmd(0xC0);
lcd_string("SEND OK");
memset(buff, 0, sizeof(buff));
}
}
}
void adc_init()
{
//ADCON0 = 0x01; /////ADON = 0 ADC channel 0
ADCON1 = 0xE0; ////Right justified, FOSC/64 4us at 16MHz acc to datasheet
}
int adc_read(char ch)
{
//ADCON0 = 0x00;
ADCON0 = (ch<<2);
ADON = 1;
__delay_ms(2);
GO_nDONE = 1;
while(GO_nDONE);
//ADON = 0;
return((ADRESH<<8)+ADRESL);
}
void adc_display()
{
adc_val = adc_read(0);
f = (float)(adc_val*5)/1024;
e = f*10;
value[0] = e/10 + '0'; ////2 of 25
value[1] = '.';
value[2] = e%10 + '0'; ////5 of 25
value[sizeof(value)-1] = '\0';
lcd_cmd(0x80);
lcd_string("VAL : ");
lcd_string(value);
}
After sending data once it get stuck on SENDING DATA in while loop but ADC value still changes.
I'm trying to write code in C to fade an off-board LED using PWM and the MSP430f2618. I can get the LED to turn on but it stays at full intensity. I am trying to read in an array of frequency values and fade the LED based on the frequency values.
int main(void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
int array_size = 0, i = 0, delay = 0;
double frequency[50] = {0.0};
array_size = sizeof(frequency);
frequency [0] = 60.0;
for (i = 1; i < array_size; i++)
{
if (frequency[i - 1] < 61)
{
frequency[i] = frequency[i-1] + 0.1;
}
else
{
frequency[i] = 60.0;
}
}
P4OUT &= 0;
P4DIR |= (BIT1 + BIT2); //P4.1 and P4.2 output
P4SEL &= ~(BIT1 + BIT2); //P4.1 and P4.2 TBx options, timer select
TBCCR0 = 512-1;
TBCCTL1 = OUTMOD_7;
TBCCTL2 = OUTMOD_7;
for (i = 0; i < array_size; i++)
{
P4OUT &= 0;
if ((frequency[i] < 60.2) && (frequency[i] >=60.0))
{
//TBCCR1 = 3200;
TBCCR1 = 384;
}
else if ((frequency[i] < 60.4) && (frequency[i] >=60.2))
{
//TBCCR1 = 2560;
TBCCR1 = 256;
}
else if ((frequency[i] < 60.6) && (frequency[i] >=60.4))
{
//TBCCR1 = 1920;
TBCCR1 = 128;
}
else if ((frequency[i] < 60.8) && (frequency[i] >=60.6))
{
//TBCCR1 = 1280;
TBCCR1 = 64;
}
else if ((frequency[i] < 61) && (frequency[i] >=60.8))
{
//TBCCR1 = 640;
TBCCR1 = 32;
}
else
{
TBCCR2 = 512;
}
P4OUT ^= BIT1;
for (delay = 0; delay < 32000; delay++);
}
TBCTL = TBSSEL_2 + MC_1; // ACLK, up mode
__bis_SR_register(LPM0_bits); // Enter LPM3
return 0;
}
The timer is not running until you start it by setting the MC field. That initialization must be done at the beginning.