I have tried several different methods found around the internet, however none of them seem to work. This code works for cases 0-2 but when it goes into case 3 which is the rainbow chase loop, the pressing of the button does not interrupt the loop and move the counter forward. I'm assuming, as usual, I'm missing something stupid so many thanks in advance.
#define FASTLED_ALLOW_INTERRUPTS 1
#define FASTLED_INTERRUPT_RETRY_COUNT 1
#include <FastLED.h>
#define AnalogIn A0
#define SwIn 2
#define LED_Out 12
#define NUM_LEDS 5
int pushCounterz = 0;
volatile int buttonState; // Set volatile for interrupt DO NOT SHAKE!
int lastButtonState;
CRGB leds[NUM_LEDS];
void setup() {
// put your setup code here, to run once:
FastLED.setMaxRefreshRate(250);
FastLED.addLeds<WS2812, LED_Out, GRB>(leds, NUM_LEDS);
pinMode(SwIn, INPUT);
pinMode(LED_Out, OUTPUT);
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB ( 255, 0, 255 );
}
FastLED.show();
delay(120);
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB ( 0, 0, 0 );
}
FastLED.show();
Serial.begin(115200);
Serial.println(pushCounterz);
lastButtonState = digitalRead(SwIn); // Set the button state to the startup state
attachInterrupt((SwIn-2), button_ISR, CHANGE); // Set SwIn button as an interrupt pin // Change to Low???
}
void loop() {
if (pushCounterz != 3) {
FastLED.show();
}
Serial.println(pushCounterz);
delay(120);
}
void button_ISR () {
buttonState = digitalRead(SwIn);
digitalWrite(13, buttonState);
if (buttonState == LOW && buttonState != lastButtonState) {
if (pushCounterz > 3) {
//Serial.println("Reset to 0: ");
pushCounterz = 0;
} else {
pushCounterz = pushCounterz + 1;
//Serial.println("Incerment");
}
//Serial.println(pushCounterz);
switch (pushCounterz) {
case 0:
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB (255, 0, 0);
}
break;
case 1:
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB ( 0, 255, 0);
}
break;
case 2:
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB ( 0, 0, 255);
}
break;
case 3:
theaterChaseRainbow(1,50);
break;
default:
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB ( 0, 0, 0);
}
break;
}
}
lastButtonState = buttonState;
}
// Theater-style crawling lights with rainbow effect
void theaterChaseRainbow(int cycles, int speed) { // TODO direction, duration
for (int j = 0; j < 256 * cycles; j++) { // cycle all 256 colors in the wheel
for (int q = 0; q < 3; q++) {
for (int i = 0; i < NUM_LEDS; i = i + 3) {
int pos = i + q;
leds[pos] = Wheel( (i + j) % 255); //turn every third pixel on
}
FastLED.show();
delay(speed);
for (int i = 0; i < NUM_LEDS; i = i + 3) {
leds[i + q] = CRGB::Black; //turn every third pixel off
}
}
}
}
CRGB Wheel(byte WheelPos) {
if (WheelPos < 85) {
return CRGB(WheelPos * 3, 255 - WheelPos * 3, 0);
}
else if (WheelPos < 170) {
WheelPos -= 85;
return CRGB(255 - WheelPos * 3, 0, WheelPos * 3);
}
else {
WheelPos -= 170;
return CRGB(0, WheelPos * 3, 255 - WheelPos * 3);
}
}
Your issue is not that the button is not changing the value, but rather your code has no exit point if it does; the button will change the value, but nothing in theaterChaseRainbow tells it to stop.
Simply add a check in the method to return out if the button state changes:
// Theater-style crawling lights with rainbow effect
void theaterChaseRainbow(int cycles, int speed) { // TODO direction, duration
for (int j = 0; j < 256 * cycles; j++) { // cycle all 256 colors in the wheel
for (int q = 0; q < 3; q++) {
for (int i = 0; i < NUM_LEDS; i = i + 3) {
int pos = i + q;
leds[pos] = Wheel( (i + j) % 255); //turn every third pixel on
}
FastLED.show();
if (pushCounterz != 3) return; //ADDED THIS HERE*****
delay(speed);
for (int i = 0; i < NUM_LEDS; i = i + 3) {
leds[i + q] = CRGB::Black; //turn every third pixel off
}
}
}
}
In addition, I would suggest simplifying your ISR to just increment the button and not have it handle the logic of the program as well. That should either be contained in the loop method or called from the loop method. This should make for some cleaner and less confusing code, as the ISR's job is simply to adjust the value of the button counter, and the loops job is to deal with the state that the program is currently in.
Also - you can't allow interrupts on AVR, or rather I should say it does nothing. I should put in a warning message when that's happening - AVR/arduino's ISR handling is so slow that even the clock tick ISR would be enough to disrupt writing out WS2812 data (resulting in FastLED cutting the frame off) so I yanked that code out of the avr WS2812 asm implementation. Most arm and esp platforms that FastLED supports do allow for interrupt handling to occur during in the small window between writing out each led's data - courtesy of their higher clock speeds.
If you're using an ARM or ESP based platform, then feel free to ignore this comment (mostly putting it here so folks who stumble on this question in a good search know what's up).
As a reference, the working code with the ISR cleanup. (mind you there is still some serial debugging code in there as I have more work to do with brightness etc)
#define FASTLED_ALLOW_INTERRUPTS 1
#define FASTLED_INTERRUPT_RETRY_COUNT 1
#include <FastLED.h>
#define AnalogIn A0
#define SwIn 2
#define LED_Out 12
#define NUM_LEDS 5
int pushCounterz = 4; // 4 = off
volatile int buttonState; // Set volatile for interrupt DO NOT SHAKE!
int lastButtonState;
CRGB leds[NUM_LEDS];
void setup() {
// put your setup code here, to run once:
FastLED.setMaxRefreshRate(250);
FastLED.addLeds<WS2812, LED_Out, GRB>(leds, NUM_LEDS);
pinMode(SwIn, INPUT);
pinMode(LED_Out, OUTPUT);
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB ( 255, 0, 255 );
}
FastLED.show();
delay(120);
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB ( 0, 0, 0 );
}
FastLED.show();
Serial.begin(19200);
Serial.println(pushCounterz);
lastButtonState = digitalRead(SwIn); // Set the button state to the startup state
attachInterrupt((SwIn-2), button_ISR, LOW); // Set SwIn button as an interrupt pin // Change to Low???
}
void loop() {
// if (pushCounterz != 3) {
//FastLED.show();
//Serial.println(pushCounterz);
// }
//delay(20);
switch (pushCounterz) {
case 0:
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB (255, 0, 0);
}
FastLED.show();
break;
case 1:
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB ( 0, 255, 0);
}
FastLED.show();
break;
case 2:
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB ( 0, 0, 255);
}
FastLED.show();
break;
case 3:
theaterChaseRainbow(1,50);
break;
default:
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB ( 0, 0, 0);
}
FastLED.show();
break;
}
}
void button_ISR () {
buttonState = digitalRead(SwIn);
//digitalWrite(13, buttonState);
if (buttonState == LOW && buttonState != lastButtonState) {
if (pushCounterz > 3 || pushCounterz < 0) {
Serial.println("Reset to 0: ");
pushCounterz = 0;
} else {
pushCounterz = pushCounterz + 1;
Serial.println("Incerment");
}
Serial.println(pushCounterz);
}
lastButtonState = buttonState;
}
// Theater-style crawling lights with rainbow effect
void theaterChaseRainbow(int cycles, int speed) { // TODO direction, duration
for (int j = 0; j < 256 * cycles; j++) { // cycle all 256 colors in the wheel
for (int q = 0; q < 3; q++) {
for (int i = 0; i < NUM_LEDS; i = i + 3) {
int pos = i + q;
leds[pos] = Wheel( (i + j) % 255); //turn every third pixel on
}
FastLED.show();
if (pushCounterz != 3) return;
delay(speed);
for (int i = 0; i < NUM_LEDS; i = i + 3) {
leds[i + q] = CRGB::Black; //turn every third pixel off
}
}
}
}
CRGB Wheel(byte WheelPos) {
if (WheelPos < 85) {
return CRGB(WheelPos * 3, 255 - WheelPos * 3, 0);
}
else if (WheelPos < 170) {
WheelPos -= 85;
return CRGB(255 - WheelPos * 3, 0, WheelPos * 3);
}
else {
WheelPos -= 170;
return CRGB(0, WheelPos * 3, 255 - WheelPos * 3);
}
}
Related
Beginner in Arduino and ESP-32 needs help.
Hello together,
I’m using the Pololu - VNH5019 Motor Driver Carrier to control a 12v motor with an ESP32.
In the following sketch i can speed up and speed down the ramp with delay();.
I tried to archiv the same result with millis(), but until now i could not make it.
What i am missing in my code.
Thanks in advance.
#define MOTOR_IN1 27
#define MOTOR_IN2 16
#define PWMPIN 14
#define frequency 40000
#define resolutionbit 8
const unsigned long eventInterval = 30;
unsigned long previousTime = 0;
void setup() {
pinMode(MOTOR_IN1, OUTPUT);
pinMode(MOTOR_IN2, OUTPUT);
ledcAttachPin(PWMPIN, 0); // assign the speed control PWM pin to a channel
ledcSetup(0, frequency, resolutionbit);
}
void loop() {
//with_delay();
with_millis();
}
//------------------------------------------
void with_delay() {
// set direction
digitalWrite(MOTOR_IN1, HIGH);
digitalWrite(MOTOR_IN2, LOW);
// ramp speed up
for (int i = 0; i <= 255; i++) {
ledcWrite(0, i);
delay(30);
}
// ramp speed down
for (int i = 255; i >= 0; i--) {
ledcWrite(0, i);
delay(30);
}
}
//-------------------------------------------
void with_millis() {
unsigned long currentTime = millis();
if (currentTime - previousTime >= eventInterval) {
digitalWrite(MOTOR_IN1, HIGH);
digitalWrite(MOTOR_IN2, LOW);
for (int i = 0; i <= 255; i++) {
ledcWrite(0, i);
previousTime = currentTime;
}
}
if (currentTime - previousTime >= eventInterval) {
digitalWrite(MOTOR_IN1, HIGH);
digitalWrite(MOTOR_IN2, LOW);
for (int i = 255; i >= 0; i--) {
ledcWrite(0, i);
previousTime = currentTime;
}
}
}
Your problem is that the program gets stuck in the for loop.
You need to also create direction variable so the program that knows which if statement to execute.
You need to create some other logic that will increase the i variable without stopping the whole program.
The code:
//Initialize the i variable globaly:
int i = 0;
bool direction = 0;
//Your function:
void with_millis() {
unsigned long currentTime = millis();
if ((currentTime - previousTime >= eventInterval) && direction == true) {
digitalWrite(MOTOR_IN1, HIGH);
digitalWrite(MOTOR_IN2, LOW);
i++;
if (i <= 255) {
ledcWrite(0, i);
previousTime = currentTime;
} elif (i > 255) {
i = 0;
direction = false;
}
}
if ((currentTime - previousTime >= eventInterval) && direction == false) {
digitalWrite(MOTOR_IN1, LOW);
digitalWrite(MOTOR_IN2, HIGH);
i++;
if (i <= 255) {
ledcWrite(0, i);
previousTime = currentTime;
} elif (i > 255) {
i = 0;
direction = true;
}
}
}
I'm trying to create a bop it game with 2 players where:
Green LED – Gyro Sensor (Z-axis)
Red LED – Temperature sensor
Blue LED – Sound sensor
Yellow LED – Light Sensor
However when I press the left button to start the game, only the yellow light above GND goes on and doesn't respond to anything, what can be wrong with my code?
long randNumber;
int temp;
int val;
int number;
int score;
boolean state;
int light;
uint8_t pixeln = 0;
int sound;
int shake;
unsigned long lastmillis = 0;
boolean game;
const byte numPins = 7;
byte pins[] = {0, 1, 2, 3, 4, 5, 6, 7};
int player1 = 0;
int player2 = 0;
#include "Adafruit_CircuitPlayground.h"
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
CircuitPlayground.begin();
CircuitPlayground.clearPixels();
//timer();
}
void loop() {
if (digitalRead(4) == HIGH && digitalRead(19) == LOW){
Serial.println("Button Pressed");
lastmillis = millis();
for (int i=0; i < 10; i ){
randNumber = random(4);
//Serial.println("Loop");
switch (randNumber){
//Serial.println("Switch");
case 0:
while (pixeln < 10){
CircuitPlayground.setPixelColor(pixeln , 255,0,0);
CircuitPlayground.strip.show();
}
pixeln = 0;
state = true;
temp = analogRead(A0);
//Serial.println(temp);
while (state == true) {
val = analogRead(A0);
number = val - temp;
if (number > 10){
score ;
state = false;
//Serial.println("Temperature");
delay(500);
}
}
break;
case 1:
while (pixeln < 10){
CircuitPlayground.setPixelColor(pixeln , 127,127,0);
CircuitPlayground.strip.show();
}
pixeln = 0;
state = true;
light = analogRead(A5);
//Serial.println(light);
while (state == true) {
val = analogRead(A5);
number = light - val;
if (number > 20){
score ;
state = false;
//Serial.println(number);
delay(500);
}
}
break;
case 2:
while (pixeln < 10){
CircuitPlayground.setPixelColor(pixeln , 0,0,255);
CircuitPlayground.strip.show();
}
pixeln = 0;
state = true;
sound = analogRead(A4);
//Serial.println(sound);
while (state == true) {
val = analogRead(A4);
number = val - sound;
if (number > 50){
score ;
state = false;
//Serial.println(number);
delay(500);
}
}
break;
case 3:
while (pixeln < 10){
CircuitPlayground.setPixelColor(pixeln , 0,255,0);
CircuitPlayground.strip.show();
}
pixeln = 0;
val = CircuitPlayground.motionZ();
//Serial.println(val);
state = true;
while (state == true) {
shake = CircuitPlayground.motionZ();
number = val - shake;
//Serial.println(number);
if (number > 15 or number < -15){
score ;
state = false;
//Serial.println("shake");
delay(500);
}
}
break;
case 4:
CircuitPlayground.clearPixels();
CircuitPlayground.setPixelColor(0, 0,255,0);
CircuitPlayground.setPixelColor(1, 0,255,0);
CircuitPlayground.strip.show();
delay(3000);
break;
case 5:
CircuitPlayground.clearPixels();
CircuitPlayground.setPixelColor(3, 255,0,0);
CircuitPlayground.setPixelColor(4, 255,0,0);
CircuitPlayground.strip.show();
delay(3000);
break;
case 6:
CircuitPlayground.clearPixels();
CircuitPlayground.setPixelColor(5, 127,0,255);
CircuitPlayground.setPixelColor(6, 127,0,255);
CircuitPlayground.strip.show();
delay(3000);
break;
case 7:
CircuitPlayground.clearPixels();
CircuitPlayground.setPixelColor(8, 255,128,0);
CircuitPlayground.setPixelColor(9, 255,128,0);
CircuitPlayground.strip.show();
delay(3000);
}
}
if (CircuitPlayground.slideSwitch()) {
player1 = (millis() - lastmillis) / 1000;
Serial.println(player1);
CircuitPlayground.clearPixels();
byte num = player1;
for (byte i=0; i < numPins; i ){
byte state = bitRead(num, i);
if (state == 1){
CircuitPlayground.setPixelColor(pins[i], 255,255,255);
CircuitPlayground.strip.show();
}
}
} else {
player2 = (millis() - lastmillis) /1000;
Serial.println(player2);
CircuitPlayground.clearPixels();
byte num = player2;
for (byte i=0; i < numPins; i ){
byte state = bitRead(num, i);
if (state == 1){
CircuitPlayground.setPixelColor(pins[i], 255,255,255);
CircuitPlayground.strip.show();
}
}}
CircuitPlayground.playTone(330, 500);
}
while (digitalRead(19) == HIGH && digitalRead(4) == LOW){
if (player1 < player2){
CircuitPlayground.clearPixels();
delay(250);
for (int i =0; i < 5; i ){
CircuitPlayground.setPixelColor(i, 255,255,255);
CircuitPlayground.strip.show();
}
delay(250);
CircuitPlayground.clearPixels();
}
else if (player1 > player2){
CircuitPlayground.clearPixels();
delay(250);
for (int i =5; i < 10; i ){
CircuitPlayground.setPixelColor(i, 255,255,255);
CircuitPlayground.strip.show();
}
delay(250);
CircuitPlayground.clearPixels();
}
else {
CircuitPlayground.clearPixels();
delay(250);
for (int i =0; i < 10; i ){
CircuitPlayground.setPixelColor(i, 255,255,255);
CircuitPlayground.strip.show();
}
delay(250);
CircuitPlayground.clearPixels();
}
}
if (digitalRead(19) == HIGH && digitalRead(4) == HIGH){
player1 = 0;
player2 = 0;
for (int a =0; a < 10; a ){
CircuitPlayground.clearPixels();
delay(100);
for (int i =0; i < 10; i ){
CircuitPlayground.setPixelColor(i, CircuitPlayground.colorWheel(25 * i));
CircuitPlayground.strip.show();
}
delay(100);
CircuitPlayground.clearPixels();
}
}
}
visual after pressing left button:
I'm using a Vishay, TDCG1050M for the LED display, a MSP430 Launchpad for the microcontroller, and Energia for the sketch. I have 110 ohm resistors for each pin lighting up the LED and two NPN transistors (one per digit displayed).
I thought the code I have was going to work properly, but the display still isn't correct. The LED display has to read 00 - 99 with the two push buttons attached onto the circuit. The first button increments the value by one while the second button decrements by one. The LED displays values 0-9 correctly, but when it's on 2's and 5's it displays "22" and "55" respectively. When trying to increment (using the button) to "10" the LED displays "0" only, not the 1 in the tens place. The LED does display 20-29 correctly (aside from displaying 25, to which it looks like 55). I've also rewired the whole circuit and it's still reading the same values.
I've tried replacing some wires, transistors, and the LED. I've also tried a new breadboard. I tried two different computers, the first a PC and the second a Mac.
#define D1 P2_2 //Display digit 1 control
#define D2 P2_0 //Display digit 2 control
#define segA P2_6 //Display segment A control
#define segB P8_2 //Display segment B control
#define segC P3_1 //Display segment C control
#define segD P8_1 //Display segment D control
#define segE P2_3 //Display segment E control
#define segF P7_4 //Display segment F control
#define segG P3_7 //Display segment G control
#define digit_pause 2
#define button1 P2_4
#define button2 P2_5
#define debounceDelay 10
volatile boolean button1_flag = 0;
volatile long lastDebounceTime = 0;
volatile boolean pressed_event_flag = 0;
volatile boolean released_event_flag = 0;
volatile boolean button2_flag = 0;
volatile boolean pressed_event_flag2 = 0;
volatile boolean released_event_flag2 = 0;
int counter = 0;
int number = 0;
int number2 = 0;
int digit = 0;
int digits[] = {D1, D2};
int segments[] {segA, segB, segC, segD, segE, segF, segG};
boolean digit0[] = {0, 0, 0, 0, 0, 0, 1};
boolean digit1[] = {1, 0, 0, 1, 1, 1, 1};
boolean digit2[] = {0, 0, 1, 0, 0, 1, 0};
boolean digit3[] = {0, 0, 0, 0, 1, 1, 0};
boolean digit4[] = {1, 0, 0, 1, 1, 0, 0};
boolean digit5[] = {0, 1, 0, 0, 1, 0, 0};
boolean digit6[] = {0, 1, 0, 0, 0, 0 ,0};
boolean digit7[] = {0, 0 ,0, 1, 1, 1, 1};
boolean digit8[] = {0, 0, 0, 0, 0, 0, 0};
boolean digit9[] = {0, 0, 0, 0, 1, 0, 0};
void setup()
{
pinMode(button1, INPUT);
attachInterrupt(button1, button1_event, CHANGE);
pinMode(button2, INPUT);
attachInterrupt(button2, button2_event, CHANGE);
pinMode(D1, OUTPUT);
pinMode(D2, OUTPUT);
pinMode(segA, OUTPUT);
pinMode(segB, OUTPUT);
pinMode(segC, OUTPUT);
pinMode(segD, OUTPUT);
pinMode(segE, OUTPUT);
pinMode(segF, OUTPUT);
pinMode(segG, OUTPUT);
for(int sweep = 0; sweep < 2; sweep++) {
digitalWrite(digits[sweep],LOW);
}
Serial.begin(9600);
}
void loop()
{
if(pressed_event_flag) {
if (counter <= 99){
counter++;
}
if(pressed_event_flag) {
if (counter == 100)
counter=99;
}
pressed_event_flag = 0;
}
if(released_event_flag) {
released_event_flag = 0;
}
if(pressed_event_flag2) {
if (counter >= 0){
counter--;
}
pressed_event_flag2 = 0;
}
if(released_event_flag2) {
released_event_flag2 = 0;
}
//the problem could be within this part of the code
delay(2);
number = counter;
digit = number % 10;
digitalWrite(digits[1], HIGH);
digitalWrite(digits[0], LOW);
displayDigit(digit);
delay(5);
number2 = (number - digit) / 10;
digitalWrite(digits[0], HIGH);
digitalWrite(digits[1], LOW);
displayDigit(number2);
number2 = (number - digit) / 10;
delay(5);
}
void displayDigit(int numeral)
{
switch (numeral) {
case 0:
for(int i = 0; i < 8; i++) {
digitalWrite(segments[i],digit0[i]);
}
break;
case 1:
for(int i = 0; i < 8; i++) {
digitalWrite(segments[i],digit1[i]);
}
break;
case 2:
for(int i = 0; i < 8; i++) {
digitalWrite(segments[i],digit2[i]);
}
break;
case 3:
for(int i = 0; i < 8; i++) {
digitalWrite(segments[i],digit3[i]);
}
break;
case 4:
for(int i = 0; i < 8; i++) {
digitalWrite(segments[i],digit4[i]);
}
break;
case 5:
for(int i = 0; i < 8; i++) {
digitalWrite(segments[i],digit5[i]);
}
break;
case 6:
for(int i = 0; i < 8; i++) {
digitalWrite(segments[i],digit6[i]);
}
break;
case 7:
for(int i = 0; i < 8; i++) {
digitalWrite(segments[i],digit7[i]);
}
break;
case 8:
for(int i = 0; i < 8; i++) {
digitalWrite(segments[i],digit8[i]);
}
break;
case 9:
for(int i = 0; i < 8; i++) {
digitalWrite(segments[i],digit9[i]);
}
break;
default:
break;
}
}
void button1_event()
{
boolean state = digitalRead(button1); //Determine if pressed or released.
if(state) {
if(!button1_flag) {
if((millis() - lastDebounceTime) >debounceDelay) {
button1_flag = 1;
lastDebounceTime = millis();
pressed_event_flag = 1;
}
}
}
else
{
if(button1_flag) {
if((millis() - lastDebounceTime) >debounceDelay) {
button1_flag = 0;
lastDebounceTime = millis();
released_event_flag = 1;
}
}
}
}
void button2_event()
{
boolean state = digitalRead(button2); //Determine if pressed or released.
if(state) {
if(!button2_flag) {
if((millis() - lastDebounceTime) >debounceDelay) {
button2_flag = 1;
lastDebounceTime = millis();
pressed_event_flag2 = 1;
}
}
}
else
{
if(button2_flag) {
if((millis() - lastDebounceTime) >debounceDelay) {
button2_flag = 0;
lastDebounceTime = millis();
released_event_flag2 = 1;
}
}
}
}
I'm using four fsr pressure sensors in parallel connection to each other. Below are the coding for my project.
I have a problem with the counter values where it display four added counter values for each of the sensor.
I need to get a counter values where it is added each time pressure is detected and if there is no pressure anymore, it will remain the previous counter value.
int fsrPin[] = {0, 1, 2, 3};
int fsrReading;
int fsrVoltage;
unsigned long fsrResistance;
unsigned long fsrConductance;
long fsrForce;
int pinCount = 4;
int counter = 0;
void setup(void) {
for(int thisPin = 0; thisPin < pinCount; thisPin++) {
pinMode(fsrPin[thisPin], OUTPUT);
}
Serial.begin(9600);
}
void loop(void) {
for(int thisPin = 0; thisPin < pinCount; thisPin++) {
fsrReading = analogRead(fsrPin[thisPin]);
Serial.print("Analog reading ");
Serial.print(fsrPin[thisPin]);
Serial.print("=> ");
Serial.println(fsrReading);
fsrVoltage = map(fsrReading, 0, 1023, 0, 5000);
Serial.print("Voltage reading in mV = ");
Serial.println(fsrVoltage);
if (fsrVoltage == 0) {
Serial.println("No pressure");
} else {
// The voltage = Vcc * R / (R + FSR) where R = 10K and Vcc = 5V
// so FSR = ((Vcc - V) * R) / V
fsrResistance = 5000 - fsrVoltage;
fsrResistance *= 10000;
fsrResistance /= fsrVoltage;
Serial.print("FSR resistance in ohms = ");
Serial.println(fsrResistance);
fsrConductance = 1000000;
fsrConductance /= fsrResistance;
Serial.print("Conductance in microMhos: ");
Serial.println(fsrConductance);
if (fsrConductance <= 1000) {
fsrForce = fsrConductance / 80;
Serial.print("Force in Newtons: ");
Serial.println(fsrForce);
} else {
fsrForce = fsrConductance - 1000;
fsrForce /= 30;
Serial.print("Force in Newtons: ");
Serial.println(fsrForce);
}
}
for(int thisPin=0; thisPin < pinCount; thisPin++){
if (fsrForce != 0) {
counter++;
Serial.print("Counter = ");
Serial.println(counter);
} else {
counter;
Serial.print("Counter = ");
Serial.println(counter);
}
}
Serial.println("--------------------");
}
delay(3000);
}
The actual problem is as below where I need to find a correct condition for looping the value of the counter:
for(int thisPin=0; thisPin < pinCount; thisPin++){
if (fsrForce != 0) {
counter++;
Serial.print("Counter = ");
Serial.println(counter);
} else {
counter;
Serial.print("Counter = ");
Serial.println(counter);
}
}
FOUR COUNTER VALUES IN EACH ANALOG READING
I believe the main problem is that you are checking if fsrForce != 0, However fsrForce does not really have a chance to get to zero because it's dependent on fsrVoltage and is set only if fsrVoltage is positive.
Just set fsrForce = 0 when fsrVoltage < VOLTAGE_THRESHOLD and you should get the expected behavior.
Try this:
int fsrPin[] = {
0,
1,
2,
3
};
int fsrReading;
int fsrVoltage;
unsigned long fsrResistance;
unsigned long fsrConductance;
long fsrForce;
int pinCount = 4;
int counter = 0;
// add threshold
const float VOLTAGE_THRESHOLD = 0.2;
void setup(void) {
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
pinMode(fsrPin[thisPin], OUTPUT);
}
Serial.begin(9600);
}
void loop(void) {
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
fsrReading = analogRead(fsrPin[thisPin]);
Serial.print("Analog reading ");
Serial.print(fsrPin[thisPin]);
Serial.print("=> ");
Serial.println(fsrReading);
fsrVoltage = map(fsrReading, 0, 1023, 0, 5000);
Serial.print("Voltage reading in mV = ");
Serial.println(fsrVoltage);
if (fsrVoltage < VOLTAGE_THRESHOLD) {
// reset the fsrForce when there is no pressure
fsrForce = 0
Serial.println("No pressure");
} else {
// The voltage = Vcc * R / (R + FSR) where R = 10K and Vcc = 5V
// so FSR = ((Vcc - V) * R) / V
fsrResistance = 5000 - fsrVoltage;
fsrResistance *= 10000;
fsrResistance /= fsrVoltage;
Serial.print("FSR resistance in ohms = ");
Serial.println(fsrResistance);
fsrConductance = 1000000;
fsrConductance /= fsrResistance;
Serial.print("Conductance in microMhos: ");
Serial.println(fsrConductance);
if (fsrConductance <= 1000) {
fsrForce = fsrConductance / 80;
Serial.print("Force in Newtons: ");
Serial.println(fsrForce);
} else {
fsrForce = fsrConductance - 1000;
fsrForce /= 30;
Serial.print("Force in Newtons: ");
Serial.println(fsrForce);
}
}
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
if (fsrForce != 0) {
counter++;
}
Serial.print("Counter = ");
Serial.println(counter);
}
Serial.println("--------------------");
}
delay(3000);
}
sketch_Show_The_Hat.ino:77:5: error: expected declaration before ‘}’ token
Can some please help figure where this error is coming from?
Here is my code:
#include "LPD8806.h"
#include "SPI.h"
//#define HOLIDAY_MODE
#define MOMENTARY_SWITCH
// Example to control LPD8806-based RGB LED Modules in a strip!
int dataPin = 2;
int clockPin = 3;
int onOffPin = 4;
// Set the first variable to the NUMBER of pixels. 32 = 32 pixels in a row
// The LED strips are 32 LEDs per meter but you can extend/cut the strip
LPD8806 strip = LPD8806(32, dataPin, clockPin);
void setup() {
// Start up the LED strip
strip.begin();
// Update the strip, to start they are all 'off'
strip.show();
randomSeed(analogRead(A0));
// On/Off switch
pinMode(onOffPin, INPUT_PULLUP);
}
int x = 0;
int dim = 0;
#ifdef MOMENTARY_SWITCH
int tdim = 0;
#endif
void loop();
int r, g, b, y, z;
#ifdef MOMENTARY_SWITCH
} if (digitalRead(onOffPin) == LOW) {
if ((dim == 0) && (tdim == 0)
tdim = !0;
} else if ((dim == 128) && (tdim != 0))
tdim = 0;
if (tdim) {
if (dim < 128) {
dim++;
} else {
}
if (dim > 0) {
dim--;
}
} if (digitalRead(onOffPin) == LOW) {
}
}
} if (dim < 128) {
dim++;
} else {
if (dim > 0) {
dim--;
}
}
#endif
for (int i = 0; i < strip.numPixels(); i++) {
y = (x + (i << 5)) & 511;
#ifdef HOLIDAY_MODE
// Holiday
if (y >= 256) {
y = 511 - y;
}
z = 255 - y;
r = (y <= 128) ? (y - 128) << 1 : 0;
g = (z <= 128) ? (z - 128) << 1 : 0;
b = 0;
# else
// Normal
if (y < 128) {
r = y;
g = 0;
b = 255 - y;
} else if (y < 256) {
r = 127;
g = y - 128;
b = 0;
} else if (y < 384) {
r = 127;
g = 127 - (y - 256);
b = 0;
} else {
r = 127 - (y - 384);
g = 0;
b = y - 384;
}
#endif
if (dim == 0) {
r = 0;
g = 0;
b = 0;
} else if (dim < 128) {
r = (r * dim) / 128;
g = (g * dim) / 128;
b = (b * dim) / 128;
}
strip.setPixelColor(i, r & 127, g & 127, b & 127);
}
strip.show();
x += 2;
x &= 511;
delay(5);
// for(int j=0; j < random(100,200); j++ ) {
// int i = random(0,strip.numPixels()-2) + 1;
// strip.setPixelColor(i-1, 43,54,219);
// strip.setPixelColor(i, 255,255,0);
// strip.setPixelColor(i+1, 255,69,0);
// strip.setPixelColor(i-1, 0,0,255);
// strip.setPixelColor(i, 255,0,0);
// strip.setPixelColor(i+1, 0,255,0);
// strip.show();
// delay(5);
// strip.setPixelColor(i-1, 255,128,0);
// strip.setPixelColor(i-4, 255,128,0);
// strip.setPixelColor(i+1, 4,0,8);
// strip.setPixelColor(i-4, 104,10,80);
// strip.setPixelColor(i-10, 43,54,219);
// strip.setPixelColor(i-30, 255,165,0);
// strip.setPixelColor(i+5, 219,125,45);
// strip.setPixelColor(i, 100,100,127);
// strip.setPixelColor(i+1, 104,100,198);
// strip.show();
// delay(1);
// strip.setPixelColor(i+32, 219,112,147);
//
// strip.setPixelColor(i+1, 255,69,0);
// strip.setPixelColor(i-2, 255,127,75);
// strip.setPixelColor(i-6, 255,69,0);
// strip.setPixelColor(i+5, 255,128,0);
// strip.setPixelColor(i+2, 10,227,100);
// strip.show();
// delay(0);
// strip.setPixelColor(i-1, 255,69,0);
// strip.setPixelColor(i, 255,69,0);
// strip.setPixelColor(i-6, 255,69,0);
// strip.setPixelColor(i-6, 255,69,0);
// strip.setPixelColor(i-6, 255,69,0);
// strip.setPixelColor(i-12, 148,0,211);
// strip.setPixelColor(i-6, 255,69,0);
// strip.setPixelColor(i-3, 148,0,211);
// strip.setPixelColor(i-1, 148,0,211);
// strip.setPixelColor(i-6, 255,69,0);
// strip.setPixelColor(i+2, 148,0,211);
// strip.show();
// delay(random(10,100));
// }
// for(int j=0; j < random(10,15); j++ ) {
// for (int i=32; i < strip.numPixels(); i++) {
// strip.setPixelColor(i, 0,0,255);
// }
// strip.show();
// delay(15);
// for (int i=2; i < strip.numPixels(); i++) {
// strip.setPixelColor(i, 255,110,0);
// }
// strip.show();
// delay(15);
// }
First of all I have corrected your formatting with an edit. The error is saying that in 77. line 5. character there is a mistake related with } character. The mistake is you did not declared any { before }.
When I tried to correct your formatting (on my version of your file) 77. line is an empty line. But the problem is probably in 76. line } if (digitalRead(onOffPin) == LOW) { so try to remove { character. I mean try to replace the line with if (digitalRead(onOffPin) == LOW) { and let's see the error persists or not.
Curly Braces: https://www.arduino.cc/en/reference/braces
Reminding: Please always pay attention to your formatting on StackOverflow while asking a question. Formatting the text especially in the question is the key to properly interchange the knowledge. While sharing one of your code file in here, formatting is a necessity. Because if you did not format your text (your codes actually) we can not test them.