FSR presses to increase variable on a display - arduino
Update: So I'm getting the error: "expected initializer before 'fsrAnalogPin'" with this code. Can someone help me sort this out?
// testshapes demo for Adafruit RGBmatrixPanel library.
// Demonstrates the drawing abilities of the RGBmatrixPanel library.
// For 32x32 RGB LED matrix:
// http://www.adafruit.com/products/607
// Written by Limor Fried/Ladyada & Phil Burgess/PaintYourDragon
// for Adafruit Industries.
// BSD license, all text above must be included in any redistribution.
#include <Adafruit_GFX.h> // Core graphics library
#include <RGBmatrixPanel.h> // Hardware-specific library
// If your 32x32 matrix has the SINGLE HEADER input,
// use this pinout:
#define CLK 8 // MUST be on PORTB! (Use pin 11 on Mega)
#define OE 9
#define LAT 10
#define A A0
#define B A1
#define C A2
#define D A3
#define PULLUP true //use the AVR's internal pullup resistor
#define INVERT true //low level means fsr pressed
#define DEBOUNCE_TIME 50 //milliseconds
// If your matrix has the DOUBLE HEADER input, use:
//#define CLK 8 // MUST be on PORTB! (Use pin 11 on Mega)
//#define LAT 9
//#define OE 10
//#define A A3
//#define B A2
//#define C A1
//#define D A0
RGBmatrixPanel matrix(A, B, C, D, CLK, LAT, OE, false);
void loop(){
Serial.begin(9600);
matrix.begin();
// draw a pixel in solid white
matrix.drawPixel(0, 0, matrix.Color333(7, 7, 7));
delay(500);
// fix the screen with green
matrix.fillRect(0, 0, 32, 32, matrix.Color333(0, 7, 0));
delay(500);
// draw a box in yellow
matrix.drawRect(0, 0, 32, 32, matrix.Color333(7, 7, 0));
delay(500);
// draw an 'X' in red
matrix.drawLine(0, 0, 31, 31, matrix.Color333(7, 0, 0));
matrix.drawLine(31, 0, 0, 31, matrix.Color333(7, 0, 0));
delay(500);
// draw a blue circle
matrix.drawCircle(10, 10, 10, matrix.Color333(0, 0, 7));
delay(500);
// fill a violet circle
matrix.fillCircle(21, 21, 10, matrix.Color333(7, 0, 7));
delay(500);
// fill the screen with 'black'
matrix.fillScreen(matrix.Color333(0, 0, 0));
// draw some text!
matrix.setCursor(1, 0); // start at top left, with one pixel of spacing
matrix.setTextSize(1); // size 1 == 8 pixels high
matrix.setTextWrap(false); // Don't wrap at end of line - will do ourselves
matrix.setTextColor(matrix.Color333(7,7,7));
matrix.println("FIST");
matrix.println(" BUMP");
// print each letter with a rainbow color
matrix.setTextColor(matrix.Color333(7,0,0));
matrix.print('C');
matrix.setTextColor(matrix.Color333(7,4,0));
matrix.print('O');
matrix.setTextColor(matrix.Color333(7,7,0));
matrix.print('U');
matrix.setTextColor(matrix.Color333(4,7,0));
matrix.print('N');
matrix.setTextColor(matrix.Color333(0,7,0));
matrix.println('T');
{
const int fsr_pin = A4; //connect fsr from this pin to ground
int read fsrAnalogPin = 4;
fsr_pin.read();
if( fsr_pin.wasPressed() ){
counter = counter + 1
matrix.setTextColor( matrix.Color333(0,7,7) );
matrix.print( counter )
}
}
// whew!
}
void loop() {
// do nothing
}
if someone can help me with my code I would be very grateful. What I'm trying to do is: Using a force-sensitive resistor, I want to count a given amount of pressure on the FSR with an arduino, that then displays each tap on an LED matrix. In other words, I have a glove with an FSR on it, and I want to count "fist bumps" that then display on an 32x32 Adafruit LED matrix.
Here's an example of what I want it to look like:
https://drive.google.com/file/d/0B4kq-ADrtz4mSzNWUTJoNGlBSU0/view?usp=sharing
That is just a static number on the bottom though. I need help making the code to connect a counting variable to the FSR "bumps." It's probably pretty simple but I'm entirely new to this so it's a lot to learn. So any help is appreciated
Here is what I have for code:
// testshapes demo for Adafruit RGBmatrixPanel library.
// Demonstrates the drawing abilities of the RGBmatrixPanel library.
// For 32x32 RGB LED matrix:
// http://www.adafruit.com/products/607
// Written by Limor Fried/Ladyada & Phil Burgess/PaintYourDragon
// for Adafruit Industries.
// BSD license, all text above must be included in any redistribution.
#include <Adafruit_GFX.h> // Core graphics library
#include <RGBmatrixPanel.h> // Hardware-specific library
// If your 32x32 matrix has the SINGLE HEADER input,
// use this pinout:
#define CLK 8 // MUST be on PORTB! (Use pin 11 on Mega)
#define OE 9
#define LAT 10
#define A A0
#define B A1
#define C A2
#define D A3
#define FSR_PIN A4 //connect fsr from this pin to ground
#define PULLUP true //use the AVR's internal pullup resistor
#define INVERT true //low level means fsr pressed
#define DEBOUNCE_TIME 50 //milliseconds
// If your matrix has the DOUBLE HEADER input, use:
//#define CLK 8 // MUST be on PORTB! (Use pin 11 on Mega)
//#define LAT 9
//#define OE 10
//#define A A3
//#define B A2
//#define C A1
//#define D A0
RGBmatrixPanel matrix(A, B, C, D, CLK, LAT, OE, false);
void setup() {
Serial.begin(9600);
matrix.begin();
// draw a pixel in solid white
matrix.drawPixel(0, 0, matrix.Color333(7, 7, 7));
delay(500);
// fix the screen with green
matrix.fillRect(0, 0, 32, 32, matrix.Color333(0, 7, 0));
delay(500);
// draw a box in yellow
matrix.drawRect(0, 0, 32, 32, matrix.Color333(7, 7, 0));
delay(500);
// draw an 'X' in red
matrix.drawLine(0, 0, 31, 31, matrix.Color333(7, 0, 0));
matrix.drawLine(31, 0, 0, 31, matrix.Color333(7, 0, 0));
delay(500);
// draw a blue circle
matrix.drawCircle(10, 10, 10, matrix.Color333(0, 0, 7));
delay(500);
// fill a violet circle
matrix.fillCircle(21, 21, 10, matrix.Color333(7, 0, 7));
delay(500);
// fill the screen with 'black'
matrix.fillScreen(matrix.Color333(0, 0, 0));
// draw some text!
matrix.setCursor(1, 0); // start at top left, with one pixel of spacing
matrix.setTextSize(1); // size 1 == 8 pixels high
matrix.setTextWrap(false); // Don't wrap at end of line - will do ourselves
matrix.setTextColor(matrix.Color333(7,7,7));
matrix.println("FIST");
matrix.println(" BUMP");
// print each letter with a rainbow color
matrix.setTextColor(matrix.Color333(7,0,0));
matrix.print('C');
matrix.setTextColor(matrix.Color333(7,4,0));
matrix.print('O');
matrix.setTextColor(matrix.Color333(7,7,0));
matrix.print('U');
matrix.setTextColor(matrix.Color333(4,7,0));
matrix.print('N');
matrix.setTextColor(matrix.Color333(0,7,0));
matrix.println('T');
void loop(void)
{
fsr.read();
if (fsr.wasPressed()) {
Serial.print(++counter, DEC);
Serial.println(" steps");
}
matrix.setTextColor(matrix.Color333(0,7,7));
matrix.print("29291");
// whew!
}
void loop() {
// do nothing
}
Firstly, you need to remove the loop() method from setup().
In the void loop() method, making following changes should get things working.
void loop(){
fsr.read();
if( fsr.wasPressed() ){
counter = counter + 1
matrix.setTextColor( matrix.Color333(0,7,7) );
matrix.print( counter )
}
}
EDIT
// testshapes demo for Adafruit RGBmatrixPanel library.
// Demonstrates the drawing abilities of the RGBmatrixPanel library.
// For 32x32 RGB LED matrix:
// http://www.adafruit.com/products/607
// Written by Limor Fried/Ladyada & Phil Burgess/PaintYourDragon
// for Adafruit Industries.
// BSD license, all text above must be included in any redistribution.
#include <Adafruit_GFX.h> // Core graphics library
#include <RGBmatrixPanel.h> // Hardware-specific library
// If your 32x32 matrix has the SINGLE HEADER input,
// use this pinout:
#define CLK 8 // MUST be on PORTB! (Use pin 11 on Mega)
#define OE 9
#define LAT 10
#define A A0
#define B A1
#define C A2
#define D A3
#define PULLUP true //use the AVR's internal pullup resistor
#define INVERT true //low level means fsr pressed
#define DEBOUNCE_TIME 50 //milliseconds
// If your matrix has the DOUBLE HEADER input, use:
//#define CLK 8 // MUST be on PORTB! (Use pin 11 on Mega)
//#define LAT 9
//#define OE 10
//#define A A3
//#define B A2
//#define C A1
//#define D A0
RGBmatrixPanel matrix(A, B, C, D, CLK, LAT, OE, false);
void setup(){
Serial.begin(9600);
matrix.begin();
// draw a pixel in solid white
matrix.drawPixel(0, 0, matrix.Color333(7, 7, 7));
delay(500);
// fix the screen with green
matrix.fillRect(0, 0, 32, 32, matrix.Color333(0, 7, 0));
delay(500);
// draw a box in yellow
matrix.drawRect(0, 0, 32, 32, matrix.Color333(7, 7, 0));
delay(500);
// draw an 'X' in red
matrix.drawLine(0, 0, 31, 31, matrix.Color333(7, 0, 0));
matrix.drawLine(31, 0, 0, 31, matrix.Color333(7, 0, 0));
delay(500);
// draw a blue circle
matrix.drawCircle(10, 10, 10, matrix.Color333(0, 0, 7));
delay(500);
// fill a violet circle
matrix.fillCircle(21, 21, 10, matrix.Color333(7, 0, 7));
delay(500);
// fill the screen with 'black'
matrix.fillScreen(matrix.Color333(0, 0, 0));
// draw some text!
matrix.setCursor(1, 0); // start at top left, with one pixel of spacing
matrix.setTextSize(1); // size 1 == 8 pixels high
matrix.setTextWrap(false); // Don't wrap at end of line - will do ourselves
matrix.setTextColor(matrix.Color333(7,7,7));
matrix.println("FIST");
matrix.println(" BUMP");
// print each letter with a rainbow color
matrix.setTextColor(matrix.Color333(7,0,0));
matrix.print('C');
matrix.setTextColor(matrix.Color333(7,4,0));
matrix.print('O');
matrix.setTextColor(matrix.Color333(7,7,0));
matrix.print('U');
matrix.setTextColor(matrix.Color333(4,7,0));
matrix.print('N');
matrix.setTextColor(matrix.Color333(0,7,0));
matrix.println('T');
}
void loop(){
const int fsr_pin = A4; //connect fsr from this pin to ground
int read fsrAnalogPin = 4;
fsr_pin.read();
if( fsr_pin.wasPressed() ){
counter = counter + 1
matrix.setTextColor( matrix.Color333(0,7,7) );
matrix.print( counter )
}
}
I will also suggest following proper indentation. That will make your code readable. It would be also great if you revise your Arduino prog. concepts.
Related
Why this adafruit screen code interferes with this code?
I have a code for artificial neural network on Arduino. I'm using an Arduino UNO at 16Mhz and a SSD1306 led screen. The ANN is a simple logic XOR function. The code generates two random numbers 1.0 or 0.0, then passes the values to the inputs of the ANN, then the ANN inference the value and prints the inference time in milliseconds on the led screen, after that, prints on Serial monitor the input values, the output value and the inference time. Complete code is: #include <SPI.h> #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <math.h> #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); volatile unsigned long adcTime = 0; const float NEFTIS_E = 2.71828183; const int NEFTIS_ANN_LENGHT = 13; // Total neurons in the network. const int NEFTIS_OUT_LENGHT = 1; // Total output neurons. const int NEFTIS_INPUT_LENGHT = 2; // Total input neurons. enum ENUM_ActivationFuntionType { AF_Logistic, // Logistic sigmoid. AF_TanH, // Hypervolic tangent. AF_ArcTan, // Arc tangent. AF_ReLU, // Rectified Linear Unit. AF_LeakyReLU, // Leaky ReLU. AF_ELU, // Exponentian Linear Unit. AF_SoftPlus, // AF_Boolean, // Boolean. }; // Synapse structure typedef struct { float weight; int inputNeuron; } struct_synapse; // Neuron structure typedef struct { float value; float bias; int indexStart; int nInputs; bool isInput; ENUM_ActivationFuntionType activationFunction; float linearDelta; } struct_neuron; struct_neuron Neftis_Neurons[] = { {1.0f, 0, 0, 0, true, AF_Logistic, 0.01}, {1.0f, 0, 0, 0, true, AF_Logistic, 0.01}, {0.0f, 0, 0, 2, false, AF_Logistic, 0.01}, {0.0f, 0, 2, 2, false, AF_Logistic, 0.01}, {0.0f, 0, 4, 2, false, AF_Logistic, 0.01}, {0.0f, 0, 6, 2, false, AF_Logistic, 0.01}, {0.0f, 0, 8, 2, false, AF_Logistic, 0.01}, {0.0f, 0, 10, 2, false, AF_Logistic, 0.01}, {0.8f, 0, 12, 2, false, AF_Logistic, 0.01}, {0.0f, 0, 14, 2, false, AF_Logistic, 0.01}, {0.0f, 0, 16, 2, false, AF_Logistic, 0.01}, {0.0f, 0, 18, 2, false, AF_Logistic, 0.01}, {0.0f, 0, 20, 10, false, AF_Logistic, 0.01} }; struct_synapse Neftis_Synapses[] = { {-5.43316f, 0}, {-5.543714f, 1}, {-4.262666f, 0}, {1.139778f, 1}, {2.446421f, 0}, {-6.2367f, 1}, {-2.993009f, 0}, {-0.5112332f, 1}, {-3.330907f, 0}, {0.02449156f, 1}, {-1.701479f, 0}, {-1.936165f, 1}, {0.5805757f, 0}, {1.178081f, 1}, {-3.712192f, 0}, {0.5605027f, 1}, {-4.816404f, 0}, {-4.946669f, 1}, {-3.816507f, 0}, {0.6793132f, 1}, {-9.65515f, 2}, {2.788617f, 3}, {5.523755f, 4}, {0.8867579f, 5}, {1.384886f, 6}, {-0.4491375f, 7}, {-3.600839f, 8}, {1.99833f, 9}, {-6.788064f, 10}, {2.101213f, 11} }; void setup() { // put your setup code here, to run once: Serial.begin(115200);/* display.begin(SSD1306_SWITCHCAPVCC, 0x3C); display.clearDisplay(); display.display();*/ pinMode(LED_BUILTIN, OUTPUT); } void loop() { // put your main code here, to run repeatedly: float inputs[NEFTIS_INPUT_LENGHT]; inputs[0] = (float) random(0, 2); inputs[1] = (float) random(0, 2); Neftis_SetInputs(inputs); unsigned long start = micros(); Neftis_Run(); adcTime = micros() - start; display.clearDisplay(); display.setTextSize(1); // Normal 1:1 pixel scale display.setTextColor(SSD1306_WHITE); // Draw white text display.setCursor(1, 16); // Start at top-left corner display.print(adcTime); Serial.println(Neftis_Neurons[0].value); Serial.println(Neftis_Neurons[1].value); Serial.println(Neftis_Neurons[12].value); display.display(); Serial.println(adcTime); digitalWrite(LED_BUILTIN, true); delay(1000); digitalWrite(LED_BUILTIN, false); delay(500); } void Neftis_SetInputs(float inputs[]) { for(int i = 0; i < NEFTIS_INPUT_LENGHT; i++) { Neftis_Neurons[i].value = inputs[i]; } } void Neftis_Run() { // for every neuron for(int i = 0; i < NEFTIS_ANN_LENGHT; i++) { float sum = 0.0f; for(int j = 0; j < Neftis_Neurons[i].nInputs; j++) { // sums the inputs sum += Neftis_Synapses[Neftis_Neurons[i].indexStart + j].weight * Neftis_Neurons[Neftis_Synapses[Neftis_Neurons[i].indexStart + j].inputNeuron].value; } sum += Neftis_Neurons[i].bias; // apply activation function if is not input neuron if(Neftis_Neurons[i].isInput == false) { // >> Logistic if(Neftis_Neurons[i].activationFunction == AF_Logistic){ Neftis_Neurons[i].value = (float) (1 / (1 + pow(NEFTIS_E, -sum))); // >> TanH. } } } } void Neftis_GetOutputs(float* outArray) { for(int i = 0; i < NEFTIS_OUT_LENGHT; i++) { outArray[i] = Neftis_Neurons[NEFTIS_ANN_LENGHT - NEFTIS_OUT_LENGHT + i].value; } } The problem is, the screen don't show anything and the serial monitor shows strange values for the inputs, output and inference time, as show in image below. But if I remove the lines display.begin(SSD1306_SWITCHCAPVCC, 0x3C); display.clearDisplay(); display.display();*/ and display.clearDisplay(); display.setTextSize(1); // Normal 1:1 pixel scale display.setTextColor(SSD1306_WHITE); // Draw white text display.setCursor(1, 16); // Start at top-left corner then the serial monitor shows the right values for inputs, output and elapsed time for inference. Now, I modified this line of code from this: Neftis_Neurons[i].value = (float) (1 / (1 + pow(NEFTIS_E, -sum))); to this: Neftis_Neurons[i].value = (float) (1 / (1 + pow(NEFTIS_E, -1.5))); and then the screen works and shows the inference time. The question is simple, why the screen code is interfering with pow function and serial monitor? or pow function is interfering with screen and serial monitor code? By the way, I executed the same code on a Raspberry Pi Pico and works ok, no issues there. Thank you very much, I hope you find the answer.
I followed the suggestion of CherryDT and posted my question on the Arduino forum and was answered in [https://forum.arduino.cc/t/why-this-adafruit-screen-code-interferes-with-this-other-code/1049580]this link1 You are running out of dynamic memory on the Uno, which causes unpredictable errors during run time. This line allocates a screen buffer of 1024 bytes, which won't be shown in the memory usage report during compiling/uploading: display.begin(SSD1306_SWITCHCAPVCC, 0x3C); To save dynamic memory space, you can put your other data arrays in program memory using the PROGMEM keyword. Check the Arduino reference for usage. I tried the same code but with a smaller ANN model and it worked.
Optimize code with predefined variables with same prefix
I'm well on my way on learning coding for Arduino. For the following case I Googled my ass off, but can't find any helpful answer. So, asking here. Maybe you can help me with some terminology to Google, or direct links where this is elaborated. I want to compress the following code. The predefined colors have to be as they are. All variables have the same prefix, namely COLOR_. Is there any way to compress the part that is in the setup? See following link. // Define the colors of remote control in RGB values // R G B #define COLOR_0 { 0, 0, 0} #define COLOR_1 {255, 0, 0} // Red #define COLOR_2 {255, 160, 25} // Orange #define COLOR_3 {255, 255, 0} // Yellow #define COLOR_4 {127, 255, 127} // Pink #define COLOR_5 { 0, 255, 0} // Green #define COLOR_6 { 0, 255, 255} // Cyan #define COLOR_7 { 0, 127, 255} // Light blue #define COLOR_8 { 0, 0, 255} // Blue #define COLOR_9 {191, 0, 255} // light green uint16_t RGB_565(uint8_t red, uint8_t green, uint8_t blue){ // 16-bit value to express the RGB565-color: // |R|R|R|R|R|G|G|G|G|G|G|B|B|B|B|B| uint16_t red_565 = red / 8; uint16_t green_565 = green / 4; uint16_t blue_565 = blue / 8; uint16_t mixed_565 = (red_565 << 11) | (green_565 << 5) | blue_565; return mixed_565; } void setup(){ // Convert the preset colors to a RGB565-value const uint8_t tint_1[] = COLOR_1; uint16_t color_1 = RGB_565(tint_1[0], tint_1[1], tint_1[2]); const uint8_t tint_2[] = COLOR_2; uint16_t color_2 = RGB_565(tint_2[0], tint_2[1], tint_2[2]); const uint8_t tint_3[] = COLOR_3; uint16_t color_3 = RGB_565(tint_3[0], tint_3[1], tint_3[2]); const uint8_t tint_4[] = COLOR_4; uint16_t color_4 = RGB_565(tint_4[0], tint_4[1], tint_4[2]); // etc... // This part should be able to be compressed } void loop(){ // Not relevant }
What about #define COLOR_1 255, 0, 0 Then simply call RGB_565(COLOR_1);
Stepper motor does not work when used with ultrasonic sensor and step motor in Arduino
When the ultrasonic sensor determines that there is an object within a certain range, I want to make the step motor move once, but the step motor does not move. The step motor lights up, but I don't know what the problem is. Except for the conditional syntax and ultrasonic sensor, the step motor works well when you run the step motor. The code is as follows: #include <AccelStepper.h> int a; int trigPin4 = A1; int echoPin4 = A2; long duration4, distance4; // Define step constants #define FULLSTEP 4 #define HALFSTEP 8 #define motorPin1 8 #define motorPin2 9 #define motorPin3 10 #define motorPin4 11 #define motorPin5 4 #define motorPin6 5 #define motorPin7 6 #define motorPin8 7 AccelStepper stepper1(FULLSTEP, motorPin1, motorPin3, motorPin2, motorPin4); AccelStepper stepper2(FULLSTEP, motorPin5, motorPin7, motorPin6, motorPin8); void setup() { pinMode(trigPin4, OUTPUT); pinMode(echoPin4, INPUT); Serial.begin(9600); // 1 revolution Motor 1 CW stepper1.setMaxSpeed(1000.0); stepper1.setAcceleration(50.0); stepper1.setSpeed(100); stepper1.moveTo(2048); // 1 revolution Motor 2 CCW stepper2.setMaxSpeed(1000.0); stepper2.setAcceleration(50.0); stepper2.setSpeed(100); stepper2.moveTo(-2048); } void loop() { digitalWrite(trigPin4, LOW); delayMicroseconds(2); digitalWrite(trigPin4, HIGH); delayMicroseconds(10); digitalWrite(trigPin4, LOW); duration4 = pulseIn(echoPin4, HIGH); distance4 = duration4 * 0.034 / 2; if (distance4 >= 500 || distance4 <= 0) { Serial.println("Out of range"); } else { Serial.print("Sensor4 : "); Serial.print(distance4); Serial.println("cm"); a = 1; } if (a == 1) { stepper1.run(); stepper2.run(); } delay(2000); }
first, do not use "delay" function. Second, put a= 0 to the first if statement because in your code, once the distance satisfies the the else condition the value of a will remain 1 forever
Arduino RGB LED Control from 2 pots
I'm trying to write some code that reads a voltage from 2 different pots and converts that to 3 pwm outputs that I can then send to an RGB LED. My idea is to use something like a colour map that is used to plot complex functions, but I'm not sure how to implement that. Any suggestions? #define COLOUR_POT_INPUT 4 #define INTENSITY_POT_INPUT 3 #define LED_RED 9 #define LED_GREEN 10 #define LED_BLUE 11 float colour_angle; float colour_radius; float colour_x_value; float colour_y_value; int red_value; int green_value; int blue_value; const float pi = 3.1415; void setup() { pinMode(COLOUR_POT_INPUT, INPUT); pinMode(INTENSITY_POT_INPUT, INPUT); pinMode(LED_RED, OUTPUT); pinMode(LED_GREEN, OUTPUT); pinMode(LED_BLUE, OUTPUT); } void loop() { colour_angle = analogRead(COLOUR_POT_INPUT); colour_radius = analogRead(INTENSITY_POT_INPUT); colour_angle = map(colour_angle, 0, 1023, 0, 2*pi); colour_radius = map(colour_radius, 0, 1023, 0, 255); colour_x_value = colour_radius*cos(colour_angle); colour_y_value = colour_radius*sin(colour_angle); } //Insert function here that maps colour x and y value to red green blue value
Assuming colour_angle and colour_radius represent hue and saturation respectively, then you can use any HSL/HSV-to-RGB conversion code, with a fixed lightness/value. Picking randomly from google results: HSL to RGB conversion HSV to RGB conversion
Why is TCNT1 not counting up on Atmega328?
I have the following code for the Arduino with Atmega328 and a common 16x2 LCD. The LCD is working, but it is always showing the starting value "333" of the Timer 1 counter TCNT1. Why? I have read the datasheet of the 328 over and over again, but I don't get it. #include <LiquidCrystal.h> const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2; LiquidCrystal lcd(rs, en, d4, d5, d6, d7); const int lcdContrastPin = 6, lcdBackligthPin = 10; void setup() { // tutn on LCD backlight and contrast pinMode(lcdContrastPin, OUTPUT); pinMode(lcdBackligthPin, OUTPUT); // fine-tuning contrast could be done by PWM on lcdContrastPin digitalWrite(lcdContrastPin, LOW); digitalWrite(lcdBackligthPin, HIGH); lcd.begin(16, 2); // configure Timer1 TCCR1A = 0; // no waveform generation TCCR1B = 0x00000010; // frequency divider 8 (i.e. counting with 2 MHz) TCCR1C = 0; TIFR1 = 0x00100000; // clear Input Capture Flag TCNT1 = 333; } void loop() { int currentTimerValue = TCNT1; lcd.setCursor(0, 0); lcd.print("TCNT1="); lcd.print(currentTimerValue); lcd.println(" "); delay(50); }
Stupid me! In a lapse of consciousness I took 0x00000010 as a binary number instead of as a hexadecimal which it is. As a result I set the all clock selection bits to 0 which means the timer stops. After replacing 0x00000010 by 0b00000010 (the true binary number) everything works as expected now: TCCR1B = 0b00000010; // frequency divider 8 (i.e. counting with 2 MHz) TCCR1C = 0; TIFR1 = 0b00100000; // clear Input Capture Flag