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:
Related
I wrote a code designed to automatically turn on and off a generator based on whether the battery is full or empty.
There are some bugs in lcd.begin() and lcd.clear() (They both don't work).
error : invalid use of non-static member function.
Thank you!
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
bool Settings = false;
unsigned long time1;
#define RELAY_PORT 10
float Voltage = 0.00;
int VoltOn = 47;
int VoltOff = 56;
int upbutton = 1;
int downbutton = 2;
int okbutton = 3;
int backbutton = 4;
void setup() {
pinMode(upbutton, INPUT);
pinMode(downbutton, INPUT);
pinMode(okbutton, INPUT);
pinMode(backbutton, INPUT);
pinMode(RELAY_PORT, OUTPUT);
// initialize the LCD
lcd.begin();
// Turn on the blacklight and print a message.
lcd.backlight();
lcd.setCursor(0, 1);
lcd.print ("generator: off");
}
void loop() {
int analog_value = analogRead(A0);
Voltage = ((analog_value * 5.0) / 1020) * 12;
lcd.setCursor(0, 0);
lcd.print("Volt: ");
lcd.print(Voltage);
lcd.print("V");
if (Voltage <= VoltOn && digitalRead(RELAY_PORT) == LOW)
{
digitalWrite(RELAY_PORT, HIGH);
lcd.setCursor(0, 1);
lcd.print ("generator: on");
}
if (Voltage >= VoltOff && digitalRead(RELAY_PORT) == HIGH)
{
digitalWrite(RELAY_PORT, LOW);
lcd.setCursor(0, 1);
lcd.print ("generator: off");
}
if ((digitalRead(upbutton) == HIGH && digitalRead(downbutton) == HIGH && digitalRead(okbutton) == HIGH && digitalRead(backbutton) == HIGH) || (Settings = true))
{
lcd.clear;
lcd.print("Settings:");
delay(2000);
time1 = millis();
bool Setting = false;
while (digitalRead(upbutton) == LOW && digitalRead(downbutton) == LOW && digitalRead(okbutton) == LOW && digitalRead(backbutton) == LOW)
{
if (Setting == false)
{
lcd.clear;
lcd.scrollDisplayLeft();
lcd.print("press Up to set turn on");
lcd.setCursor(0, 1);
lcd.print("press down to set turn off");
Setting = true;
}
if (millis() > time1 + 60000)
{
loop();
}
}
time1 = millis();
while (millis() > time1 + 60000)
{
if (upbutton == HIGH)
{
time1 = millis();
//lcd.clear;
lcd.scrollDisplayLeft();
lcd.print("press Up/ Down to up/ Down Voltage turn on");
while (digitalRead(upbutton) == LOW && digitalRead(downbutton) == LOW && digitalRead(okbutton) == LOW && digitalRead(backbutton) == LOW)
{
if (millis() > time1 + 60000)
{
loop();
}
}
time1 = millis();
while (millis() > time1 - 60000)
{
if (upbutton == HIGH)
{
int xdelay = 1000;
time1 = millis();
(VoltOn) = (VoltOn) + 1;
lcd.setCursor(0, 1);
lcd.print("Voltage-on: ");
lcd.print(VoltOn);
lcd.print("V");
delay(xdelay);
xdelay = xdelay / 1.5;
}
if (downbutton == HIGH)
{
int xdelay = 1000;
time1 = millis();
(VoltOn) = (VoltOn) - 1;
lcd.setCursor(0, 1);
lcd.print("Voltage-on: ");
lcd.print(VoltOn);
lcd.print("V");
delay(xdelay);
xdelay = xdelay / 1.5;
}
if (okbutton == HIGH)
{
loop();
}
if (backbutton == HIGH)
{ Settings = true;
}
if (millis() > time1 + 60000)
{
loop();
}
}
}
if (downbutton == HIGH)
{
time1 = millis();
lcd.clear;
lcd.scrollDisplayLeft();
lcd.print("press Up/ Down to up/ Down Voltage turn off");
while (digitalRead(upbutton) == LOW && digitalRead(downbutton) == LOW && digitalRead(okbutton) == LOW && digitalRead(backbutton) == LOW)
{
if (millis() > time1 + 60000)
{
loop();
}
}
time1 = millis();
while (millis() > time1 - 60000)
{
if (upbutton == HIGH)
{
int xdelay = 1000;
time1 = millis();
(VoltOff) = (VoltOff) + 1;
lcd.setCursor(0, 1);
lcd.print("Voltage-off: ");
lcd.print(VoltOff);
lcd.print("V");
delay(xdelay);
xdelay = xdelay / 1.5;
}
if (downbutton == HIGH)
{
int xdelay = 1000;
time1 = millis();
(VoltOff) = (VoltOff) - 1;
lcd.setCursor(0, 1);
lcd.print("Voltage-off: ");
lcd.print(VoltOff);
lcd.print("V");
delay(xdelay);
xdelay = xdelay / 1.5;
}
if (okbutton == HIGH)
{
loop();;
}
if (backbutton == HIGH)
{
Settings = true;
}
if (millis() > time1 + 60000)
{
loop();
}
}
}
if (backbutton == HIGH)
{
loop();
}
if (okbutton == HIGH)
{
loop();
}
}
}
}
I think you shouldn't ask on stackoverflow for someone just to do your work. People don't like unspecific questions here, that show little to no effort of own research..
That said, one thing that stands out is, that all your lcd.clear are missing brackets, they should be lcd.clear();
Try to split up your code in smaller sections and test individual components, if something doesn't work.
1.How to make this code (at the beginning of the loop) only run every half a second without rivet the rest of the code?
lcd.setCursor(0, 0);
lcd.print("Volt: ");
lcd.print(Voltage);
lcd.print("V");
2.The LCD displays (that the current coming to Arduino from the accumulator is lower than its starting power) "generator: onf" and not "generator: off":
lcd.print ("generator: off");
if (Voltage <= VoltOn && digitalRead(RELAY_PORT) == LOW)
{
digitalWrite(RELAY_PORT, HIGH);
lcd.setCursor(0, 1);
lcd.print ("generator: on");
}
if (Voltage >= VoltOff && digitalRead(RELAY_PORT) == HIGH)
{
digitalWrite(RELAY_PORT, LOW);
lcd.setCursor(0, 1);
lcd.print ("generator: off");
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);
}
}
I'm currently doing a project on an Arduino Uno. The project is based on receiving an IR Signal from an IR Remote and then based on the signal received, perform other operations.
The problem is that the signal gets reset every time. I want to store the value received from the IR Remote and then resets it if detects another pulse.
Here is my code :
int brojac = 0;
int pinData = 10;
unsigned long lengthHeader;
unsigned long bit;
int byteValue;
int vrime = 1000 ;
int storeValue = 0;
void setup()
{
Serial.begin(9600);
pinMode(pinData, INPUT);
}
void loop() {
lengthHeader = pulseIn(pinData, LOW);
if (lengthHeader > 1500)
{
for (int i = 1; i <= 32; i++) {
bit = pulseIn(pinData, HIGH);
if (i > 16 && i <= 24)
if (bit > 1000)
byteValue = byteValue + (1 << (i - 17));
}
}
Serial.print("byteValue = ");
Serial.println(byteValue);
if(byteValue == 66){
digitalWrite(11,HIGH);
}
else{
digitalWrite(11,LOW);
}
delay(vrime);
byteValue = 0;
delay(250);
}
I got the answer by storing the value in a variable until a new variable is detected.
int pinData = 10;
int led = 11;
unsigned long lengthHeader;
unsigned long bit;
int byteValue;
int storeValue = 0;
int previousValue = 0;
void setup()
{
Serial.begin(9600);
pinMode(pinData, INPUT);
pinMode(led, LOW);
}
void loop() {
lengthHeader = pulseIn(pinData, LOW);
if (lengthHeader > 1500)
{
for (int i = 1; i <= 32; i++) {
bit = pulseIn(pinData, HIGH);
if (i > 16 && i <= 24)
if (bit > 1000)
byteValue = byteValue + (1 << (i - 17));
}
}
Serial.print("byteValue = ");
Serial.println(byteValue);
**storeValue = byteValue;
if (storeValue != 0){
previousValue = storeValue;
}
Serial.print("Previous value = ");
Serial.println(previousValue);**
byteValue = 0;
delay(500);
}
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);
}
I'm using an Arduino Uno R3 connected to a 16x2 LCD through I2C. Also using the LiquidCrystal_I2C library to print to the LCD. I recently changed a bunch of my code and this new version of my code makes my Arduino reset as soon as it hits an lcd.print() statement outside void setup(). The code is a work in progress so please bear with any spaghetti code. Here it is:
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <Wire.h>
#include "Time.h"
#include "WiFly.h" //include the WiFly experimental library
#include "Credentials.h"
#define I2C_ADDR 0x27 // Define I2C Address where the PCF8574A is
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
char msg[320];
char * ptr = &msg[0];
int letterCount = 0;
int countdownFlag = 0;
int cstopFlag = 0;
int lpcount = 0;
void(* resetFunc)(void) = 0; //pointer to reset function # address 0
long days, hrs, mins, sec, rem1, rem2;
long gametime, timediff, new_timediff, current_time;
int flag1 = 0;
int flag2 = 0;
int flag3 = 0;
int flag4 = 0;
int flag5 = 0;
int flag6 = 0;
int flag7 = 0;
char hmTeam[25];
char awTeam[25];
char dateTime[25];
WiFlyClient client("api.football-data.org", 80);
void setup(){
//Begin LCD
lcd.begin (16,2);
// Switch on the backlight
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
lcd.home (); // go home
lcd.print("Loading...");
//Begin WiFly and Serial
WiFly.begin();
Serial.begin(9600);
Serial.println("Serial begun :D");
current_time = WiFly.getTime();
Serial.print("connecting to server...");
if(client.connect()){
Serial.println("connected");
client.print("GET http://api.football-data.org/teams/61/fixtures/?timeFrame=n20");
Serial.print("GET http://api.football-data.org/teams/61/fixtures/?timeFrame=n20");
client.println(" HTTP/1.1");
Serial.println(" HTTP/1.1");
client.println("Host: api.football-data.org");
Serial.println("Host: api.football-data.org");
client.println("X-Auth-Token: 4f02cc524412487989ee61aed27503d5");
Serial.println("X-Auth-Token: 4f02cc524412487989ee61aed27503d5");
client.println("Connection: close");
Serial.println("Connection: close");
client.println();
} else{
Serial.println("connection failed");
}
}
void loop(){
if (client.available()) {
char c = client.read();
Serial.print(c);
if(flag4 == 1 && letterCount < 321){
recordMessage(c);
flag5 = 1;
}
if(c == '\n' && !client.available() && flag5 == 1){
char * ptr2; //declare a second pointer to change reference from start of msg string to start of "homeTeam" - i.e. "h"
int count = 0; //this is to count for hmTeam character array
int i;
for(i = 0; i < 321; i++){
if(*(ptr+i) == 'h' && *(ptr+i+4) == 'T' && *(ptr+i+7) == 'm'){ //once the sequence "h T m" is found, we know we have reached the word "homeTeam" in the string
ptr2 = ptr+i+11;
while(*ptr2 != '"'){
hmTeam[count] = *ptr2;
ptr2++;
count++;
}
}
count = 0;
if(*(ptr+i) == 'a' && *(ptr+i+3) == 'y' && *(ptr+i+4) == 'T' && *(ptr+i+7) == 'm'){
ptr2 = ptr+i+11;
while(*ptr2 != '"'){
awTeam[count] = *ptr2;
ptr2++;
count++;
}
}
count = 0;
if(*(ptr+i) == 'd' && *(ptr+i+2) == 't' && *(ptr+i+3) == 'e'){
ptr2 = ptr+i+7;
while(*ptr2 != '"'){
dateTime[count] = *ptr2;
ptr2++;
count++;
}
}
}
}
if(c == '{'){
flag1 = 1;
}
if(flag1 == 1){
if(c == '"'){
flag2 = 1;
}
}
if(flag2 == 1){
if(c == '_'){
flag3 = 1;
}
}
if(flag3 == 1){
if(c == 'l'){
flag4 = 1;
}
}
}
if(!client.connected() && cstopFlag == 0){
delay(100);
client.flush();
client.stop();
Serial.println();
Serial.println("Disconnected.");
delay(500);
Serial.println();
cstopFlag = 1;
flag7 = 1;
}
if(flag7 == 1){
checkAction();
countdownFlag = 1;
}
if(countdownFlag == 1){
//lcd.setCursor(0,0);
//lcd.setBacklight(LOW);
//teamNamePrint(hmTeam);
//lcd.print(" vs ");
//teamNamePrint(awTeam);
countdown();
}
}
void recordMessage(char message){
msg[letterCount] = message;
letterCount++;
}
/*
void teamNamePrint(){
char team[25];
strcpy(team,hmTeam);
if(team == "Arsenal FC")
lcd.print("Arsenal");
else if (team == "Aston Villa FC")
lcd.print("AVFC");
else if (team =="Burnley FC")
lcd.print("Burnley");
else if (team =="Chelsea FC")
lcd.print("Chelsea");
else if (team =="Crystal Palace FC")
lcd.print("CPFC");
else if (team =="Everton FC")
lcd.print("Everton");
else if (team =="Hull City")
lcd.print("Hull");
else if (team =="Leiceter City FC")
lcd.print("Lcity");
else if (team =="Liverpool FC")
lcd.print("Lpool");
else if (team =="Manchester City FC")
lcd.print("ManCity");
else if (team =="Manchester United")
lcd.print("ManU");
else if (team =="Newcastle FC")
lcd.print("NCFC");
else if (team =="Queens Park Rangers")
lcd.print("QPR");
else if (team =="FC Southampton")
lcd.print("Sthtn");
else if (team =="Stoke FC")
lcd.print("Stoke");
else if (team =="Sunderland FC")
lcd.print("Sndlnd");
else if (team =="Swansea FC")
lcd.print("SwnFC");
else if (team =="Tottenham Hotspur")
lcd.print("Spurs");
else if (team =="West Bromwich Albion")
lcd.print("WBrom");
else if (team =="West Ham Utd")
lcd.print("WHU");
}
*/
void checkAction(){
if(lpcount < 60){
gametime_calc();
timediff = gametime - current_time;
days = timediff / 86400;
rem1 = timediff % 86400;
hrs = rem1 / 3600;
rem2 = rem1 % 3600;
mins = rem2 / 60;
sec = rem2 % 60;
} else if(lpcount == 60){
lpcount = 0;
current_time = WiFly.getTime();
new_timediff = gametime - current_time;
if((new_timediff - timediff) != 0){
lcd.setBacklight(HIGH);
lcd.clear();
lcd.print("Resetting...");
delay(1500);
days = new_timediff / 86400;
rem1 = new_timediff % 86400;
hrs = rem1 / 3600;
rem2 = rem1 % 3600;
mins = rem2 / 60;
sec = rem2 % 60;
lcd.clear();
}
}
}
void countdown(){
lcd.setCursor(0,1);
lcd_print(days,0); //print days
lcd_print(hrs,3); //print hours
lcd_print(mins,6); //print minutes
lcd_print(sec,9); //print seconds
if(sec == 0 && mins == 0 && hrs == 0 && days == 0){
lcd.setBacklight(HIGH);
lcd.setCursor(0,0);
lcd.print("Game begins now!");
lcd.setCursor(0,1);
lcd.print(" ");
delay(30000);
resetFunc(); //call reset
} else if(sec == 0 && mins != 0){
mins--;
lpcount++;
sec = 60;
} else if(sec == 0 && mins == 0 && hrs != 0){
hrs--;
mins = 59;
sec = 60;
} else if(sec == 0 && mins == 0 && hrs == 0 && days != 0){
days--;
hrs = 23;
mins = 59;
sec = 60;
}
if(lpcount == 60){
checkAction();
lcd.setCursor(0,1);
lcd_print(days,0); //print days
lcd_print(hrs,3); //print hours
lcd_print(mins,6); //print minutes
lcd_print(sec,9); //print seconds
}
delay(1000);
sec--;
}
void lcd_print(long period, int col){
if(period < 10){
lcd.setCursor(col,1);
lcd.print("0");
lcd.setCursor(col+1,1);
lcd.print(period);
} else{
lcd.print(period);
}
if(col != 9){
lcd.print(":");
}
}
void gametime_calc(){
TimeElements tm; //create a TimeElements variable
//for conversion to time_t variable
//Fill in the elements of tm with those from the msg array
tm.Second = (dateTime[17] - '0')*10 + (dateTime[18] - '0');
tm.Minute = (dateTime[14] - '0')*10 + (dateTime[15] - '0');
tm.Hour = (dateTime[11] - '0')*10 + (dateTime[12] - '0');
tm.Day = (dateTime[8] - '0')*10 + (dateTime[9] - '0');
tm.Month = (dateTime[5] - '0')*10 + (dateTime[6] - '0');
tm.Year = ((dateTime[0] - '0')*1000 + (dateTime[1] - '0')*100 + (dateTime[2] - '0')*10 + (dateTime[3] - '0')) - 1970;
gametime = makeTime(tm); //game time as time_t variable
}
What could be causing this issue? Possibly hardware?